jframe

Java GUI: about getContentPane( ) method and content

早过忘川 提交于 2020-01-12 01:47:23
问题 In this piece of code: JLabel emptyLabel = new JLabel(""); emptyLabel.setPreferredSize(new Dimension(175, 100)); frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); I can see it makes a new label and adds it to the JFrame object frame . But I want to understand what does getContentPane() do, and why do I need it? I read this API but I still didn't understand. 回答1: Every JPanel is a container, so either add it to a panel then add it to the container or directly use add(component) or

How can I replace one of two JPanels with another JPanel in Java?

烂漫一生 提交于 2020-01-11 11:42:11
问题 I designed an interface for the welcome screen with one JFrame included two JPanels (JPanel1 on right and JPanel2 on left). The buttons on the left is to switch the Panels in JPanel1. I want to press on a button to replace JPanel1 content with another JPanel but I don`t know how. Please help. 回答1: Here is a very simple example of something that should approximate your description. On the left, we have a hug button to toggle the content of the right panel. On the right, you have a panel with a

frame 2 inside frame 1

天大地大妈咪最大 提交于 2020-01-11 07:43:08
问题 I have 2 classes; Students and RegisterStudents, and hence 2 different main_panel(Class 1) and panel_1 (Class 2). What I am trying to do is, when a button on the Students Interface is pressed, the whole panel_1 should appear within main_panel. I have set both to same size already. is that possible? The code i got so far is: JButton btnNewButton = new JButton("Register Student"); btnNewButton.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { Students

How do I start optimising my Java code? - CPU is at 100%

冷暖自知 提交于 2020-01-11 07:30:15
问题 i have written an application, but for some reason it keeps peaking at 100%. I ran a profile r on a few of the classes and a report show that isReset() and isRunning() seems to be called alot of times. Do you see anything wrong please inform me. thanks Class 1 is the only class that uses the isReset() code so i hope this helps u guys in detecting the error Class 1 package SKA; /* * ver 0.32 June 2009 * * Bug Fix Release: * * Fixed Array resize * Fixed Red Black Tree delete method * Fixed Red

How to set Jframe Background Image in GroupLayout Java

喜夏-厌秋 提交于 2020-01-11 06:15:42
问题 Am trying to set a background image for my frame but it does not work. I tried this link: Setting background images in JFrame The code: setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("/Images/about.png"))))); I tried adding the above code to my Contentpane but it does not work. public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MainMenu frame = new MainMenu(); frame.setVisible(true); } catch (Exception e) { e

How to use setVisible in JFrames?

大城市里の小女人 提交于 2020-01-11 03:58:07
问题 In my program I have two JFrame instances. When I click next button I want to show next frame and hide current frame. So I use this.setVisible(false) and new Next().setVisible(true) . But in Next window if I click back button I want to set previous frame to be visible again and next frame must be ended (which means it must be exited). Is there any special method(s) to do this? How can I do it? 回答1: Consider using CardLayout instead of hunting for how many JFrames there are. Then.. only one

Opening a new JFrame from a Button

蹲街弑〆低调 提交于 2020-01-09 12:10:52
问题 I want to open a new JFrame by clicking a button (btnAdd); I have tried to create an actionlistener but I am having no luck; the code runs but nothing happens when the button is clicked. The methods in question are the last two in the following code. Any help is much appreciated! package AdvancedWeatherApp; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font;

Opening a new JFrame from a Button

旧城冷巷雨未停 提交于 2020-01-09 12:08:07
问题 I want to open a new JFrame by clicking a button (btnAdd); I have tried to create an actionlistener but I am having no luck; the code runs but nothing happens when the button is clicked. The methods in question are the last two in the following code. Any help is much appreciated! package AdvancedWeatherApp; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font;

Opening a new JFrame from a Button

青春壹個敷衍的年華 提交于 2020-01-09 12:05:38
问题 I want to open a new JFrame by clicking a button (btnAdd); I have tried to create an actionlistener but I am having no luck; the code runs but nothing happens when the button is clicked. The methods in question are the last two in the following code. Any help is much appreciated! package AdvancedWeatherApp; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font;

Update jPanel background color when a button is pressed

孤街浪徒 提交于 2020-01-07 08:32:37
问题 I'm just starting with Java and I wanted to make a little program that opens a jFrame with a text field in which you can write a number. Then you press a button and another jFrame with a jPanel, which will turn green if the number is even, and black if it's odd. I edited the code of the jPanel so that the color changes depending on the number, but the problem is that it will only work once. If I write "2" and press the button, the jFrame will appear with a green panel, but then if I write