简体   繁体   English

如何将按钮的文本调整为按钮的大小 - Java 的 Swing

[英]How to adjust buttons' text to buttons' size - Java's Swing

I'm trying to create a simple GUI with a grid containing numbered buttons.我正在尝试使用包含编号按钮的网格创建一个简单的 GUI。 Problem is, the buttons' text will not adjust itself to the buttons' size.问题是,按钮的文本不会根据按钮的大小自行调整。

let me first show you the code, so you'll see what I mean:让我首先向您展示代码,这样您就会明白我的意思:

import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class SimpleGUI implements ActionListener {
    
    private JFrame frame;
    private JPanel grid;
    private JButton[] buttons;
    
    public SimpleGUI() {
        frame=new JFrame("Buttons grid");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        grid=new JPanel();
        buttons=new JButton[100];
        
        
        for (int i=0; i<buttons.length; i++) {
            buttons[i]=new JButton(Integer.toString(i+1));
            buttons[i].setActionCommand(Integer.toString(i+1));
            grid.add(buttons[i]);
            buttons[i].addActionListener(this);
        }
        
        frame.getContentPane().add(grid);
    }
    
    public static void main(String[] args) {
        SimpleGUI gui=new SimpleGUI();
        gui.go();
    }
    
    
    public void go() {
        grid.setLayout(new GridLayout(10,10));
        
        frame.setSize(500,500);
        frame.setVisible(true);
    }


    public void actionPerformed(ActionEvent e) {
        String buttonNum=e.getActionCommand();
        System.out.println("You've pressed "+buttonNum);
    }
    
}

The buttons are too small for the text to fit in, so instead of numbers, three dots will appear on them.按钮太小,无法容纳文本,因此按钮上会出现三个点而不是数字。 ( ... ) Only when I expand the window (and thus expanding the buttons' dimension accordingly), the numbers appear on the buttons properly. ( ... ) 只有当我扩展窗口(从而相应地扩展按钮的尺寸)时,数字才会正确显示在按钮上。 Is there a way to make the text adjust itself to the buttons' dimension so it'll always be visible?有没有办法让文本根据按钮的尺寸自行调整,使其始终可见?

It indeed is possible to give a bit more space to the text within the buttons.这的确可能给多一点空间的按钮中的文本。 The extra empty space around the text is controlled by the margins and that can be changed:文本周围的额外空白空间由边距控制,可以更改:

button.setMargin(new Insets(0, 0, 0, 0));

If you want to make the buttons square by default, you can override their preferred size:如果你想让按钮默认为方形,你可以覆盖它们的首选大小:

buttons[i] = new JButton(Integer.toString(i + 1)) {
    @Override
    public Dimension getPreferredSize() {
        Dimension naturalSize = super.getPreferredSize();
        int sideLength = Math.max(naturalSize.width, naturalSize.height);
        return new Dimension(sideLength, sideLength);
    }
};

Then the initial layout will be square, when using pack() (which you should be using, as per Andrew Thompson's answer).然后,当使用pack()时,初始布局将是方形的pack()根据 Andrew Thompson 的回答,您应该使用它)。

Edit: Changed the preferred size to take in account the button's normal preferred dimensions.编辑:更改了首选尺寸以考虑按钮的正常首选尺寸。 (Credits to A. Thompson again). (再次归功于 A. Thompson)。

Make the change seen below.进行如下所示的更改。

public void go() {
    grid.setLayout(new GridLayout(10,10));

    //frame.setSize(500,500);
    frame.pack();
    frame.setVisible(true);
}

The trick here is not to change the buttons at all, but to get the GUI to adjust to the size of the buttons.这里的技巧根本不是改变按钮,而是让 GUI 调整到按钮的大小。 That is achieved by calling pack() which..这是通过调用pack()实现的,它..

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.使此窗口的大小适合其子组件的首选大小和布局。 .. ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM