注释很详细,直接上代码
上一篇
新增内容
1.为按钮指定图标
2. 列表框的并列
3.菜单项绑定快捷键
4.控件悬浮提示信息
5.菜单项设置小图标
6.五种布局风格右键选择切换
package swing21_30;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;public class swing_test_25 {JFrame frame = new JFrame("swing测试");//定义一个按钮,并为其指定图标Icon okIcon = new ImageIcon("img/ok.png");JButton ok = new JButton("确定",okIcon);//定义一个单选按钮,初始处于选中的状态JRadioButton male = new JRadioButton("男",true);//定义一个单选按钮,初始处于未选中状态JRadioButton female = new JRadioButton("女",false);//定义一个ButtonGroup,把male和female组合起来,实现单选ButtonGroup buttonGroup = new ButtonGroup();//定义一个复选框,初始处于没有选中状态JCheckBox married = new JCheckBox("是否已婚?",false);//定义一个数组存储颜色String[] colors = { "红色", "绿色 " , "蓝色 " };//定义一个下拉选择框,展示颜色JComboBox<String> colorChooser = new JComboBox<String>(colors);//定一个列表框,展示颜色JList<String> colorList = new JList<String>(colors);//定义一个8行20列的多行文本域JTextArea textArea = new JTextArea(8,20);//定义一个40列的单行文本域JTextField name = new JTextField(40);//定义菜单条JMenuBar menuBar = new JMenuBar();//定义菜单JMenu file = new JMenu("文件");JMenu edit = new JMenu("编辑");//创建菜单项,并指定图标JMenuItem newItem = new JMenuItem("新建",new ImageIcon("img/new.png"));JMenuItem saveItem = new JMenuItem("保存",new ImageIcon("img/save.png"));JMenuItem exitItem = new JMenuItem("退出",new ImageIcon("img/exit.png"));JCheckBoxMenuItem autoWrap = new JCheckBoxMenuItem("自动换行");JMenuItem copyItem = new JMenuItem("复制",new ImageIcon("img/copy.png"));JMenuItem pasteItem = new JMenuItem("粘贴",new ImageIcon("img/paste.png"));//定义二级菜单,将来会添加到编辑中JMenu format = new JMenu("格式");JMenuItem commentItem = new JMenuItem("注释");JMenuItem cancelItem = new JMenuItem("取消注释");//定义一个右键菜单,用于设置程序的外观风格JPopupMenu pop = new JPopupMenu();//定义一个ButtongGroup对象,用于组合风格按钮,形成单选ButtonGroup flavorGroup = new ButtonGroup();//定义五个单选按钮菜单项,用于设置程序风格JRadioButtonMenuItem metalItem = new JRadioButtonMenuItem("Metal 风格",true);JRadioButtonMenuItem nimbusItem = new JRadioButtonMenuItem("Nimbus 风格",true);JRadioButtonMenuItem windowsItem = new JRadioButtonMenuItem("Windows 风格",true);JRadioButtonMenuItem classicItem = new JRadioButtonMenuItem("Windows 经典风格",true);JRadioButtonMenuItem motifItem = new JRadioButtonMenuItem("Motif 风格",true);//初始化界面public void init(){//----------------------组合主区域------------------------//创建一个装载文本框和按钮的JPanelJPanel bottom = new JPanel();bottom.add(name);bottom.add(ok);frame.add(bottom, BorderLayout.SOUTH);//创建一个装载下拉选择框、三个JChekBox的JPanelJPanel checkPanel = new JPanel();checkPanel.add(colorChooser);buttonGroup.add(male);buttonGroup.add(female);checkPanel.add(male);checkPanel.add(female);checkPanel.add(married);//创建一个垂直排列的Box,装载checkPanel和多行文本域Box topLeft = Box.createVerticalBox();//使用JScrollPane作为普通组件的JViewPortJScrollPane taJsp = new JScrollPane(textArea);topLeft.add(taJsp);topLeft.add(checkPanel);//创建一个水平排列的Box,装载topLeft和colorListBox top = Box.createHorizontalBox();top.add(topLeft);top.add(colorList);//将top Box 添加到窗口的中间frame.add(top);//---------------------------组合菜单条----------------------------------------------//为newItem添加快捷键 ctrl+NnewItem.setAccelerator(KeyStroke.getKeyStroke('N', InputEvent.CTRL_MASK));newItem.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {textArea.append("用户点击了“新建”菜单\n");}});//为file添加菜单项file.add(newItem);file.add(saveItem);file.add(exitItem);//为edit添加菜单项edit.add(autoWrap);edit.addSeparator();edit.add(copyItem);edit.add(pasteItem);//为commentItem添加提示信息commentItem.setToolTipText("将程序代码注释起来");//为format菜单添加菜单项format.add(commentItem);format.add(cancelItem);//给edit添加一个分隔符edit.addSeparator();//把format添加到edit中形成二级菜单edit.add(format);//把edit file 添加到菜单条中menuBar.add(file);menuBar.add(edit);//把菜单条设置给窗口frame.setJMenuBar(menuBar);flavorGroup.add(metalItem);flavorGroup.add(nimbusItem);flavorGroup.add(windowsItem);flavorGroup.add(classicItem);flavorGroup.add(motifItem);//给5个风格菜单创建事件监听器ActionListener flavorLister = new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String command = e.getActionCommand();try {changeFlavor(command);} catch (Exception e1) {e1.printStackTrace();}}};//为5个风格菜单项注册监听器metalItem.addActionListener(flavorLister);nimbusItem.addActionListener(flavorLister);windowsItem.addActionListener(flavorLister);classicItem.addActionListener(flavorLister);motifItem.addActionListener(flavorLister);pop.add(metalItem);pop.add(nimbusItem);pop.add(windowsItem);pop.add(classicItem);pop.add(motifItem);//调用textArea组件的setComponentPopupMenu即可设置右键菜单,无需使用事件textArea.setComponentPopupMenu(pop);// 设置关闭窗口时推出程序frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置jFrame最佳大小并可见frame.pack();frame.setVisible(true);}//定义一个方法,用于改变界面风格private void changeFlavor(String command) throws Exception{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;}//更新窗口内顶级容器以及所有组件的UISwingUtilities.updateComponentTreeUI(frame.getContentPane());//更新mb菜单条及每部所有组件UISwingUtilities.updateComponentTreeUI(menuBar);//更新右键菜单及内部所有菜单项的UISwingUtilities.updateComponentTreeUI(pop);}public static void main(String[] args) {//创建窗口new swing_test_25().init();}}
效果演示:
素材链接:
img-blog.csdnimg.cn/direct/e2ba782862f64694af460841dbb86bb5.png
img-blog.csdnimg.cn/direct/5e09d28a6caf416d82145600077152e7.png
img-blog.csdnimg.cn/direct/de7ff1de40f84947a7e8a454e4096dcc.png
img-blog.csdnimg.cn/direct/8ea757b4c350484eb7dfa0ef265ea634.png