我正在爲我的學生寫一個MathQuiz,包括JLatexMath用於蜂鳴器的渲染和輸入。問題在於,有時(如每第四次)當我啓動程序時,沒有任何組件可見。它們在調整JFrame大小後出現。 首先,我想在jinput或jlatexMath庫錯誤的,但我得到了同樣的錯誤,即使這個最小代碼:Java佈局不顯示組件(有時)
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();
}
}
什麼是錯的,用的代碼?
那麼,如果您對Swing有奇怪的問題,最好注意文檔並確保所有的[Swing代碼在Event Dispatch Threa上執行d。](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html)從那裏開始。 – markspace 2014-10-31 01:05:07
好的,thx爲快速回答。我添加了 System.out.println(javax.swing.SwingUtilities.isEventDispatchThread()); 給構造函數我在控制檯上弄錯了。我怎樣才能讓我的線程EventDispatchThread? – user3648884 2014-10-31 01:12:21
我給你的鏈接左側有幾個鏈接。最好閱讀所有的文檔,否則誰知道你錯過了什麼。 [嘗試此鏈接,](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html),但您確實需要閱讀關於Java中併發性的整個部分。至少。 – markspace 2014-10-31 01:16:56