Java:系统L&;F仅第二次正确显示

Java:系统L&;F仅第二次正确显示,java,swing,user-interface,look-and-feel,Java,Swing,User Interface,Look And Feel,我正在使用此代码设置程序的外观: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) 我只在程序运行时显示的JPanel(在JFrame内)上使用系统外观。我第一次展示JPanel时,L&F是错误的(看起来它适用于旧版本的windows或其他组件,与我以前在其他组件上使用的不同)。我隐藏了JPanel,然后再次打开它,L&F现在是正确的。 遗憾的是,我无法生成一个可复制的示例,但问题仍然存在于原始程序中。 它由多

我正在使用此代码设置程序的外观:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
我只在程序运行时显示的JPanel(在JFrame内)上使用系统外观。我第一次展示JPanel时,L&F是错误的(看起来它适用于旧版本的windows或其他组件,与我以前在其他组件上使用的不同)。我隐藏了JPanel,然后再次打开它,L&F现在是正确的。
遗憾的是,我无法生成一个可复制的示例,但问题仍然存在于原始程序中。
它由多个类组成,并有一个面向对象编写的UI,这是导致问题JPanel的代码:

package almanah;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SeriesProperties extends JPanel {

    JButton button_close = new JButton();
    JPanel container = new JPanel();
    JScrollPane scPane = new JScrollPane(container);

    public SeriesProperties(ItemLib lib) {
        
        try {
            UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SeriesProperties.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(SeriesProperties.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(SeriesProperties.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(SeriesProperties.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        this.setLayout(new BorderLayout());

        this.add(button_close, BorderLayout.EAST);
        button_close.addActionListener((e) -> {
            Almanah.frame.swapToTiles();
        });

        scPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        this.add(scPane, BorderLayout.CENTER);
        
        //container
        container.setBackground(Color.red);
        container.setLayout(new WrapLayout());
        
        for (int i = 0; i < 100; i++) {
            JPanel panel=new JPanel();
            panel.setPreferredSize(new Dimension(100, 100));
            container.add(panel);
        }
        this.updateUI();
    }

}
package-almanah;
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.swing.JButton;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类系列属性扩展了JPanel{
JButton button_close=新JButton();
JPanel容器=新的JPanel();
JScrollPane scPane=新的JScrollPane(容器);
公共系列属性(ItemLib){
试一试{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}捕获(ClassNotFoundException ex){
Logger.getLogger(serieProperties.class.getName()).log(Level.SEVERE,null,ex);
}catch(实例化异常){
Logger.getLogger(serieProperties.class.getName()).log(Level.SEVERE,null,ex);
}捕获(非法访问例外){
Logger.getLogger(serieProperties.class.getName()).log(Level.SEVERE,null,ex);
}捕获(无支持的LookandFeelexception ex){
Logger.getLogger(serieProperties.class.getName()).log(Level.SEVERE,null,ex);
}
此.setLayout(新的BorderLayout());
此.add(按钮关闭,BorderLayout.EAST);
按钮\关闭。添加ActionListener((e)->{
Almanah.frame.swapToTiles();
});
scPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u ALWAYS);
scPane.setHorizontalScrollBarPolicy(JScrollPane.HorizontalScrollBar\uNever);
添加(scPane,BorderLayout.CENTER);
//容器
容器。立根背景(颜色。红色);
setLayout(新的WrapLayout());
对于(int i=0;i<100;i++){
JPanel面板=新的JPanel();
面板。设置首选尺寸(新尺寸(100100));
容器。添加(面板);
}
这个。updateUI();
}
}

在创建任何UI元素之前,先建立外观状态。在运行时切换L&F的能力实际上是一个副作用,通常不鼓励这样做,因为这从来不是系统的一个功能

相反,可能在
main
方法中,设置外观,然后在其之后构建UI

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                JFrame frame = new JFrame();
                // I don't know what ItemLib, as the source is incomplete
                frame.add(new SeriesProperties(...));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class SeriesProperties extends JPanel {

        JButton button_close = new JButton();
        JPanel container = new JPanel();
        JScrollPane scPane = new JScrollPane(container);

        public SeriesProperties(ItemLib lib) {

            this.setLayout(new BorderLayout());

            this.add(button_close, BorderLayout.EAST);
            button_close.addActionListener((e) -> {
                // This seems like a bad idea
                // You should consider using a observer/delegate
                // pattern instead, reducing the coupling of your code
                Almanah.frame.swapToTiles();
            });

            scPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            scPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            this.add(scPane, BorderLayout.CENTER);

            //container
            container.setBackground(Color.red);
            container.setLayout(new WrapLayout());

            for (int i = 0; i < 100; i++) {
                JPanel panel = new JPanel();
                panel.setPreferredSize(new Dimension(100, 100));
                container.add(panel);
            }
            //this.updateUI();
        }

    }
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公开课考试{
公共静态void main(字符串[]args){
新测试();
}
公开考试(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JFrame=新JFrame();
//我不知道ItemLib是什么,因为源代码不完整
添加(新系列属性(…);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类系列属性扩展了JPanel{
JButton button_close=新JButton();
JPanel容器=新的JPanel();
JScrollPane scPane=新的JScrollPane(容器);
公共系列属性(ItemLib){
此.setLayout(新的BorderLayout());
此.add(按钮关闭,BorderLayout.EAST);
按钮\关闭。添加ActionListener((e)->{
//这似乎是个坏主意
你应该考虑使用观察员/代表。
//模式,从而减少代码的耦合
Almanah.frame.swapToTiles();
});
scPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u ALWAYS);
scPane.setHorizontalScrollBarPolicy(JScrollPane.HorizontalScrollBar\uNever);
添加(scPane,BorderLayout.CENTER);
//容器
容器。立根背景(颜色。红色);
setLayout(新的WrapLayout());
对于(int i=0;i<100;i++){
JPanel面板=新的JPanel();
面板。设置首选尺寸(新尺寸(100100));
容器。添加(面板);
}
//这个。updateUI();
}
}
}

在创建任何UI元素之前,先建立外观状态。在运行时切换L&F的能力实际上是一个副作用,通常不鼓励这样做,因为这从来不是系统的一个功能

相反,可能在
main
方法中,设置外观,然后在其之后构建UI

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                JFrame frame = new JFrame();
                // I don't know what ItemLib, as the source is incomplete
                frame.add(new SeriesProperties(...));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class SeriesProperties extends JPanel {

        JButton button_close = new JButton();
        JPanel container = new JPanel();
        JScrollPane scPane = new JScrollPane(container);

        public SeriesProperties(ItemLib lib) {

            this.setLayout(new BorderLayout());

            this.add(button_close, BorderLayout.EAST);
            button_close.addActionListener((e) -> {
                // This seems like a bad idea
                // You should consider using a observer/delegate
                // pattern instead, reducing the coupling of your code
                Almanah.frame.swapToTiles();
            });

            scPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            scPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            this.add(scPane, BorderLayout.CENTER);

            //container
            container.setBackground(Color.red);
            container.setLayout(new WrapLayout());

            for (int i = 0; i < 100; i++) {
                JPanel panel = new JPanel();
                panel.setPreferredSize(new Dimension(100, 100));
                container.add(panel);
            }
            //this.updateUI();
        }

    }
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
感应电动机