繁体   English   中英

Java:添加此面板时,我做错什么了吗?

[英]Java : Did i do something wrong when i added this panel?

我在中间创建pong,不确定是否做错了什么。 如果错误与框架无关,请告诉我。

public class PongFrame extends JFrame {
  PongFrame(){
    super("PONG");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(600,400);
    this.setVisible(true);
    this.setResizable(false);
    this.setLocation(450,200);

    setLayout(new BorderLayout());
    PongPanel panel = new PongPanel();

    add(panel, BorderLayout.CENTER);

  }
}

您正在调用this.setVisible(true); 在完成GUI的构造之前,应尽可能先调用此函数

public class PongFrame extends JFrame {
    PongFrame(){
        super("PONG");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);

        setLayout(new BorderLayout());
        PongPanel panel = new PongPanel();

        add(panel, BorderLayout.CENTER);

        this.setSize(600,400);
        this.setLocation(450,200);
        this.setVisible(true);

    }
}

您还应该依赖JFrame#pack而不是setSize

暂无
暂无

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

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