Java JFrame在创建新对象时不显示组件
由于我的程序中存在问题,代码的第一次运行非常完美,但是当尝试创建一个新对象,然后调用使窗口显示的原始方法时,窗口以正确的大小显示,标题正确,但没有显示任何组件 我已经检查过没有静态变量,所有必需的变量都在构造函数中初始化 类来显示窗口:Java JFrame在创建新对象时不显示组件,java,swing,user-interface,frame,Java,Swing,User Interface,Frame,由于我的程序中存在问题,代码的第一次运行非常完美,但是当尝试创建一个新对象,然后调用使窗口显示的原始方法时,窗口以正确的大小显示,标题正确,但没有显示任何组件 我已经检查过没有静态变量,所有必需的变量都在构造函数中初始化 类来显示窗口: public class DifficultySelect implements ActionListener { private JPanel getInput; private JFrame startFrame; p
public class DifficultySelect implements ActionListener
{
private JPanel getInput;
private JFrame startFrame;
private JButton easy;
private JButton medium;
private JButton hard;
private JButton custom;
private JLabel labelRow;
private JLabel labelColumn;
private JLabel labelMines;
private JTextArea textRow;
private JTextArea textColumn;
private JTextArea textMines;
public ArrayList<Integer> getArray;
public DifficultySelect()
{
getInput = new JPanel();
startFrame = new JFrame("Select Difficulty:");
easy = new JButton();
medium = new JButton();
hard = new JButton();
custom = new JButton();
labelRow = new JLabel();
labelColumn = new JLabel();
labelMines = new JLabel();
textRow = new JTextArea(5, 20);
textColumn = new JTextArea(5, 20);
textMines = new JTextArea(5, 20);
getArray = new ArrayList<Integer>();
}
public void setDisplay()
{
getInput.setLayout(new BoxLayout(getInput, BoxLayout.PAGE_AXIS));
getInput.setVisible(true);
Dimension buttonSize = new Dimension(300,40);
easy.setText("Easy: 5x5 - 4 Mines");
easy.setMaximumSize(buttonSize);
easy.addActionListener(this);
medium.setText("Medium: 10x10 - 20 Mines");
medium.setMaximumSize(buttonSize);
medium.addActionListener(this);
hard.setText("Hard: 15 x 15 - 50 Mines");
hard.setMaximumSize(buttonSize);
hard.addActionListener(this);
custom.setText("Custom: Enter Rows/Columns/Mines then Click");
custom.setMaximumSize(buttonSize);
custom.addActionListener(this);
labelRow.setText("Enter Row Size: ");
labelColumn.setText("Enter Column Size: ");
labelMines.setText("Enter Amount of Mines:");
textRow.setAlignmentX(0);
textColumn.setAlignmentX(0);
textMines.setAlignmentX(0);
getInput.add(easy);
getInput.add(medium);
getInput.add(hard);
getInput.add(custom);
getInput.add(labelRow);
getInput.add(textRow);
getInput.add(labelColumn);
getInput.add(textColumn);
getInput.add(labelMines);
getInput.add(textMines);
startFrame.add(getInput);
startFrame.setSize(310, 250);
startFrame.setResizable(false);
startFrame.setVisible(true);
startFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
我一直在尝试许多不同的方法来实现这一点,我目前在构造函数中进行调用,因此现在我使用StartGame newGame=newstartgame调用对象;这只会产生一个空白的白色屏幕,尽管它的大小正确,并且具有正确的JFrame名称,这意味着某些方面的创建是正确的。请尝试启动GUI:
public class StartGame {
public static void main(String[] args) {
DifficultySelect d = new DifficultySelect();
d.setDisplay();
}
}
看起来:
当您想在其他地方再次显示此表单时,可以使用以下代码:
if (d == null) {
d = new DifficultySelect();
d.setVisible(true);
} else {
d.setVisible(true);
}
如果要创建新实例,请将以下代码放在您的位置:
DifficultySelect new_d = new DifficultySelect();
new_d.setDisplay();
为了更快地获得更好的帮助,请发布or。您是否尝试过重新绘制JPanel和/或JFrame?我尝试过使用重新绘制,但没有成功。我不知道确切的问题是什么。您正在询问如何从与main不同的方法调用GUI?确切的问题是:从main调用时,框架显示正确。当我创建一个新的类“困难”实例时,请选择制作的框架,并尝试显示该框架,按钮/文本区域/标签不会被输出。我不知道您到底在告诉我什么,这就是我的程序在第一次运行时的外观。直到我再次回忆起创建该框架的方法,问题才开始出现。你的意思是吗?这解决了问题,谢谢。我是在没有新功能的情况下创建的谢谢,我很高兴能帮上忙!
DifficultySelect new_d = new DifficultySelect();
new_d.setDisplay();