无法看到Java GUI按钮的标签 [英] Java GUI button's label can't be seen

查看:176
本文介绍了无法看到Java GUI按钮的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是二年级学生,正在从事我的OOP项目(计算器).我已经完成了数字按钮和运算符的功能.现在我处于重新排列按钮的阶段.刚开始,我只是将按钮的大小设置为(50,50),它可以正常工作,并且其标签仍然可见,但是当我决定缩小按钮的大小(30,30)时,其标签变成了"..."

I'm a second year student and I'm working on my OOP project (Calculator). I'm done with the functions of the numeric buttons and operators. Now I'm in the stage of re-arranging my buttons. At first, I just set my button size to (50,50), it works fine and its label is still visible, but when I decided to make it smaller (30, 30), its label turned into "..." instead.

这是图片:

这是我的代码:

  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);

推荐答案

您的问题是您将按钮大小设置为开头.如果改为使用适当的布局管理器并在JFrame上调用pack()来让JButton和GUI自行调整大小,则将获得外观漂亮的GUI,该GUI可以显示任何OS中的所有文本.解决方案:不要使用空布局,不要调用setBounds(...),继续阅读并使用嵌套在JPanels中的合适的布局管理器,让这些布局管理器为您完成所有繁重的布局工作.

Your problem is that you set the button sizes to begin with. If you instead leave the JButtons and the GUI to size itself using proper layout managers and calling pack() on the JFrame, you will get a decent looking GUI that shows all the text in any OS. Solution: don't use null layouts, don't call setBounds(...), read up on and use appropriate layout managers held in nested JPanels, and let these layout managers do all the heavy layout lifting for you.

例如,您可以使用GridLayout创建按钮网格,并通过简单地更改按钮的字体大小来更改网格和按钮的大小.例如,运行下面的代码两次,更改按钮的字体大小(在下面的代码中,浮点常量BTN_FONT_SIZE),并查看GUI如何通过将按钮的大小调整为最佳大小来自动适应按钮的字体.

For example, you could create a grid of buttons using a GridLayout, and alter the size of the grid and the buttons by simply changing the font size of the button. For example run the code below twice, changing the button font size (in the code below, the float constant BTN_FONT_SIZE) and seeing how the GUI automatically accommodates the button font by resizing the button to its optimal size.

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将按钮JPanel嵌套到BorderLayout中,并向其PAGE_START或NORTH末尾添加JTextField,并且使用不同的字体大小,则会看到以下内容:


If you nest the button JPanel into a BorderLayout using JPanel and add a JTextField to its PAGE_START or the NORTH end, and you play with different font sizes, you'll see something like this:

这篇关于无法看到Java GUI按钮的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆