什么都没有显示

时间:2014-04-25 08:31:30

标签: java swing jframe layout-manager null-layout-manager

下面写的代码只显示这是在控制台中为什么它没有显示我使用Windows构建器创建UI的UI。

 import javax.swing.JFrame;

    public class getData extends JFrame {

         JFrame frame = new JFrame();
         JTextArea textArea = new JTextArea();
         JTextArea textArea_1 = new JTextArea();
         JTextArea textArea_2 = new JTextArea();
         JLabel    lblNewLabel = new JLabel("BOX");
         JLabel    lblPlc = new JLabel("PLC");
         JLabel    lblHmi = new JLabel("HMI");
         JButton   btnSubmit = new JButton("SUBMIT");
         JButton   btnExport = new JButton("EXPORT");


        public getData() {
            initGUI();
    }   
        void initGUI(){

            getContentPane().setLayout(null);
            this.textArea.setBounds(28, 50, 105, 162);

            getContentPane().add(this.textArea);
            this.textArea_1.setBounds(183, 50, 105, 162);

            getContentPane().add(this.textArea_1);
            this.textArea_2.setBounds(319, 50, 105, 162);

            getContentPane().add(this.textArea_2);
            this.lblNewLabel.setFont(new Font("Times New Roman", Font.PLAIN, 13));
            this.lblNewLabel.setBounds(28, 25, 46, 14);

            getContentPane().add(this.lblNewLabel);
            this.lblPlc.setFont(new Font("Times New Roman", Font.PLAIN, 13));
            this.lblPlc.setBounds(183, 25, 46, 14);

            getContentPane().add(this.lblPlc);
            this.lblHmi.setFont(new Font("Times New Roman", Font.PLAIN, 13));
            this.lblHmi.setBounds(317, 25, 46, 14);

            getContentPane().add(this.lblHmi);
            this.btnSubmit.setFont(new Font("Times New Roman", Font.PLAIN, 13));
            this.btnSubmit.setBounds(22, 228, 89, 23);

            getContentPane().add(this.btnSubmit);
            this.btnExport.setFont(new Font("Times New Roman", Font.PLAIN, 13));
            this.btnExport.setBounds(300, 228, 89, 23);
            getContentPane().add(this.btnExport);
            frame.setSize(500, 500);
            frame.add(getContentPane());        
            frame.setTitle("Bar Code Scanner");
            frame.setVisible(true);     
    }        
        public static void main(String[] args) {
            System.out.println("This Is It");       
        }   
    }

当我将标签从源切换到设计时,它也会显示。 在此先感谢。

3 个答案:

答案 0 :(得分:1)

在intiGUI()方法中,将框架替换为' this'。

        frame.setSize(500, 500);
        frame.add(getContentPane());        
        frame.setTitle("Bar Code Scanner");
        frame.setVisible(true); 

使用以下代码:

        this.setSize(500, 500);
        this.add(getContentPane());        
        this.setTitle("Bar Code Scanner");
        this.setVisible(true); 

此处当前类本身是一个JFrame,您已将组件添加到当前内容窗格。因此,您需要设置当前JFrame的大小和可见性,即当前类。

public static void main(String[] args) 
{

  System.out.println("This Is It"); 

   new getData();     // ** You need to instantiate the class

}

答案 1 :(得分:0)

您需要实际制作JFrame对象

    public static void main(String[] args) {
        System.out.println("This Is It"); 
        JFrame frame = new getData();     
    } 

答案 2 :(得分:0)

您应该用自己替换类中的JFrame。

  • 删除JFrame frame = new JFrame();
  • frame.setSize(500, 500)替换为this.setSize(500, 500)
  • frame
  • 的每个其他出现时重复此操作

然后你必须制作JFrame-Object。

public static void main(String[] args) {
  System.out.println("This Is It"); 
  getData yourFrame = new getData();     
} 
相关问题