java 从 JButton 中删除三个点“...”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5808195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:53:03  来源:igfitidea点击:

Removing the three dots "..." from a JButton?

javaswingjbutton

提问by Stan

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?

嘿,我正在创建一个带有一些小按钮的计算器程序,我希望其中一个按钮上带有“Ans”,但是每当我将 JButton 设置为小于 50、50 时,它都会显示三个点。“...”,我怎样才能删除这些点并显示给定的正常文本?

回答by lukastymo

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

回答by camickr

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.

不要设置按钮的首选大小。使用按钮的首选大小并让布局管理器布局组件。首选大小可确保所有文本都能在不同的外观和感觉上正确显示。

回答by Andrew Thompson

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

这段代码试图解释为什么布局和首选尺寸如此重要。重要的部分在于输入/输出。

TestGuiSize.java

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.

运行时参数只是可能影响应用程序 GUI 代码的运行之间差异的冰山一角。布局旨在处理此类差异。

回答by truhanen

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

使用setMargin(Insets m)调整JButton的边框和标签之间的空间。默认为(2, 14, 2, 14)。为了最大化标签的可用空间(并完全删除点),您可以使用类似

myButton.setFont(new Font("Tahoma", Font.BOLD, 11));
myButton.setMargin(new Insets(0, -1, 0, -20));
myButton.setHorizontalAlignment(SwingConstants.LEFT);

回答by Entity

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)

增加字体大小(可以轻松更改以减小字体大小)