首页 > 解决方案 > 如何使一个类中的更改在另一个类中生效?

问题描述

我正在用java设计下载管理器的GUI,在程序的主框架(这是一个JFrame)中有几个按钮,其中一个是打开到JDialog的设置按钮,在设置部分用户可以选择几个默认外观和感觉(如钢、Nimbus 等)。
问题是,我想对设置部分和大型机应用外观和感觉的更改,但由于设置部分是不同的框架和类,更改仅在设置框架中生效,我不知道如何进行此更改影响主面板。
代码(设置类的一部分):

UIManager.LookAndFeelInfo[] LAFInfo = UIManager.getInstalledLookAndFeels();
String [] LookAndFeels = new String[LAFInfo.length];
for (int i = 0; i < LAFInfo.length; i++) {
    LookAndFeels [i] =  LAFInfo[i].getName();
    JComboBox<String> lookAndFeelChoices = new JComboBox<>(LookAndFeels);
    lookAndFeelChoices.setBounds(200,220,200,30);
    settings.add(lookAndFeelChoices);
    lookAndFeelChoices.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            try {
                UIManager.setLookAndFeel(LAFInfo[lookAndFeelChoices.getSelectedIndex()].getClassName());
                SwingUtilities.updateComponentTreeUI(Settings.super.getContentPane())
            } catch (Exception ei) {
                System.out.println(ei);
            }
        }
    });
}

标签: javaclassuser-interface

解决方案


这是一个如何实现的示例:将调用窗口传递给您的对话框,并使用更新它SwingUtilities.updateComponentTreeUI

public class Main {
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton open=new JButton("Open Look and feel chooser");
        open.addActionListener(e -> {
            new LookAndFeelDialog(frame);
        });
        frame.setLayout(new FlowLayout());
        frame.getContentPane().add(open);
        frame.setLocationRelativeTo(null);
        frame.setPreferredSize(new Dimension(400, 100));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

和 JDialog

class LookAndFeelDialog extends JDialog {


    public LookAndFeelDialog(Frame callingFrame) {
        super(callingFrame, "Pick a look and feel");
        setPreferredSize(new Dimension(300,100));

        UIManager.LookAndFeelInfo[] LAFInfo = UIManager.getInstalledLookAndFeels();
        String [] LookAndFeels = new String[LAFInfo.length];
        for (int i = 0; i < LAFInfo.length; i++) {
            LookAndFeels [i] =  LAFInfo[i].getName();
            JComboBox<String> lookAndFeelChoices = new JComboBox<>(LookAndFeels);
            add(lookAndFeelChoices);

            lookAndFeelChoices.addItemListener(e -> {
                try {
                    UIManager.setLookAndFeel(LAFInfo[lookAndFeelChoices.getSelectedIndex()].getClassName());
                    // update current window
                    SwingUtilities.updateComponentTreeUI(getContentPane());
                    // update calling frame
                    SwingUtilities.updateComponentTreeUI(callingFrame);
                } catch (Exception ei) {
                    ei.printStackTrace();
                }
            });
        }

        setLocationRelativeTo(callingFrame);
        pack();
        setVisible(true);
    }

}

推荐阅读