没有显示任何内容



下面写的代码只显示了这一点,这是在控制台中,为什么它没有显示我制作的UI,我已经使用windows生成器创建了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");       
        }   
    }

当我将选项卡从源切换到设计时,它也会显示。提前感谢。

在intiGUI()方法中,将frame替换为'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
}

您需要实际将JFrame对象设置为

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

您应该将类中的JFrame替换为自身。

  • 删除JFrame frame = new JFrame();
  • frame.setSize(500, 500)替换为this.setSize(500, 500)
  • 每隔一次出现frame时重复该步骤

然后您必须创建JFrame对象。

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

最新更新