Java Swing JTabbedPane中的外观未更新
我已经用JavaSwing创建了一个应用程序。我提供了从菜单更改应用程序外观的选项,但在Java Swing JTabbedPane中的外观未更新,java,swing,look-and-feel,Java,Swing,Look And Feel,我已经用JavaSwing创建了一个应用程序。我提供了从菜单更改应用程序外观的选项,但在JTabbedPane中添加新选项卡后,它不会得到新外观的更新 我已经使用了以下代码: Window windows[] = Frame.getWindows(); for(Window window : windows) { SwingUtilities.updateComponentTreeUI(window); } 利用@Andrew's和这个旧的,它似乎对我有用 import java.aw
JTabbedPane
中添加新选项卡后,它不会得到新外观的更新
我已经使用了以下代码:
Window windows[] = Frame.getWindows();
for(Window window : windows) {
SwingUtilities.updateComponentTreeUI(window);
}
利用@Andrew's和这个旧的,它似乎对我有用
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
/**
* @see https://stackoverflow.com/a/11949899/230513
* @see https://stackoverflow.com/a/5773956/230513
*/
public class JTabbedText {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
private final JTabbedPane jtp = new JTabbedPane();
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtp.addTab("Model", createPanel());
jtp.addTab("View", createPanel());
jtp.addTab("Control", createPanel());
f.add(createToolBar(f), BorderLayout.NORTH);
f.add(jtp, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
private static JToolBar createToolBar(final Component parent) {
final UIManager.LookAndFeelInfo[] available =
UIManager.getInstalledLookAndFeels();
List<String> names = new ArrayList<String>();
for (LookAndFeelInfo info : available) {
names.add(info.getName());
}
final JComboBox combo = new JComboBox(names.toArray());
String current = UIManager.getLookAndFeel().getName();
combo.setSelectedItem(current);
combo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
int index = combo.getSelectedIndex();
try {
UIManager.setLookAndFeel(
available[index].getClassName());
SwingUtilities.updateComponentTreeUI(parent);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
});
JToolBar bar = new JToolBar("L&F");
bar.add(combo);
return bar;
}
private static Box createPanel() {
Box panel = new Box(BoxLayout.X_AXIS);
JLabel label = new JLabel("Code: ", JLabel.LEFT);
label.setAlignmentY(JLabel.TOP_ALIGNMENT);
JTextArea text = new JTextArea(4, 16);
text.setAlignmentY(JTextField.TOP_ALIGNMENT);
text.append("#" + panel.hashCode());
text.append("\n#" + label.hashCode());
text.append("\n#" + label.hashCode());
panel.add(label);
panel.add(text);
return panel;
}
}
导入java.awt.BorderLayout;
导入java.awt.Component;
导入java.awt.EventQueue;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.Box;
导入javax.swing.BoxLayout;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JTabbedPane;
导入javax.swing.JTextArea;
导入javax.swing.JTextField;
导入javax.swing.JToolBar;
导入javax.swing.SwingUtilities;
导入javax.swing.UIManager;
导入javax.swing.UIManager.LookAndFeelInfo;
/**
*@见https://stackoverflow.com/a/11949899/230513
*@见https://stackoverflow.com/a/5773956/230513
*/
公共类JTabbedText{
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
专用最终JTabbedPane jtp=新JTabbedPane();
@凌驾
公开募捐{
JFrame f=新的JFrame();
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addTab(“Model”,createPanel());
addTab(“视图”,createPanel());
addTab(“Control”,createPanel());
f、 添加(createToolBar(f),BorderLayout.NORTH);
f、 添加(jtp,BorderLayout.CENTER);
f、 包装();
f、 setLocationRelativeTo(空);
f、 setVisible(真);
}
});
}
专用静态JToolBar createToolBar(最终组件父级){
最终UIManager.LookAndFeelInfo[]可用=
UIManager.getInstalledLookAndFeels();
列表名称=新的ArrayList();
对于(LookAndFeelInfo信息:可用){
name.add(info.getName());
}
最终JComboBox组合=新的JComboBox(names.toArray());
字符串current=UIManager.getLookAndFeel().getName();
组合设置选择项(当前);
combo.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效行动(行动事件ae){
int index=combo.getSelectedIndex();
试一试{
UIManager.setLookAndFeel(
可用[index].getClassName());
SwingUtilities.updateComponentTreeUI(父级);
}捕获(例外e){
e、 printStackTrace(System.err);
}
}
});
JToolBar=新的JToolBar(“L&F”);
添加(组合);
返回杆;
}
专用静态框createPanel(){
盒子面板=新盒子(盒子布局X_轴);
JLabel标签=新的JLabel(“代码:”,JLabel.LEFT);
标签.设置对齐(JLabel.顶部对齐);
JTextArea text=新的JTextArea(4,16);
text.setAlignmentY(JTextField.TOP_对齐);
text.append(“#”+panel.hashCode());
text.append(“\n#”+label.hashCode());
text.append(“\n#”+label.hashCode());
面板。添加(标签);
添加(文本);
返回面板;
}
}
为了更快地获得更好的帮助,请发布一个。您可以向我们展示更多的代码吗,也许是添加Jtabbedpane的代码?您是否设置了新的外观和感觉?听起来很明显,但有时明显的东西会丢失:DUIManager.setLookAndFeel(lnfName)代码>你通过了吗