gpt4 book ai didi

java - jbutton 在启动时不可见

转载 作者:行者123 更新时间:2023-12-01 08:13:22 25 4
gpt4 key购买 nike

我正在为数独开发 GUI。我有一个适合我的 Droid (SudokuFree) 的好应用程序,我想复制它的功能并添加一个小功能(保存正在进行的棋盘,继续,并在需要时恢复)。一些代码已被注释掉,以使其更加SSCCE,如果您有疑问,请参阅第二篇文章。两篇文章对我帮助很大,我从他们那里借用了下面的代码。

Action listener for JButton array

Building a GUI for a Sudoku Solver (Complete with ASCII Example)

我的问题是 JButton 仅在滚动后才可见。我搜索了以前的帖子,只找到了对 setVisible(true) 的引用,我已经完成了。

谢谢

public class RunSudokuNinja implements Runnable{
@Override
public void run() {
//SudokuEngineInterface sudokuEngine = new SudokuEngine();
SudokuView sudokuView = new SudokuView();
//sudokuView.setSudokuImplementation(sudokuEngine);
sudokuView.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new RunSudokuNinja());
}
}

public class SudokuView extends JFrame{
//SudokuController controller;
//SudokuEngine sudokuEngine;

private static final int width = 500;
private static final int height = 650;
private static final int yMar = 100;
private static final int xMar = 25;
private static final int cellSize = 40;
private static final int gridSpace = 3;
private static final int secSpace = 9;

public JButton[][] btnz = new JButton[9][9];

public SudokuView() {
//controller = new SudokuController();
//sudokuEngine = new SudokuEngine();

setTitle("Sudoku Ninja 0.1");
setSize(width, height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setBackground(Color.GRAY);

int y=yMar;
for (int r=0;r<9;r++){
int x=xMar;
for (int c=0;c<9;c++){
btnz[r][c]=new JButton("1");
btnz[r][c].setBounds(x,y,cellSize,cellSize);
btnz[r][c].setVisible(true);
add(btnz[r][c]);
if (c==2||c==5){
x = x+cellSize+secSpace;
}else{
x = x+cellSize+gridSpace;
}
}
if (r==2||r==5){
y = y+cellSize+secSpace;
}else{
y = y+cellSize+gridSpace;
}
}
setVisible(true);
}

@Override
public void paint(Graphics g){

}


public void setSudokuImplementation(SudokuEngineInterface listener) {
//controller.setListener(listener);
}
}

最佳答案

您正在重写 paint 方法,告诉它什么也不做,请将您的 paint() 更正为:

public void paint(Graphics g){
super.paint(g);
//do whatever you need to do here
}

关于java - jbutton 在启动时不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15599256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com