⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stylechooser2.java

📁 提供了常用的JAVA技术的示例
💻 JAVA
字号:
// 13-4 构 造 swing 不 同 显 示 风 格 。package StyleChooser2;import java.awt.*;import java.awt.color.*;import java.awt.event.*;import javax.swing.*;import javax.swing.ImageIcon;import javax.swing.border.*;import javax.swing.UIManager;public class StyleChooser2 extends JFrame{    StyleChooser2 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 StyleChooser2(){        super("StyleChooser");        JPanel panel=new JPanel();        frame=this;        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);        // 监 听        RadioListener myListener=new RadioListener();        motifButton.addActionListener(myListener);        metalButton.addActionListener(myListener);        windowsButton.addActionListener(myListener);        getContentPane().setLayout(new BorderLayout());        getContentPane().add("North",label);        getContentPane().add("Center",panel);        panel.add(motifButton);        panel.add(metalButton);        panel.add(windowsButton);        panel.setLayout(new GridLayout(3,1));        motifButton.setToolTipText("motif");        metalButton.setToolTipText("metal");        windowsButton.setToolTipText("windows");        pack();        //frame.setSize(200,150);        setVisible(true);        addWindowListener(new WindowAdapter(){          public void windowClosing(WindowEvent e){            System.exit(0);          }        });    }    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) {      StyleChooser2 f = new StyleChooser2();      f.updateState();  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -