1
我目前正在开发一个允许用户选择外观和感觉的项目。 但是,当用户选择另一个外观并将其更改回原始CrossPlatformLookAndFeel时,按钮的边框消失。更改外观后,是否可以保留按钮的边框? (Java Swing)
代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class SSCCE {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JFrame frame = new JFrame();
final JButton button = new JButton("Button");
button.setBorder(LineBorder.createBlackLineBorder());
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(frame);
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(frame);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
});
//
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
正如你看到的,你点击该按钮后,边框消失。
所以问题是:在改变外观和感觉后边界可以保留吗?我知道边框不会出现在WindowsLookAndFeel中,但是在外观和感觉恢复为默认的外观之后是否可以“重新出现”?