java - 如何使一个类中的更改在另一个类中生效?
问题描述
我正在用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);
}
}
});
}
解决方案
这是一个如何实现的示例:将调用窗口传递给您的对话框,并使用更新它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);
}
}
推荐阅读
- c# - Overlapped.AsyncResult 即使在操作成功完成后也不会改变
- c - 转换为字符串的运算符
- android - 为什么这些随机标记出现在每个 recyclerview 和 viewpager 项目中?
- orientdb - orientdb oetl 错误/我想要 pokec 数据
- python-3.x - 具有 FIFO 的 Python 选择器运行到无限循环
- vue.js - 如何清理所有缓存数据?
- fpdi - setasign\Fpdi\FpdfTplTrait::setPageFormat(array $size, string $orientation) Laravel 5.4 和 php 7 的声明
- powershell - 如果 Else Loop 在 Powershell 清单脚本中不起作用
- python - 海上联合图上的 set_title
- c++ - 查找生成给定数字所需的最小位数