我正在为我的学生编写一个 MathQuiz,包括用于渲染的 JLatexMath 和用于蜂鸣器的 jinput。问题是,有时(就像每四次一样)当我启动程序时,没有任何组件可见。它们在调整 JFrame 大小后出现。
首先,我想到了 jinput 或 jlatexMath 库中的错误,但即使使用这个最少的代码,我也确实得到了相同的错误:
public class Shell extends JFrame{
private JButton button1;
private JButton button2;
private Formula formula;
public Shell() {
super("blaBla");
this.setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
Box b = Box.createHorizontalBox();
button1 = new JButton(" ");
button1.setEnabled(false);
b.add(button1);
b.add(Box.createHorizontalGlue());
button2 = new JButton(" ");
button2.setEnabled(false);
b.add(button2);
add(b);
JPanel formulaPanel = new JPanel();
add(Box.createVerticalStrut(20));
add(formulaPanel);
}
public static void main(String[] args) {
Shell s = new Shell();
}
}
代码有什么问题吗?
从移动开始setVisible(true);
到构造函数的末尾。
而不是来过这里...
public Shell() {
super("blaBla");
this.setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
//...
}
移到这里...
public Shell() {
super("blaBla");
//...
add(Box.createVerticalStrut(20));
add(formulaPanel);
setVisible(true);
}
为了防止任何其他可能的图形故障,您应该始终从事件调度线程内启动 UI,请参阅初始线程更多细节
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)