JButton 更改默认边框

JButton change default border

我正在使用 SystemLookAndFeel,它在我的按钮周围设置了默认边框。

现在我想要一个黑色边框,但是当我设置边框时,它只是在默认边框周围添加了我的新边框,所以我有两个。

如何在不移除 LookAndFeel 的情况下更改或移除边框?

另外:我正在使用 java 7 和 Win 8.1

试试这个程序,它有你可以在 Jbutton 中使用的所有类型的边框

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

    public class jbuttonBoders extends JFrame {

        private JButton button[];

        private JPanel panel;

        public jbuttonBoders() {

            super("JButton Border");

            setSize(220,190);

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            button = new JButton[12];

            panel = new JPanel();

            panel.setLayout(new FlowLayout(FlowLayout.CENTER));

            //Constructing all 12 JButtons using "for loop"

            for(int count=0; count<button.length; count++) {

                button[count] = new JButton("Button "+(count+1));

                panel.add(button[count]);

            }



            //Setting Different Borders on each JButton

            button[0].setBorder(BorderFactory.createLineBorder(Color.blue)); // Simple Line Border

            button[1].setBorder(BorderFactory.createLineBorder(Color.red, 5)); // Line Border + Thickness of the Border

            button[2].setBorder(BorderFactory.createBevelBorder(1)); // Inner Bevel Border

            button[3].setBorder(BorderFactory.createBevelBorder(0)); // Outer Bevel Border

            button[4].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.blue)); // Two Colors Inner Bevel

            button[5].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange)); // Two Colors Outer Bevel

            button[6].setBorder(BorderFactory.createBevelBorder(1, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Inner Bevel

            button[7].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Outer Bevel

            button[8].setBorder(BorderFactory.createEmptyBorder(5,10,5,50)); // Empty Border (Upper Space, Left Space, Bottom Space, Right Space)

            button[9].setBorder(BorderFactory.createEtchedBorder(0)); //Raised Border Line

            button[10].setBorder(BorderFactory.createEtchedBorder(1)); //

            button[11].setBorder(BorderFactory.createTitledBorder("My Titled Border")); // Titled Border



            /** The Borders shown above are the basic borders that we commonly used.

             *  There are still lots of Border Styles available so all you have to do is to discover

             *  and have some experiment using all the available borders. I recommend you use JCreator Pro

             *  if want to know more about different border styles and learn how to implement them.

             */



            //Setting up the container ready for the components to be added.

            Container pane = getContentPane();

            setContentPane(pane);



            //Adding the JPanel to the container

            pane.add(panel);



            /**Set all the Components Visible.

             * If it is set to "false", the components in the container will not be visible.

             */

            setVisible(true);

        }



        //Main Method

        public static void main (String[] args) {

            jbuttonBoders jbb = new jbuttonBoders();

        }

    }


    Important Part of the Program: 

    //Setting Different Borders on each JButton

            button[0].setBorder(BorderFactory.createLineBorder(Color.blue)); // Simple Line Border

            button[1].setBorder(BorderFactory.createLineBorder(Color.red, 5)); // Line Border + Thickness of the Border

            button[2].setBorder(BorderFactory.createBevelBorder(1)); // Inner Bevel Border

            button[3].setBorder(BorderFactory.createBevelBorder(0)); // Outer Bevel Border

            button[4].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.blue)); // Two Colors Inner Bevel

            button[5].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange)); // Two Colors Outer Bevel

            button[6].setBorder(BorderFactory.createBevelBorder(1, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Inner Bevel

            button[7].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Outer Bevel

            button[8].setBorder(BorderFactory.createEmptyBorder(5,10,5,50)); // Empty Border (Upper Space, Left Space, Bottom Space, Right Space)

            button[9].setBorder(BorderFactory.createEtchedBorder(0)); //Raised Border Line

            button[10].setBorder(BorderFactory.createEtchedBorder(1)); //

            button[11].setBorder(BorderFactory.createTitledBorder("My Titled Border")); // Titled Border

输出:

与 Java 8 在 Windows 10 上工作,我做了这个小测试

如您所见,我可以开始工作的唯一方法是使用 setContentAreaFilled

普遍的问题是,许多外观不使用 border 属性,而是独立绘制自己的边框(因此是 setBorderPainted 的原因),但是Windows 10 的外观和感觉只是想与众不同

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class ButtonTest {

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

    public ButtonTest() {
        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() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            JButton btn = new JButton("Normal");
            add(btn, gbc);

            btn = new JButton("With border");
            btn.setBorder(new LineBorder(Color.BLACK));
            add(btn, gbc);

            btn = new JButton("borderPainted false");
            btn.setBorderPainted(false);
            btn.setBorder(new LineBorder(Color.BLACK));
            add(btn, gbc);

            btn = new JButton("contentArea false");
            btn.setContentAreaFilled(false);
            btn.setBorder(new LineBorder(Color.BLACK));
            add(btn, gbc);

            btn = new JButton("focusPained false");
            btn.setContentAreaFilled(false);
            btn.setFocusPainted(false);
            btn.setBorder(new LineBorder(Color.BLACK));
            add(btn, gbc);
        }

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

    }

}