发布网友 发布时间:2022-05-16 01:58
共4个回答
热心网友 时间:2023-10-08 22:57
public static void main(String[] args) {
try {
//设置UI为当前系统的UI
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
//new AssetLogin().validate();
}
//LookAndFeelFrame.java
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class LookAndFeelFrame extends JFrame{
/**
* 可插入外观
*/
private static final long serialVersionUID = 1L;
private UIManager.LookAndFeelInfo[] looks;
private String[] lookNames;
private JRadioButton[] radio;
private ButtonGroup group;
private JLabel label;
private JButton button;
private JComboBox comboBox;
public LookAndFeelFrame(){
super("Look and Feel Demo");
//获得系统中可用外观
looks=UIManager.getInstalledLookAndFeels();
lookNames=new String[looks.length];
for(int i=0;i<looks.length;i++){
lookNames[i]=looks[i].getName();
}
JPanel northPanel=new JPanel();
northPanel.setLayout(new GridLayout(3,1,0,5));
label=new JLabel("This is a"+lookNames[0]+" look-and-feel",SwingConstants.CENTER);
northPanel.add(label);
button=new JButton("JButton");
northPanel.add(button);
comboBox=new JComboBox(lookNames);
northPanel.add(comboBox);
radio=new JRadioButton[looks.length];
JPanel southPanel=new JPanel();
int rows=(int)Math.ceil(radio.length/3.0);
southPanel.setLayout(new GridLayout(rows,3));
group=new ButtonGroup();
ItemHandler handler=new ItemHandler();
for(int count=0;count<radio.length;count++){
radio[count]=new JRadioButton(lookNames[count]);
radio[count].addItemListener(handler);
group.add(radio[count]);
southPanel.add(radio[count]);
}
comboBox.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent event){
if(event.getStateChange()==ItemEvent.SELECTED){
label.setText(String.format("This is a %s look-and- feel",lookNames[comboBox.getSelectedIndex()]));
changeTheLookAndFeel(comboBox.getSelectedIndex());
radio[comboBox.getSelectedIndex()].setSelected(true);
}
}
});
button.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(LookAndFeelFrame.this, "How are you?\n");
}
});
add(northPanel,BorderLayout.NORTH);
add(southPanel,BorderLayout.SOUTH);
radio[0].setSelected(true);
}
private void changeTheLookAndFeel(int value){
try{
UIManager.setLookAndFeel(looks[value].getClassName());
//通过当前外观初始化其 UI 属性
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception exception){
exception.printStackTrace();
}
}
private class ItemHandler implements ItemListener{
public void itemStateChanged(ItemEvent event){
for(int count=0;count<radio.length;count++){
if(radio[count].isSelected()){
label.setText(String.format("This is a %s look-and-feel",lookNames[count]));
comboBox.setSelectedIndex(count);
changeTheLookAndFeel(count);
}
}
}
}
}
//LookAndFeelDemo.java
import javax.swing.JFrame;
public class LookAndFeelDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LookAndFeelFrame lookAndFeelFrame=new LookAndFeelFrame();
lookAndFeelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lookAndFeelFrame.setSize(400,200);
lookAndFeelFrame.setVisible(true);
}
}
//Java金属风格
//Nimbus
//CDE/Motif
//windows
//Windows Classic
热心网友 时间:2023-10-08 22:58
程序中添加代码“UIManager.setLookAndFeel(lookAndFeel);”即可,热心网友 时间:2023-10-08 22:58
使用jdk自带的nimbus,感觉很好了,很方便热心网友 时间:2023-10-08 22:59
http://hi.baidu.com/epdc147/item/625efbd23c354bc6795daac5