void main(String[] s)
{
MyFrame frame = new MyFrame(); //MyFrame is extended from JFrame
frame.getMetalItem().addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
MetalLookAndFeel m = new MetalLookAndFeel();
try {
UIManager.setLookAndFeel(m);
} catch (UnsupportedLookAndFeelException a) {
a.printStackTrace();
}
}});
}
点了MetalItem的菜单选项后没反应,要手动改变一下frame的大小才可以看到效果(就是点窗口右上角的最大化或者拉一下窗口大小).
有没有什么办法解决? update()? repaint()?
貌似UIManger的setLookAndFeel()要在Frame的构造函数前调用才有效,不知道和这个有没有关系的......
{
MyFrame frame = new MyFrame(); //MyFrame is extended from JFrame
frame.getMetalItem().addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
MetalLookAndFeel m = new MetalLookAndFeel();
try {
UIManager.setLookAndFeel(m);
} catch (UnsupportedLookAndFeelException a) {
a.printStackTrace();
}
}});
}
点了MetalItem的菜单选项后没反应,要手动改变一下frame的大小才可以看到效果(就是点窗口右上角的最大化或者拉一下窗口大小).
有没有什么办法解决? update()? repaint()?
貌似UIManger的setLookAndFeel()要在Frame的构造函数前调用才有效,不知道和这个有没有关系的......
import java.awt.event.*;
import javax.swing.*;public class Test
{
public static void main(String[] args)
{
TestFrame frame=new TestFrame();
frame.show();
}
}class TestFrame extends JFrame
{
private JMenuBar mb;
private JMenu fileMenu_1;
private JMenuItem itemMenu_Metal;
private JMenuItem itemMenu_Motif;
private JMenuItem itemMenu_Windows;
private JMenuItem itemMenu_Exit;
public TestFrame()
{
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension size=kit.getScreenSize();
int width=size.width;
int height=size.height;
setSize(width/4,height/4);
setTitle("Test");
setLocation(width/4,height/4);
mb=new JMenuBar();
fileMenu_1=new JMenu("查看");
itemMenu_Metal=new JMenuItem("Metal");
itemMenu_Motif=new JMenuItem("Motif");
itemMenu_Windows=new JMenuItem("Windows");
itemMenu_Exit=new JMenuItem("Exit");
fileMenu_1.add(itemMenu_Metal);
fileMenu_1.add(itemMenu_Motif);
fileMenu_1.add(itemMenu_Windows);
fileMenu_1.addSeparator();
fileMenu_1.add(itemMenu_Exit); mb.add(fileMenu_1);
setJMenuBar(mb);
itemMenu_Metal.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(TestFrame.this);
}
catch(Exception e){e.printStackTrace();}
}
});
itemMenu_Motif.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(TestFrame.this);
}
catch(Exception e){e.printStackTrace();}
}
}); itemMenu_Windows.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(TestFrame.this);
}
catch(Exception e){e.printStackTrace();}
}
});
itemMenu_Exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
}
}
anyway, thanks a lot indeed
你可以试下
Thank you, to hujiaboy.