如何获得 a 的默认边框JButton
?
一个例子是:
Border border = new JButton().getBorder();
但是我可以在不创建新按钮的情况下做到这一点吗?
您可以从以下位置检索默认边框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();
}
});
}
}