简体   繁体   中英

Java: Objects in JFrame are messed up

I'm creating a simple Java JFrame in Eclipse with a label, 2 radio buttons with 2 textfields, and a JButton. When i run the program, the objects inside it are messed up, the buttons and textfields don't show up and sometimes a textfield takes the entire size of the frame. However, when I minimize/maximize the frame and then restore it, they work normally. Here's the code:

   import java.awt.BorderLayout;
   import java.awt.Dimension;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;
   import java.awt.event.WindowAdapter;
   import java.awt.event.WindowEvent;

   import javax.swing.BorderFactory;
   import javax.swing.Box;
   import javax.swing.BoxLayout;
   import javax.swing.JButton;
   import javax.swing.JFrame;
   import javax.swing.JLabel;
   import javax.swing.JPanel;
   import javax.swing.JRadioButton;
   import javax.swing.JTextField;

   public class myframe {

   public static void main(String s[]) {
    JFrame frame = new JFrame("Title");
    frame.addWindowListener(new WindowAdapter() {

        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    // This is an empty content area in the frame
    JLabel jlbempty = new JLabel("");
    jlbempty.setPreferredSize(new Dimension(500, 400));
    frame.getContentPane().add(jlbempty, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true); 
    frame.setBounds(0, 0, 500, 400);

JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(Box.createRigidArea(new Dimension(0,5)));
panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

JLabel label = new JLabel("My label");
panel.add(label);

JPanel buttonPane = new JPanel();
frame.add(buttonPane);
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JRadioButton cb = new JRadioButton("1");
buttonPane.add(cb);
JTextField tf = new JTextField(0);
tf.setText("");
buttonPane.add(tf);

JPanel panel3 = new JPanel();
frame.add(panel3);
panel3.setLayout(new BoxLayout(panel3, BoxLayout.LINE_AXIS));
panel3.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JRadioButton cb2 = new JRadioButton("2");
panel3.add(cb2);
JTextField tf2 = new JTextField(0);
tf.setText("");
panel3.add(tf2);


JPanel panel2 = new JPanel();
JButton button = new JButton("click me");
frame.add(panel2);
panel2.add(button);
button.addActionListener(new Action());

panel.add(buttonPane);
panel.add(panel3);
panel.add(panel2);
    }

static class Action implements ActionListener{

    public void actionPerformed(ActionEvent arg0) {
        JFrame frame2 = new JFrame("Clicked");
        frame2.setVisible(true);
        frame2.setSize(100, 200);
        JLabel label2 = new JLabel("You clicked me");
        JPanel panel2 = new JPanel();
        frame2.add(panel2);
        frame2.add(label2);

    }

}
}

In your main method you need to do this at the very end:

frame.pack();
frame.setVisible(true);

You should call frame.pack(); again after adding all the components, so that all the container elements can resize to fit their components best.

http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#pack()

You should also call frame.SetVisible(true); at the very end, so the form is only displayed ones all components are loaded (otherwise you can see a black box while it loads).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM