java 获取 JButton 的默认边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13055120/
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-31 11:20:57  来源:igfitidea点击:

Get default border of JButton

javaswingborderjbutton

提问by Zulatin

How can I get the default border of a JButton?

如何获得 a 的默认边框JButton

An example is:

一个例子是:

Border border = new JButton().getBorder();

But can I do it without creating a new button?

但是我可以在不创建新按钮的情况下做到这一点吗?

回答by tenorsax

You can retrieve the default border from UIManager:

您可以从UIManager以下位置检索默认边框:

UIManager.getBorder("Button.border");

For example:

例如:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class TestButton {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("TestButton");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button = new JButton("Click");
        button.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ((JButton)e.getSource()).setBorder(UIManager.getBorder("Button.border"));
            }
        });

        frame.add(button);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}