jframe

Panel components disappear when I minimize frame

主宰稳场 提交于 2019-12-10 17:04:55
问题 I have a layout with a main panel whose content is decided by me throught buttons in this way: public void actionPerformed(ActionEvent e) { mainPanel.removeAll(); //removing all current panel components if(e.getActionCommand().equals("content1")){ mainPanel = new Content1Panel(); add(mainPanel,BorderLayout.CENTER); validate(); }else if(e.getActionCommand().equals("content2")){ mainPanel = new Content2Panel(); add(mainPanel,BorderLayout.CENTER); validate(); } } now, if I minimize the frame and

Displaying an image in a JFrame

一笑奈何 提交于 2019-12-10 16:19:29
问题 I am currently learning Java, and I am stuck with something at the moment. I was looking for a way to add an image to my JFrame. I found this on the internet: ImageIcon image = new ImageIcon("path & name & extension"); JLabel imageLabel = new JLabel(image); And after implementing it to my own code, it looks like this (this is only the relevant part): class Game1 extends JFrame { public static Display f = new Display(); public Game1() { Game1.f.setSize(1000, 750); Game1.f.setResizable(false);

What is a good way to Organize multiple JPanel's in a JFrame?

痞子三分冷 提交于 2019-12-10 15:55:21
问题 What I am trying to do is organize five seperate JPanel's within the frame. Here is what the output is supposed to look like: There will be one panel across the top. Two panels directly below the top panel that vertically split the space and then another two panels that split the remaining space horizontally. I cannot figure out how to organize the panels like described above and I think it is because I just don't know the proper syntax. So any help or advise is greatly appreciated here is

Adding JApplet into JFrame

时光怂恿深爱的人放手 提交于 2019-12-10 15:34:40
问题 I am trying to view a JApplet within a JFrame. Class: Paint public void paint(Graphics g) { g.drawString("hi", 50, 50); } public static void main(String args[]) { JFrame frame = new JFrame("test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setJMenuBar(methodThatReturnsJMenuBar()); JPanel panel = new JPanel(new BorderLayout()); frame.add(panel); JApplet applet = new Paint(); panel.add(applet, BorderLayout.CENTER); applet.init(); frame.pack(); frame.setVisible(true); } The

Java how to make JFrames start off as a maximised window

纵饮孤独 提交于 2019-12-10 15:00:33
问题 I want to know how to make a java JFrame start out maximised. I don't want it to be fullscreen (no window around it) I just want it to start out like a normal program such as a web browser. I already know about using: Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); To get the screen size, but when you apply this Dimension to the JFrame it can still be dragged around even though it takes up almost all of the screen. You can press the maximse button to stop this but I would

setPreferredSize does not work

南笙酒味 提交于 2019-12-10 14:30:11
问题 Code: import java.awt.Dimension; import javax.swing.*; public class Game extends JFrame { private static final long serialVersionUID = -7919358146481096788L; JPanel a = new JPanel(); public static void main(String[] args) { new Game(); } private Game() { setTitle("Insert name of game here"); setLocationRelativeTo(null); setLayout(null); setDefaultCloseOperation(EXIT_ON_CLOSE); a.setPreferredSize(new Dimension(600, 600)); add(a); pack(); setVisible(true); } } So I set the preferred size of the

Display image in Java JFrame

笑着哭i 提交于 2019-12-10 12:58:51
问题 What's the best way to display an image at specific coordinates in a java JFrame? I know there are a number of ways to do this, I just need to know the best way to display an image that I am planning on moving around the frame! 回答1: I would suggest adding it as an ImageIcon: ImageIcon image = new ImageIcon("image.jpeg"); add(image); EDIT (How to add it add specific coordinates): First a note, usually (99% of the cases) it is NOT encouraged to position JComponents yourself. Use a LayoutManager

How to make a JFrame scrollable in Java?

拟墨画扇 提交于 2019-12-10 12:43:05
问题 I have this code in which I am trying to fit a scrollable Panel (JPanel) but I don't get it. Here is my code: public class Sniffer_GUI extends JFrame { Canvas c = new Canvas(); ConnectorPropertiesPanel props; public Sniffer_GUI() { super("JConnector demo"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(new GridBagLayout()); init(); getContentPane().add(new JLabel("Connectors example. You can drag the connected component to see how the line will be changed"), new

ALWAYS on top window

怎甘沉沦 提交于 2019-12-10 12:39:44
问题 I'm searching for a solution in order to keep a JFrame always on top and with always I really mean always. setAlwaysOnTop( true ); This won't work when I'm starting a game in fullscreen mode. I know you normally don't want your windows to stay on top but in this case it's required. 回答1: This can't be done. For example, the Windows Task Manager, even when set to Always on Top will get covered up by full-screen applications. This is due to the fact that full-screen applications typically use a

How to close a JFrame and open it consequently, without exit the JVM/Application?

一世执手 提交于 2019-12-10 12:34:36
问题 How to close a JFrame and open it consequently, without exit the JVM/entire application? I want that only the JFrame be exited and opened immediately, without close the JFrame1 nor principally exit the JVM/entire application. I want do it with a JButton. I want a JButton this way: JButton("I close this JFrame and open this immediately, so that the constructor is executed again, without exit the JVM/application"); What I want but I don't know how to do is: I need a method that closes the