gpt4 book ai didi

java - 在构造函数中放置函数后,JFrame 将不会加载

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

这是我尝试加载的函数:

public void ShowButtonsArray()
{
SeatsUI Form = new SeatsUI();
Form.setVisible(true);
Form.setSize(1000, 1000); //Sets the screen's size
JButton [][] buttonArr = new JButton[15][12];
int CellSize = 60;

for(int i = 0; i < 15; i++)
{
for(int j = 0 ; j < 12; j++)
{
buttonArr[i][j] = new javax.swing.JButton();
buttonArr[i][j].setText("" + i + "," + j );
buttonArr[i][j].setBounds(j*CellSize, i*CellSize + 50, CellSize, CellSize);
buttonArr[i][j].addActionListener(new Java.awt.event.ActionListener()
{
//Seat Button Event
public void actionPerformed(java.awt.event.ActionEvent evt)
{
JButton but = (JButton)evt.getSource();
but.setBackground(Color.yellow);
}
}
);

Form.add(buttonArr[i][j]);
}
}
}

在将其转换为函数之前,我将其编写在 run() 中,它运行良好,但将其移至此处后:

public SeatsUI() {
initComponents();
ShowButtonsArray();
}

它刚刚停止工作。我运行该项目,但由于某种原因它被卡住并永远加载该项目。我在这里做错了什么?谢谢。

最佳答案

当您调用 ShowButtonsArray() 时,您可以从此方法中调用 SeatsUI Form = new SeatsUI();当您调用 new SeatsUI() 时,您可以在构造函数中调用 ShowButtonsArray()。我认为它无法摆脱这个循环。

关于java - 在构造函数中放置函数后,JFrame 将不会加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20116830/

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