案例:
javapublic class exer1 { JFrame s=new JFrame("练习"); //菜单 JMenuBar jMenuBar=new JMenuBar(); JMenu wenjian=new JMenu("文件"); JMenu bianji=new JMenu("编辑"); JMenuItem zidong=new JMenuItem("自动换行"); JMenuItem fuzhi=new JMenuItem("复制"); JMenuItem zhantie=new JMenuItem("粘贴"); JMenu geshi=new JMenu("格式"); JMenuItem zhushi=new JMenuItem("注释"); JMenuItem quxiao=new JMenuItem("取消注释"); //文本域 JTextArea text=new JTextArea(10,30); //列表 String[] ss={"红色","白色","紫色"}; JList<String> colorlist=new JList<>(ss); //组件 JComboBox<String> colorselect=new JComboBox<>(ss); ButtonGroup bg=new ButtonGroup(); JRadioButton b1=new JRadioButton("男",true);//默认选中 JRadioButton b2=new JRadioButton("女",false); //复选框 JCheckBox checkBox=new JCheckBox("是否已婚",true); //最下方 JTextField tf=new JTextField(40); JButton button=new JButton("确定",new ImageIcon("D:\\idea\\project\\pro1\\exercise1\\src\\ok.png")); //声明鼠标右键点击的菜单 JPopupMenu popupMenu=new JPopupMenu(); ButtonGroup bb=new ButtonGroup(); JRadioButtonMenuItem metal=new JRadioButtonMenuItem("metal风格"); JRadioButtonMenuItem nimbus=new JRadioButtonMenuItem("nimbus风格"); JRadioButtonMenuItem windows=new JRadioButtonMenuItem("windows风格"); JRadioButtonMenuItem windoowsclassic=new JRadioButtonMenuItem("windows经典风格"); JRadioButtonMenuItem motif=new JRadioButtonMenuItem("motif经典风格"); public void init(){ //开始组装 JPanel p1=new JPanel(); p1.add(tf); p1.add(button); s.add(p1,BorderLayout.SOUTH); //关于选择的组件 JPanel selectpanel=new JPanel(); selectpanel.add(colorselect); bg.add(b1); bg.add(b2); selectpanel.add(b1); selectpanel.add(b2); selectpanel.add(checkBox); //文本域和选择相关 Box topleft=Box.createVerticalBox(); topleft.add(text); topleft.add(selectpanel); //组装顶部 Box top=Box.createHorizontalBox(); top.add(topleft); top.add(colorlist); s.add(top); //开始组装顶部菜单 geshi.add(zhushi); geshi.add(quxiao); bianji.add(zidong); //加横线 bianji.addSeparator(); bianji.add(fuzhi); bianji.add(zhantie); //加横线 bianji.addSeparator(); bianji.add(geshi); jMenuBar.add(wenjian); jMenuBar.add(bianji); s.setJMenuBar(jMenuBar); //组装鼠标右键菜单 popupMenu.add(metal); popupMenu.add(nimbus); popupMenu.add(windows); popupMenu.add(windoowsclassic); popupMenu.add(motif); ActionListener listener=new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //当前选择的是哪种风格 String ss=e.getActionCommand(); try { change(ss); } catch (UnsupportedLookAndFeelException unsupportedLookAndFeelException) { unsupportedLookAndFeelException.printStackTrace(); } catch (ClassNotFoundException classNotFoundException) { classNotFoundException.printStackTrace(); } catch (InstantiationException instantiationException) { instantiationException.printStackTrace(); } catch (IllegalAccessException illegalAccessException) { illegalAccessException.printStackTrace(); } } }; metal.addActionListener(listener); nimbus.addActionListener(listener); windows.addActionListener(listener); windoowsclassic.addActionListener(listener); motif.addActionListener(listener); popupMenu.add(metal); popupMenu.add(nimbus); popupMenu.add(windows); popupMenu.add(windoowsclassic); popupMenu.add(motif); //添加在文本区域右键出现菜单,这个不用在判断,直接设置即可 text.setComponentPopupMenu(popupMenu); //关闭程序 s.setDefaultCloseOperation(3); s.pack(); s.setVisible(true); } private void change(String command) throws UnsupportedLookAndFeelException, ClassNotFoundException, InstantiationException, IllegalAccessException { //定义的这个方法用来改变界面风格 switch (command){ case "metal风格": UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); break; case "nimbus风格": UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); break; case "windows风格": UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); break; case "windows经典风格": UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); break; case "motif风格": UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); break; } //刷新组件的外观 SwingUtilities.updateComponentTreeUI(s.getContentPane()); SwingUtilities.updateComponentTreeUI(popupMenu); SwingUtilities.updateComponentTreeUI(jMenuBar); } }