我目前正在从事一个允许用户选择外观的项目。
但是,当用户选择其他外观并将其更改回原始的CrossPlatformLookAndFeel时,按钮的边框会消失。

码:

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中,但是在将外观更改为默认边框后是否有可能“重新出现”?

最佳答案

上次我检查时,PLAF中存在多种错误,这些错误导致了这类奇怪的行为。尤其是从MetaL LAF更改时(但良好的结合也与雨云有关)。

取得应用程式的唯一可靠方法。更改PLAF是:


序列化用户对新PLAF的选择(即作为完全限定的类名称的String)。
启动一个调用main(String[])的新进程,该进程将检查并使用要使用的新PLAF的序列化字符串。
(可能将当前GUI的状态传递给新的GUI。)
关闭当前的GUI。


如您所见,获得“绝对可靠的” PLAF更改非常麻烦。

07-26 09:27
查看更多