如何让JFrame显示我的面板?

时间:2013-08-16 00:47:52

标签: java swing jframe jpanel

我正在学习使用JFrame以便我可以开始构建一些应用程序,但是我在让我的面板显示组件时遇到了很多麻烦。到目前为止,我创建的代码只是练习并了解实际的JFrame库。

这是我的代码:

    import java.awt.Dimension;
    import java.awt.Toolkit;
    import javax.swing.*;

    public class GUI extends JFrame {

    public static void main(String[] args){
        new GUI();          
    }

    /*
     * This is the constructor of the GUI. It sets the dimension,
     * the visibility, and the positioning of the frame relative
     * to the users dimension of their screen size. 
     */
    public GUI(){
        //'this' is in reference to the frame being created
        this.setSize(500, 400);
        //this.setLocationRelativeTo(null);
        this.setVisible(true);

        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension dim = tk.getScreenSize();     //will hold height and width

        //Grabs the height and width positioning
        //of the screen and centers the window
        int xPos = (dim.width /2) - (this.getWidth() / 2);
        int yPos = (dim.height /2) - (this.getHeight() / 2);

        this.setLocation(xPos, yPos);

        //This prevents someone from resizing the frame
        //By default this method is true.
        this.setResizable(false);

        //Sets how the frame will close
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Sets the title of the frame
        this.setTitle("Woot I created a frame!");

        /*
         * The panel is used to hold all of the different
         * labels and components within the frame.
         */
        JPanel thePanel = new JPanel();

        JLabel label1 =  new JLabel("Tell me something");
        label1.setToolTipText("Click if you need help");

        thePanel.add(label1);

        //Creates a button
        JButton button1 = new JButton("OK");
        button1.setBorderPainted(true);
        button1.setContentAreaFilled(true);
        button1.setToolTipText("This is my button");
        thePanel.add(button1);

        //Creates a text field
        JTextField txtField = new JTextField("Type here", 15);

        txtField.setToolTipText("It's a field");
        //Adds txtField to the Frame
        thePanel.add(txtField);

        //Adds thePanel to the Frame originally created
        this.add(thePanel);
    }    
} 

当我运行它时,会出现一个显示窗口,但我看不到文本字段。当我注释掉文本字段时,按钮和标签就会显示出来。

1 个答案:

答案 0 :(得分:4)

您的问题是您在 之前调用了setVisible(true) ,并且已将组件添加到GUI中,因此GUI不会显示稍后添加的组件。

建议:

  • 避免使用null布局和setBounds(...)
  • 将所有组件添加到JFrame之后, 之前不要致电setVisible(true)
  • 避免使用setSize()。让GUI及其组件自行调整大小。

因此,仅此一项就可以解决您的问题:

public GUI() {
  this.setSize(500, 400);
  // this.setVisible(true);

  // .........

  txtField.setToolTipText("It's a field");
  thePanel.add(txtField);
  this.add(thePanel);

  setVisible(true);

}

如,

import java.awt.Dimension;
import javax.swing.*;

public class Gui2 extends JPanel {
   private static final int PREF_W = 500;
   private static final int PREF_H = 400;

   public Gui2() {
      JLabel label1 = new JLabel("Tell me something");
      label1.setToolTipText("Click if you need help");

      add(label1);
      JButton button1 = new JButton("OK");
      button1.setBorderPainted(true);
      button1.setContentAreaFilled(true);
      button1.setToolTipText("This is my button");
      add(button1);
      JTextField txtField = new JTextField("Type here", 15);

      txtField.setToolTipText("It's a field");
      add(txtField);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      Gui2 mainPanel = new Gui2();

      JFrame frame = new JFrame("Woot I created a frame!");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.setResizable(false);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}