java使用三个外观 我写后显示不出来 这是什么情况?麻烦帮我看一下什么问题?
meta=newJRadioButtonMenuItem("Meta风格",true);motif=newJRadioButtonMenuItem("Motif风格");...
meta = new JRadioButtonMenuItem("Meta风格", true);
motif = new JRadioButtonMenuItem("Motif风格");
windows = new JRadioButtonMenuItem("Windows风格");
meta.addActionListener(new MetaListener());
motif.addActionListener(new MotifListener());
windows.addActionListener(new WindowsListener());
public class MetaListener implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
try {// 设置窗体外观
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
}
} 展开
motif = new JRadioButtonMenuItem("Motif风格");
windows = new JRadioButtonMenuItem("Windows风格");
meta.addActionListener(new MetaListener());
motif.addActionListener(new MotifListener());
windows.addActionListener(new WindowsListener());
public class MetaListener implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
try {// 设置窗体外观
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
}
} 展开
1个回答
2014-12-26
展开全部
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LittleWhite extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
private static final String PLASTIC3D = "com.jgoodies.looks.plastic.Plastic3DLookAndFeel";
private static final String PLASTIC = "com.jgoodies.looks.plastic.PlasticLookAndFeel";
private static final String PLASTXP = "com.jgoodies.looks.plastic.PlasticXPLookAndFeel";
private Vector<SupportedLaF> lafVector = new Vector<SupportedLaF> ();
public LittleWhite ()
{
UIManager.installLookAndFeel ("Plastic3D", PLASTIC3D);
UIManager.installLookAndFeel ("Plastic", PLASTIC);
UIManager.installLookAndFeel ("PlasticXP", PLASTXP);
UIManager.LookAndFeelInfo[] installedLafs = UIManager.getInstalledLookAndFeels ();
for ( UIManager.LookAndFeelInfo lafInfo : installedLafs )
{
try
{
Class<?> lnfClass = Class.forName (lafInfo.getClassName ());
LookAndFeel laf = (LookAndFeel) ( lnfClass.newInstance () );
if (laf.isSupportedLookAndFeel ())
{
String name = lafInfo.getName ();
lafVector.add (new SupportedLaF (name, laf));
}
}
catch (Exception e)
{
continue;
}
}
}
private class SupportedLaF
{
String name;
LookAndFeel laf;
SupportedLaF ( String name, LookAndFeel laf )
{
this.name = name;
this.laf = laf;
}
public String toString ()
{
return name;
}
}
private void addComponents ()
{
JPanel panel = new JPanel ();
panel.setLayout (new BoxLayout (panel, BoxLayout.Y_AXIS));
ButtonGroup bg = new ButtonGroup ();
for ( int i = 0; i < lafVector.size (); i++ )
{
JRadioButton r = new JRadioButton (lafVector.get (i).toString ());
r.setName (i + "");
r.addActionListener (this);
bg.add (r);
panel.add (r);
}
add (panel);
}
public static void main ( String[] args )
{
LittleWhite littleWhite = new LittleWhite ();
littleWhite.setTitle ("LittleWhite");
littleWhite.setSize (400, 300);
littleWhite.setLocationRelativeTo (null);
littleWhite.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
littleWhite.setLayout (new FlowLayout (FlowLayout.LEFT));
littleWhite.addComponents ();
littleWhite.setVisible (true);
}
@Override
public void actionPerformed ( ActionEvent e )
{
JRadioButton target = (JRadioButton) e.getSource ();
try
{
UIManager.setLookAndFeel (lafVector.get (Integer.parseInt (target.getName ())).laf);
SwingUtilities.updateComponentTreeUI (this);
}
catch (UnsupportedLookAndFeelException exc)
{
System.err.println (exc.toString ());
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询