2015-07-12 53 views
0

因此,我在我寫的Java程序中有幾個JButton,它顯然包含一個值(如在按鈕上出現的文本中)。JButton的值總是適合Windows中的按鈕,但不適用於Linux

當我在Windows中運行應用程序時,這個文本總是適合在JButton中 - 無論窗口有多小(我爲該窗口設置了最小尺寸)。但是,當我在Linux中運行應用程序時,文本似乎不適合按鈕。

我定義我的Jbutton將是這樣的:

public class ButtonPanel extends JPanel { 

private JButton undoButton; 

/** 
* Button panel constructor. 
*/ 
public ButtonPanel() { 
    // Set the layout 
    this.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 5)); 

    // Add all buttons and register their listeners 
    undoButton = new JButton("Undo"); 
    undoButton.setPreferredSize(new Dimension(100, 50)); 
    undoButton.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) { 
             System.out.println("Undo button pressed"); 
             try { 
              App.performActivity(Activity.UNDO); 
             } catch (Exception e1) { 
              e1.printStackTrace(); 
             } 
            } 
           }); 
    this.add(undoButton); 
} 

有沒有人有什麼想法?

謝謝

卡勒姆

回答

0
undoButton.setPreferredSize(new Dimension(100, 50)); 

擺脫這種說法的!

不要硬編碼組件的首選大小。每個組件都將確定其首選大小,佈局管理器將使用此信息將組件定位在面板上。

+0

絕對正確!非常感謝。 – Callum

相關問題