为按钮添加边框使其更小

时间:2018-08-23 18:02:58

标签: java swing border jbutton

当我在按钮上添加边框时,按钮会缩小。我正在使用以下行添加边框:

jButton.setBorder(BorderFactory.createLineBorder(Color.RED));

这是我的无边框按钮:

Button without border

这是带有边框的按钮:

enter image description here

3 个答案:

答案 0 :(得分:2)

如果要添加边框,并且还希望在标签文本和边框的边缘之间留一个边距,请创建一个复合边框,内部有一个空边框(边框的大小合适),然后执行未设置首选尺寸。这样,无论标签的文本和字体如何,按钮都可以自行调整大小。例如:

LineBorder border1 = new LineBorder(Color.red);
EmptyBorder border2 = new EmptyBorder(1,5,1,5);
Border newBorder = BorderFactory.createCompoundBorder(border1, border2);

答案 1 :(得分:1)

一个简单的肮脏例子,说明为什么setPreferredSize是一个糟糕的主意,以及为什么应该尽可能避免使用它。

您也可以查看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?,以获取有关该主题的更多讨论

Normal Exaggerated

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            JButton goodButton = new JButton("Location 1");
            Insets insets = goodButton.getInsets();
            goodButton.setBorder(new CompoundBorder(
                            new LineBorder(Color.RED),
                            new EmptyBorder(insets)));
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.insets = new Insets(8, 8, 8, 8);
            add(goodButton, gbc);

            JButton badButton = new JButton("Location 2");
            badButton.setPreferredSize(new Dimension(110, 29));
            badButton.setBorder(new LineBorder(Color.RED));
            add(badButton, gbc);

            ActionListener listener = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    goodButton.setText(random());
                    badButton.setText(random());

                    float size = rnd.nextInt(48) + 12;

                    Font f = goodButton.getFont().deriveFont(size);
                    goodButton.setFont(f);
                    badButton.setFont(f);
                }
            };

            goodButton.addActionListener(listener);
            badButton.addActionListener(listener);
        }

        private Random rnd = new Random();

        protected String random() {
            int length = rnd.nextInt(15) + 5;
            StringBuilder sb = new StringBuilder(length);
            for (int index = 0; index < length; index++) {
                sb.append((char)('A' + rnd.nextInt(26)));
            }
            return sb.toString();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 600);
        }

    }


}

这是一个夸张的示例,但不幸的是,我继承了一个项目,其中以前的开发人员使用setPreferredSize,并且在运行相同操作系统的同一台PC上运行正常时,数量众多,看似随机的问题,甚至还有更多,甚至让我不了解它在MacOS上所做的事情。

  

但是我只为Windows开发

大笔交易,正如我所说,在某些Windows PC上,我们的应用程序运行正常,但是一旦有人拥有自定义DPI或字体指标,或者将其升级到新版Windows,它便开始崩溃-简短答案-不要使用setPreferredSize

答案 2 :(得分:0)

根据需要定义宽度和高度,并实现以下代码行:

jButton.setPreferredSize(new Dimension(width, height));