简体   繁体   English

简单的 JFrame 不显示

[英]Simple JFrame doesn't show up

I'm learning how to make simple JFrame's and I did something wrong.我正在学习如何制作简单的 JFrame,但我做错了。 I'm sure it's a quick fix.我确定这是一个快速修复。 If somebody could tell me the things I'm doing wrong, I would greatly appreciate it.如果有人能告诉我我做错了什么,我将不胜感激。 Thanks.谢谢。

Oh... also... could you check up on this and tell me if they follow good practice?呵呵......还......你能不能检查了在,告诉我,如果他们遵循好的做法呢? (Besides their naming conventions which I can easily tell are terrible.) (除了他们的命名约定,我可以很容易地看出它们很糟糕。)

My class's code:我的班级代码:

FlowLayout flow = new FlowLayout();
JPanel pan;
JFrame fra;
JButton but = new JButton();
JLabel lab = new JLabel();

public MainScreen(){
    gui();
}


public void gui(){

    fra = new JFrame("ATR Utilities");
    fra.setVisible(true);
    fra.setSize(400, 600);
    fra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    pan = new JPanel(flow);
    pan.setBackground(Color.CYAN);

    but = new JButton("Test");
    lab = new JLabel("Test label");

    pan.add(but);
    pan.add(lab);



}

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

You aren't adding your panel to your frame, and you should add everything before you make it visible.您没有将面板添加到框架中,您应该在使其可见之前添加所有内容。

public void gui() {
    fra = new JFrame("ATR Utilities");
    fra.setSize(400, 600);
    fra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    pan = new JPanel(flow);
    pan.setBackground(Color.CYAN);

    but = new JButton("Test");
    lab = new JLabel("Test label");

    pan.add(but);
    pan.add(lab);
    fra.add(pan);
    fra.setVisible(true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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