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