//就是这样作的。
//其它地方不用多注意.
//就是加了注释的地方重要import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class LookAndFeel extends JPanel {
static JFrame frame;
//设置外观属性:
static String metal= "Metal";
static String metalClassName = "javax.swing.plaf.metal.MetalLookAndFeel";
//关键 static String motif = "Motif";
//关键
static String motifClassName =
"com.sun.java.swing.plaf.motif.MotifLookAndFeel"; static String windows = "Windows";
//关键
static String windowsClassName =
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; JRadioButton metalButton, motifButton, windowsButton; public LookAndFeel() {
JButton button = new JButton("Hello, world");
button.setMnemonic('h'); metalButton = new JRadioButton(metal);
metalButton.setMnemonic('o');
metalButton.setActionCommand(metalClassName); motifButton = new JRadioButton(motif);
motifButton.setMnemonic('m');
motifButton.setActionCommand(motifClassName); windowsButton = new JRadioButton(windows);
windowsButton.setMnemonic('w');
windowsButton.setActionCommand(windowsClassName);
ButtonGroup group = new ButtonGroup();
group.add(metalButton);
group.add(motifButton);
group.add(windowsButton);
RadioListener myListener = new RadioListener();
metalButton.addActionListener(myListener);
motifButton.addActionListener(myListener);
windowsButton.addActionListener(myListener); add(button);
add(metalButton);
add(motifButton);
add(windowsButton);
}
class RadioListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String lnfName = e.getActionCommand(); try {
//设置外观属性:
UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();
}
catch (Exception exc) {
JRadioButton button = (JRadioButton)e.getSource();
button.setEnabled(false);
updateState();
System.err.println("Could not load LookAndFeel: " + lnfName);
}
}
} public void updateState() {
String lnfName = UIManager.getLookAndFeel().getClass().getName();
if (lnfName.indexOf(metal) >= 0) {
metalButton.setSelected(true);
} else if (lnfName.indexOf(windows) >= 0) {
windowsButton.setSelected(true);
} else if (lnfName.indexOf(motif) >= 0) {
motifButton.setSelected(true);
} else {
System.err.println("SimpleExample is using an unknown L&F: " + lnfName);
}
} public static void main(String s[]) {
LookAndFeel panel = new LookAndFeel();
frame = new JFrame("SimpleExample");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.getContentPane().add("Center", panel);
frame.pack();
frame.setVisible(true);
panel.updateState();
}
}
//其它地方不用多注意.
//就是加了注释的地方重要import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class LookAndFeel extends JPanel {
static JFrame frame;
//设置外观属性:
static String metal= "Metal";
static String metalClassName = "javax.swing.plaf.metal.MetalLookAndFeel";
//关键 static String motif = "Motif";
//关键
static String motifClassName =
"com.sun.java.swing.plaf.motif.MotifLookAndFeel"; static String windows = "Windows";
//关键
static String windowsClassName =
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; JRadioButton metalButton, motifButton, windowsButton; public LookAndFeel() {
JButton button = new JButton("Hello, world");
button.setMnemonic('h'); metalButton = new JRadioButton(metal);
metalButton.setMnemonic('o');
metalButton.setActionCommand(metalClassName); motifButton = new JRadioButton(motif);
motifButton.setMnemonic('m');
motifButton.setActionCommand(motifClassName); windowsButton = new JRadioButton(windows);
windowsButton.setMnemonic('w');
windowsButton.setActionCommand(windowsClassName);
ButtonGroup group = new ButtonGroup();
group.add(metalButton);
group.add(motifButton);
group.add(windowsButton);
RadioListener myListener = new RadioListener();
metalButton.addActionListener(myListener);
motifButton.addActionListener(myListener);
windowsButton.addActionListener(myListener); add(button);
add(metalButton);
add(motifButton);
add(windowsButton);
}
class RadioListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String lnfName = e.getActionCommand(); try {
//设置外观属性:
UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();
}
catch (Exception exc) {
JRadioButton button = (JRadioButton)e.getSource();
button.setEnabled(false);
updateState();
System.err.println("Could not load LookAndFeel: " + lnfName);
}
}
} public void updateState() {
String lnfName = UIManager.getLookAndFeel().getClass().getName();
if (lnfName.indexOf(metal) >= 0) {
metalButton.setSelected(true);
} else if (lnfName.indexOf(windows) >= 0) {
windowsButton.setSelected(true);
} else if (lnfName.indexOf(motif) >= 0) {
motifButton.setSelected(true);
} else {
System.err.println("SimpleExample is using an unknown L&F: " + lnfName);
}
} public static void main(String s[]) {
LookAndFeel panel = new LookAndFeel();
frame = new JFrame("SimpleExample");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.getContentPane().add("Center", panel);
frame.pack();
frame.setVisible(true);
panel.updateState();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货