所以我正在完成一项任务,我似乎陷入了一个我似乎无法克服的错误。基本上在尝试创建10x10网格时,它并不会出现,而其他所有内容(例如标题和菜单)都是在我运行应用程序时。这是我迄今为止所做的重要部分
public class minesweeperGUI extends JFrame{
private JFrame gameGUI;
private JPanel board;
private JButton[][] boardButtons = new JButton [10][10];
public minesweeperGUI(){
gameGUI = new JFrame("Enhanced Minesweeper"); //Title of the Window
gameGUI.setVisible(true); //We want (true) to set it visible
gameGUI.setSize(400,550); //400 550
gameGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //This will close the frame when you press X. IMPORTANT
gameGUI.getContentPane().setBackground(Color.CYAN);
gameGUI.setLayout(null);
//Panel for minesweeper board
board = new JPanel();
GridLayout experimentLayout = new GridLayout(10, 10);
board.setLayout(experimentLayout);
board.setSize(400,550);
for(int x=0;x<boardButtons.length;x++){
for(int y=0;y<boardButtons[x].length;y++){
boardButtons[x][y] = new JButton();
board.add(boardButtons[x][y]);
}
}
gameGUI.add(board);
}
public static void main(String[] args) {
minesweeperGUI mainInterface = new minesweeperGUI();
}
}
非常感谢任何类型的帮助:)
答案 0 :(得分:2)
gameGUI.setVisible(真); //我们希望(true)将其设置为可见
minesweeperGUI()
中的最后一个代码行,否则你将JComponent
添加到已经可见的Swing Gui(缺少通知程序以进行自动刷新,重新绘制)minesweeperGUI mainInterface = new minesweeperGUI();
应该包含在invokeLater
中,更多内容包含在Oracle跟踪Initial Thread
班级名称应为MinesweeperGUI
,搜索Java Namings Convention
gameGUI.setLayout(空);和board.setSize(400,550);
缺少setBounds
board.setSize(400,550);
JPanel
JFrame
JPanel
,因为Dimension
为零null layout
)
没有理由使用gameGUI.setLayout(null);
,删除getPreferredSize
覆盖private JPanel board;
gameGUI.setLayout(null);
删除代码行board.setSize(400,550);
和pack()
在setVisible(true)
之前添加代码行JFrame
,以便getPreferredSize
正确调整大小(基于private JPanel board;
{{1}}
答案 1 :(得分:1)
删除gameGUI.setLayout(null);
来电并将下一行移至按钮添加
gameGUI.setSize(400,550); //400 550
gameGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //This will close the frame when you press X. IMPORTANT
gameGUI.getContentPane().setBackground(Color.CYAN);
gameGUI.setVisible(true); //We want (true) to set it visible
答案 2 :(得分:0)
我认为一个问题是你混淆了你的实例......
public static void main(String[] args) {
minesweeperGUI mainInterface = new minesweeperGUI();
}
制作一个新的扫雷实例GUI,它扩展了JFrame - 所以这只是一个新的JFrame ......
但是在您的构造函数中,您创建了另一个JFrame实例private JFrame gameGUI;
和gameGUI = new JFrame("Enhanced Minesweeper");
...
我建议你删除私有变量gameGUI(private JFrame gameGUI;
)并简单地使用this
作为你的布局......
//private JFrame gameGUI; //remove this one!
private JPanel board;
private JButton[][] boardButtons = new JButton [10][10];
public minesweeperGUI(){
super("Enhanced Minesweeper"); //construktor from superclass
this.setVisible(true); //use of this
this.setSize(400,550); //use of this
...
//Panel for minesweeper board
board = new JPanel();
GridLayout experimentLayout = new GridLayout(10, 10);
board.setLayout(experimentLayout);
board.setSize(400,550);
for(int x=0;x<boardButtons.length;x++){
for(int y=0;y<boardButtons[x].length;y++){
boardButtons[x][y] = new JButton();
board.add(boardButtons[x][y]);
}
}
this.add(board); //use of this
}
你没有有来写this.setXXX()
你也可以简单地写一下setVisible(true)
......