3

我是第二年級學生,我正在開發我的OOP項目(計算器)。我完成了數字按鈕和運算符的功能。現在我正在重新安排我的按鈕。起初,我只是將我的按鈕尺寸設置爲(50,50),它的工作正常,它的標籤仍然可見,但是當我決定將其縮小(30,30)時,它的標籤變成了「...」 。無法看到Java GUI按鈕的標籤

這裏的PIC:

GUI btn shows ... instead of MC, MR, MS, and M+

而且我的繼承人代碼:

lblEdit.setBounds(-138,-5,180,50);  
    lblView.setBounds(-90,-5,180,50); 
    lblHelp.setBounds(-40,-5,180,50); 
    txt.setBounds(15,35,250,30);  // text pane 
    txt2.setBounds(0,330,100,20); 
    blank.setBounds(15,80,30,30);  // this is just an extra button, no use at all, OK? :D 
    btnMC.setBounds(15,115,30,30); 
    btnMR.setBounds(15,150,30,30); 
    btnMS.setBounds(15,185,30,30); 
    btnMp.setBounds(15,220,30,30); 
+0

可能重複(http://stackoverflow.com/questions/7971178/find-out-if-text-of-jlabel-exceeds-label-大小) – ziesemer 2013-02-24 05:55:56

回答

6

你的問題是,你設置的按鈕大小開始。如果您使用適當的佈局管理器讓JButton和GUI自行調整大小,並在JFrame上調用pack(),您將看到一個體面的GUI,它可以顯示任何操作系統中的所有文本。解決方案:不要使用空佈局,不要調用setBounds(...),閱讀並使用嵌套JPanel中保存的合適佈局管理器,讓這些佈局管理器爲您完成所有沉重的佈局。

例如,您可以使用GridLayout創建一個按鈕的網格,並通過更改按鈕的字體大小來更改網格和按鈕的大小。例如,運行下面的代碼兩次,更改按鈕字體大小(在下面的代碼中,浮點數常量BTN_FONT_SIZE),並通過將按鈕的大小調整爲最佳大小來查看GUI如何自動適應按鈕字體。

import java.awt.GridLayout; 
import javax.swing.*; 

public class CalcEg { 
    private static final float BTN_FONT_SIZE = 20f; // **** try using 40f here **** 
    private static final String[][] BTN_LABELS = { 
     {"7", "8", "9", "-"}, 
     {"4", "5", "6", "+"},  
     {"1", "2", "3", "/"}, 
     {"0", ".", " ", "="} 
    }; 
    private JPanel mainPanel = new JPanel(); 

    public CalcEg() { 
     int rows = BTN_LABELS.length; 
     int cols = BTN_LABELS[0].length; 
     int gap = 4; 
     mainPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap)); 
     mainPanel.setLayout(new GridLayout(rows, cols, gap, gap)); 
     for (String[] btnLabelRow : BTN_LABELS) { 
     for (String btnLabel : btnLabelRow) { 
      JButton btn = createButton(btnLabel); 
      // add ActionListener to btn here 
      mainPanel.add(btn); 
     } 
     } 
    } 

    private JButton createButton(String btnLabel) { 
     JButton button = new JButton(btnLabel); 
     button.setFont(button.getFont().deriveFont(BTN_FONT_SIZE)); 
     return button; 
    } 

    public JComponent getMainComponent() { 
     return mainPanel; 
    } 

    private static void createAndShowGui() { 
     CalcEg mainPanel = new CalcEg(); 

     JFrame frame = new JFrame("CalcEg"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel.getMainComponent()); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

如果嵌套按鈕的JPanel到BorderLayout的使用JPanel並一個JTextField添加到其PAGE_START或北端,和你玩不同的字體大小,你會看到這樣的事情:

的[找出是否一個JLabel的文本超出標籤尺寸]

enter image description here

+0

另一個相關示例[here](http://stackoverflow.com/a/10596800/10 57230) – 2013-02-24 06:42:24

+0

非常感謝氣墊船!你是這樣一個小人物! ;) – 2013-02-24 09:49:57

+2

當你有機會時,請[接受](http://meta.stackexchange.com/a/65088/155831)答案。 – 2013-02-24 10:42:58