📄 stylechooser.java~99~
字号:
// 13-4 构 造 swing 不 同 显 示 风 格 。package StyleChooser;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.ImageIcon;import javax.swing.border.*;import javax.swing.UIManager;public class StyleChooser 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 motifButton=new JRadioButton(motif), metalButton=new JRadioButton(metal), windowsButton=new JRadioButton(windows); JLabel label=new JLabel("the styles of swing has "); public StyleChooser(){ metalButton.setMnemonic(KeyEvent.VK_E); metalButton.setActionCommand(metalClassName); motifButton.setMnemonic('M'); motifButton.setActionCommand(motifClassName); windowsButton.setMnemonic('w'); windowsButton.setActionCommand(windowsClassName); // 归 为 一 组 ButtonGroup group=new ButtonGroup(); group.add(motifButton); group.add(metalButton); group.add(windowsButton); //windowsButton.setSelected(true); // group.setSelected(new DefaultButtonModel(),true); // 监 听 RadioListener myListener=new RadioListener(); motifButton.addActionListener(myListener); metalButton.addActionListener(myListener); windowsButton.addActionListener(myListener); add(label); add(motifButton); add(metalButton); add(windowsButton); } class RadioListener implements ActionListener{ public void actionPerformed(ActionEvent e){ String InfName=e.getActionCommand(); try{ UIManager.setLookAndFeel(InfName); SwingUtilities.updateComponentTreeUI(frame); System.out.println("InfName="+InfName); frame.pack(); updateState(); } catch(Exception ee){ JRadioButton button=(JRadioButton)e.getSource(); button.setEnabled(false); updateState(); System.err.println("error info :"+ee.getMessage()); System.err.println("could not load lookandfeel:"+InfName); } } } public void updateState(){ String InfName=UIManager.getLookAndFeel().getClass().getName(); System.out.println("indexOf(motif)="+InfName.indexOf(motif)); System.out.println("indexOf(metal)="+InfName.indexOf(metal)); System.out.println("indexOf(windows)="+InfName.indexOf(windows)); if(InfName.indexOf(metal)>=0){ metalButton.setSelected(true); }else if(InfName.indexOf(windows)>=0){ windowsButton.setSelected(true); }else if(InfName.indexOf(motif)>=0){ motifButton.setSelected(true); }else{ System.err.println("StyleChooser is using an unknown:"+InfName); } } public static void main(String[] args) { StyleChooser panel = new StyleChooser(); frame=new JFrame("StyleChooser"); frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); frame.getContentPane().add("Center",panel); frame.pack(); //frame.setVisible(true); frame.show(); panel.updateState(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -