📄 guangan1.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class guangan1 extends JPanel
{
static JFrame frame;
static String metal="Metal观感";
static String motif="Motif观感";
static String windows="Windows观感";
JRadioButton metalButton,motifButton,windowsButton;
public guangan1()
{
JButton button=new JButton("不同的观感效果");
metalButton=new JRadioButton(metal);
metalButton.setActionCommand(metal);
motifButton=new JRadioButton(motif);
motifButton.setActionCommand(motif);
windowsButton=new JRadioButton(windows);
windowsButton.setActionCommand(windows);
//建立组
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);
}
public static void main(String args[])
{
JPanel panel=new guangan1();
frame=new JFrame("观感");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add("Center",panel);
frame.setSize(450,70);
frame.setVisible(true);
}
class RadioListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if((e.getActionCommand()).equals(metal))
SetLAF.setJavaLookAndFeel();
else if((e.getActionCommand()).equals(motif))
SetLAF.setMotifLookAndFeel();
else SetLAF.setNativeLookAndFeel();
SwingUtilities.updateComponentTreeUI(frame);
frame.setSize(450,70);
}
}
}
class SetLAF {
public static void setNativeLookAndFeel()
{
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
System.out.println("设置NativeLAF错误:"+e);
}
}
public static void setJavaLookAndFeel()
{
try{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}catch(Exception e){
System.out.println("设置JavaLAF错误:"+e);
}
}
public static void setMotifLookAndFeel()
{
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}catch(Exception e){
System.out.println("设置MotifLAF错误:"+e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -