Removing the three dots “…” from a JButton?

2020-02-09 09:21发布

Hey, I am creating a calculator program, with some small buttons, I want one of the buttons to have "Ans" on them, but whenever I make the JButton smaller then 50, 50, it will show three dots. "...", how can I remove these dots and show the normal text given?

5条回答
Viruses.
2楼-- · 2020-02-09 09:32

You could change the size of the font on the button. See these links:

Setting font size

Increasing font size (Can be easily changed to decrease font size)

查看更多
放我归山
3楼-- · 2020-02-09 09:39

Use setMargin(Insets m) to adjust the space between the JButton border and the label. The default is (2, 14, 2, 14). To maximize the space available for the label (and to remove the dots completely) you can use something like

myButton.setFont(new Font("Tahoma", Font.BOLD, 11));
myButton.setMargin(new Insets(0, -1, 0, -20));
myButton.setHorizontalAlignment(SwingConstants.LEFT);
查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-02-09 09:45

This code attempts to explain why layouts and preferred sizes are so important. The important part lies in the input/output.

TestGuiSize.java

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

class TestGuiSize {

    public static void addButtonToPanel(JPanel panel, String label) {
        JButton button = new JButton(label);
        button.setMargin(new Insets(1,1,1,1));
        panel.add(button);
    }

    public static void main(String[] args) {
        JPanel p = new JPanel(new GridLayout(4,3,3,3));
        addButtonToPanel(p, "7");
        addButtonToPanel(p, "8");
        addButtonToPanel(p, "9");
        addButtonToPanel(p, "/");

        addButtonToPanel(p, "4");
        addButtonToPanel(p, "5");
        addButtonToPanel(p, "6");
        addButtonToPanel(p, "*");

        addButtonToPanel(p, "1");
        addButtonToPanel(p, "2");
        addButtonToPanel(p, "3");
        addButtonToPanel(p, "-");

        addButtonToPanel(p, "0");
        p.add(new JLabel(""));
        addButtonToPanel(p, "Del");
        addButtonToPanel(p, "+");

        Dimension d = p.getPreferredSize();
        System.out.println(
            "Preferred Size: " +
            d.getWidth() +
            "x" +
            d.getHeight());

        JOptionPane.showMessageDialog(null, p);
    }
}

Input/output

prompt> java TestGuiSize
Preferred Size: 113.0x105.0

prompt>java -Dswing.plaf.metal.controlFont=Dialog-22 TestGuiSize
Preferred Size: 169.0x157.0

prompt>java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel TestGuiSize
Preferred Size: 93.0x93.0

prompt>java -Dswing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel TestGuiSize
Preferred Size: 205.0x129.0

prompt> 

Run-time parameters are just the tip of the iceberg of the differences between runs that might sink an application's GUI code. Layouts are designed to handle such differences.

查看更多
▲ chillily
5楼-- · 2020-02-09 09:46

Don't set the preferred size of the button. Use the preferred size of the button and let the layout manager layout the components. The preferred size ensures all the text will be displayed properly on different Look and Feels.

查看更多
放荡不羁爱自由
6楼-- · 2020-02-09 09:48

Probably this because margin of your button is too big.

Try this:

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

You can also turn off border:

button.setBorder(null);
查看更多
登录 后发表回答