我做了个小查询程序,现在遇到一个问题,用UIManager给这个界面设置外观,直接固定的写在main方法里,或者写到jframe的构造方法里,是可以的,但是我做成按钮,给按钮添加actionlistener,改变UIManager后面括号里德值就不好用了,没有效果,我很纳闷,请高手指教,下面是我写的一小段示例代码,和我的程序差不多,复制到ecplise里直接可以运行。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;import javax.swing.*; import ccpsic.pre_test7;public class TestSQL{
public static void main(String argv[])
{
final JFrame frame = new JFrame( "只能运行一个 ");
try { UIManager
.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");// Nimbus风格,新出来的外观,jdk6
} catch (Exception e) {
e.printStackTrace();
}
frame.setBounds(100,100,200,200);
JButton jb1=new JButton("当前系统风格");
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.dispose();
frame.setBounds(200,200,300,300);
//frame.setVisible(true);
try {
UIManager
.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());// 当前系统风格
//frame.dispose();
frame.repaint();
frame.setVisible(true);
} catch (Exception e_myUIManager) {
e_myUIManager.printStackTrace();
}
}
});
JButton jb2=new JButton("Motif风格");
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.dispose();
frame.setBounds(200,200,300,300);
//frame.setVisible(true);
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");// Motif风格,外观接近windows经典,但宽宽大大,而且不是黑灰主色,而是蓝黑
//frame.dispose();
frame.repaint();
frame.setVisible(true);
} catch (Exception e_myUIManager) {
e_myUIManager.printStackTrace();
}
}
});
JButton jb3=new JButton("windows风格");
jb3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.dispose();
frame.setBounds(200,200,300,300);
//frame.setVisible(true);
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");// windows风格
// update10版本以后的才会出现
//frame.dispose();
frame.repaint();
frame.setVisible(true);
} catch (Exception e_myUIManager) {
e_myUIManager.printStackTrace();
}
}
});
JButton jb4=new JButton("java风格");
jb4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.dispose();
frame.setBounds(200,200,300,300);
//frame.setVisible(true);
try {
UIManager
.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");// java风格
// update10版本以后的才会出现
//frame.dispose();
frame.repaint();
frame.setVisible(true);
} catch (Exception e_myUIManager) {
e_myUIManager.printStackTrace();
}
}
});
JPanel panel1=new JPanel();
panel1.add(jb1);
panel1.add(jb2);
panel1.add(jb3);
panel1.add(jb4);
frame.add(panel1);
frame.setVisible(true);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;import javax.swing.*; import ccpsic.pre_test7;public class TestSQL{
public static void main(String argv[])
{
final JFrame frame = new JFrame( "只能运行一个 ");
try { UIManager
.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");// Nimbus风格,新出来的外观,jdk6
} catch (Exception e) {
e.printStackTrace();
}
frame.setBounds(100,100,200,200);
JButton jb1=new JButton("当前系统风格");
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.dispose();
frame.setBounds(200,200,300,300);
//frame.setVisible(true);
try {
UIManager
.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());// 当前系统风格
//frame.dispose();
frame.repaint();
frame.setVisible(true);
} catch (Exception e_myUIManager) {
e_myUIManager.printStackTrace();
}
}
});
JButton jb2=new JButton("Motif风格");
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.dispose();
frame.setBounds(200,200,300,300);
//frame.setVisible(true);
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");// Motif风格,外观接近windows经典,但宽宽大大,而且不是黑灰主色,而是蓝黑
//frame.dispose();
frame.repaint();
frame.setVisible(true);
} catch (Exception e_myUIManager) {
e_myUIManager.printStackTrace();
}
}
});
JButton jb3=new JButton("windows风格");
jb3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.dispose();
frame.setBounds(200,200,300,300);
//frame.setVisible(true);
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");// windows风格
// update10版本以后的才会出现
//frame.dispose();
frame.repaint();
frame.setVisible(true);
} catch (Exception e_myUIManager) {
e_myUIManager.printStackTrace();
}
}
});
JButton jb4=new JButton("java风格");
jb4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.dispose();
frame.setBounds(200,200,300,300);
//frame.setVisible(true);
try {
UIManager
.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");// java风格
// update10版本以后的才会出现
//frame.dispose();
frame.repaint();
frame.setVisible(true);
} catch (Exception e_myUIManager) {
e_myUIManager.printStackTrace();
}
}
});
JPanel panel1=new JPanel();
panel1.add(jb1);
panel1.add(jb2);
panel1.add(jb3);
panel1.add(jb4);
frame.add(panel1);
frame.setVisible(true);
}
}
ublic void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.dispose();
frame.setBounds(200,200,300,300);
//frame.setVisible(true);
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");// windows风格
// update10版本以后的才会出现
//frame.dispose();
//////////////////////////////////////////增加
SwingUtilities.updateComponentTreeUI(frame);
/////////////////////////////////////////
frame.repaint();
frame.setVisible(true);
} catch (Exception e_myUIManager) {
e_myUIManager.printStackTrace();
}
}
});