简体   繁体   English

JButton更改默认边框

[英]JButton change default border

I'm using the SystemLookAndFeel which makes the default border around my buttons. 我正在使用SystemLookAndFeel,它使按钮周围具有默认边框。

Now I would like a ticker black border, but when I set the border it just adds my new border around the default one, so I have two. 现在我想要一个黑色的边框,但是当我设置边框时,它只是在默认边框周围添加了新边框,所以我有两个边框。

How can I change or remove the border without removing the LookAndFeel? 如何在不删除LookAndFeel的情况下更改或删除边框?

Also: I am using java 7 and Win 8.1 另外:我正在使用Java 7和Win 8.1

Work with Java 8 on Windows 10, I did this little test 在Windows 10上使用Java 8,我做了这个小测试

很多按钮

As you can see, about the only method I can get to work is using setContentAreaFilled 如您所见,关于我上班的唯一方法是使用setContentAreaFilled

The general problem is, many look and feels don't use the border property, but instead paint their own borders independently (hence the reason for setBorderPainted ), but the look and feel for Windows 10 just wants to be different 通常的问题是,许多外观不使用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);
        }

    }

}

Try this program it has all types of borders you can have in a Jbutton 试试这个程序,它具有您可以在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

Output: 输出:

在此处输入图片说明

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

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