如何获取JButton
的默认边框?
一个例子是:
Border border = new JButton().getBorder();
但是我可以不创建新按钮吗?
答案 0 :(得分:7)
您可以从UIManager
找回默认边框:
UIManager.getBorder("Button.border");
例如:
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();
}
});
}
}