2016-04-24 35 views
1

我的Java程序正在創建給定JFrame窗口的多個實例,但是,jframe接縫可以在不同實例之間隨意更改大小(從小到大)。也就是說,假設我的程序創建了jframe的10個實例,那麼7可能具有正確的大小,但是3更大。這裏是我的代碼:Java swing JFrame更改實例之間的大小

public class ConvertionDialog extends JFrame{ 
    private JComboBox<String> selection; 
    private JButton okButton; 
public ConvertionDialog(){ 
    super("Select Output Format"); 
    this.setAlwaysOnTop(true); 
    this.setSize(new Dimension(500,55)); 
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    this.selection=new JComboBox<String>(GUI.getInstance().getRdfNotations()); 
    this.okButton=new JButton("OK");  
    this.setLayout(new BorderLayout()); 
    this.add(this.selection,BorderLayout.CENTER); 
    this.add(this.okButton,BorderLayout.EAST); 
    this.setVisible(true); 
} 
} 

JFrame中使用簡單new ConvertionDialog();

這裏實例化一個screenprint: enter image description here

回答

3

所有Swing組件應在事件指派線程(EDT)創建。當你得到隨機結果時,這可能是因爲你沒有在EDT上創建GUI。

查看在How to Make Frames的Swing教程中找到的FrameDemo示例代碼。該代碼將向您展示如何更好地構建代碼,以便在EDT上創建GUI。

您還應該閱讀Concurrency in Swing上的教程部分,以獲取有關EDT的更多信息以及爲什麼需要這樣做。

如果問題仍然存在,那麼您需要發佈一個合適的SSCCE來演示問題並列出您的操作系統和JDK版本,以便使用這些平臺的人員可以測試代碼以查看它們是否具有相同的問題。

+0

我嘗試添加SwingUtilities.invokeLater(新的Runnable(){ \t公共無效的run(){ \t \t新ConvertionDialog(); \t} \t});但是,這並沒有解決問題。我在課堂上應該做些什麼(擴展JFrame)?謝謝。 –

+1

@DanielValland:扔掉你的代碼。按照Camickr在答案中鏈接的[Oracle How To Make Frames教程](http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html)。根據需要多次查看教程,直到完全瞭解如何製作JFrame。 –