Java 如何使JFrame看起来和感觉到
当我通过Java 如何使JFrame看起来和感觉到,java,swing,look-and-feel,Java,Swing,Look And Feel,当我通过UIManager.setLookAndFeel设置外观时,框架的内容会按预期更改其外观。例如 public static void main(String[] args) throws Exception { UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); JFrame frame = new JFrame(); Container contentPane =
UIManager.setLookAndFeel
设置外观时,框架的内容会按预期更改其外观。例如
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(new JButton("Some Button"));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(200, 80);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
但框架仍然使用操作系统默认设置(在我的例子中是Windows)绘制
如何使框架具有外观和感觉,使其看起来像光环的外观和感觉?
通常,外观是使用实现的,但a不是JComponent
,因此我无法实现自己的组件ui
1.解决方案
我的第一个想法是使用一个未修饰的JFrame
,它的主要组件是
public class LAFAwareJFrame extends JFrame {
private JInternalFrame lafFrame = new JInternalFrame("", true, true, true, true);
private JDesktopPane desktopPane = new JDesktopPane();
public LAFAwareJFrame() {
super.setUndecorated(true);
desktopPane.add(lafFrame);
lafFrame.setVisible(true);
Container contentPane = super.getContentPane();
contentPane.add(desktopPane);
}
@Override
public void setUndecorated(boolean undecorated) {
throw new UnsupportedOperationException("Can't change the undecorated property for a LAFAwareJFrame");
}
@Override
public void setSize(int width, int height) {
super.setSize(width, height);
lafFrame.setSize(width, height);
}
@Override
public Container getContentPane() {
return lafFrame.getContentPane();
}
@Override
public void setTitle(String title) {
lafFrame.setTitle(title);
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
JFrame frame = new LAFAwareJFrame();
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(new JButton("Some Button"));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(200, 80);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
但是我还有很多事情要做,比如事件处理和委托。例如,当JInternalFrame
被移动时,我真的不想移动内部帧。相反,我想移动未装饰的框架
2.解决方案
仅将JInternalFrame
用作渲染器。类似于ListCellRender
由于所有的解决方案都需要大量的工作,我想首先问你是否有更好的解决方案。例如,一个库,或者可能已经可以使用标准Java,但我错过了一些东西 编辑
我尝试过使用,但它不适用于nimbus,也不适用于motif。您尝试过更改吗?检查了Windows JRE上安装的所有
LookAndFeel
。只有Metal LookAndFeel
支持windows平台上的装饰。在Mac OS X上,setWindowDecorationStyle()
可与com.apple.laf.AquaLookAndFeel
和javax.swing.plaf.Metal.MetalLookAndFeel
一起使用;更多]这里]()。