簡體   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