本篇文章为大家展示了Java是如何做带复选框的菜单,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
上面是我用Java做的扫雷游戏,其中就用到了带复选框式的菜单,原来也是用JCheckBoxMenuItem做的,但发现实在是问题多多,后干脆就用普通的JMenuItem来做,效果也不错。实际上说穿了很简单,就是在菜单的文本上做文章,前面加上一个 √ 即可。通过比较文本内容来判断是显示选中还是未选中,前面加还是不加 √ ,同时其他的文本内容如何变化,就好像扫雷的难度,初级、中级、高级只能选中一个。
package com.game.mine; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JCheckBoxMenuItem; /** * 功能:游戏窗口<br> * 作者:我是小木鱼(Lag)<br> */ public class GameFrame extends JFrame implements ActionListener { private static final long serialVersionUID = 2596945399892762751L; /** 游戏面板 */ private GamePanel gamePanel; /** 菜单控件 */ JMenuItem jmi_easy,jmi_normal,jmi_hard; /** * 功能:构造函数<br> */ public GameFrame() { try { //窗口 this.setTitle("扫雷"); this.setLayout(null); this.setResizable(false); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //菜单 JMenuBar jmb_minesweeper = new JMenuBar(); JMenu jm_game = new JMenu("游戏"); jm_game.setFont(new Font("微软雅黑",Font.PLAIN,12)); JMenuItem jmi_new = jm_game.add(" 开局"); jmi_new.setFont(new Font("微软雅黑",Font.PLAIN,12)); jmi_new.addActionListener(this); jmi_new.setActionCommand("new"); jm_game.addSeparator(); this.jmi_easy = jm_game.add("√ 初级"); this.jmi_easy.setFont(new Font("微软雅黑",Font.PLAIN,12)); this.jmi_easy.addActionListener(this); this.jmi_easy.setActionCommand("easy"); this.jmi_normal = jm_game.add(" 中级"); this.jmi_normal.setFont(new Font("微软雅黑",Font.PLAIN,12)); this.jmi_normal.addActionListener(this); this.jmi_normal.setActionCommand("normal"); this.jmi_hard = jm_game.add(" 高级"); this.jmi_hard.setFont(new Font("微软雅黑",Font.PLAIN,12)); this.jmi_hard.addActionListener(this); this.jmi_hard.setActionCommand("hard"); jm_game.addSeparator(); JMenuItem jmi_exit = jm_game.add(" 退出"); jmi_exit.setFont(new Font("微软雅黑",Font.PLAIN,12)); jmi_exit.addActionListener(this); jmi_exit.setActionCommand("exit"); jmb_minesweeper.add(jm_game); JMenu jm_help = new JMenu("帮助"); jm_help.setFont(new Font("微软雅黑",Font.PLAIN,12)); JMenuItem jmi_about = jm_help.add("关于"); jmi_about.setFont(new Font("微软雅黑",Font.PLAIN,12)); jmi_about.addActionListener(this); jmi_about.setActionCommand("about"); jmb_minesweeper.add(jm_help); this.setJMenuBar(jmb_minesweeper); //面板 this.gamePanel = new GamePanel(); this.add(this.gamePanel); //显示 this.gamePanel.setLevel(this.gamePanel.EASY); this.setSize(this.gamePanel.getWidth() + 6,this.gamePanel.getHeight() + 50); this.setVisible(true); } catch(Exception e) { JOptionPane.showMessageDialog(this,"程序出现异常错误,即将退出!\r\n\r\n"+e.toString(),"提示",JOptionPane.ERROR_MESSAGE); System.exit(0); } } /** * 功能:事件监听<br> */ @Override public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if("new".equals(command)) { this.gamePanel.newGame(); } else if("easy".equals(command)) { this.jmi_easy.setText("√ 初级"); this.jmi_normal.setText(" 中级"); this.jmi_hard.setText(" 高级"); this.gamePanel.setLevel(this.gamePanel.EASY); this.setSize(this.gamePanel.getWidth() + 6,this.gamePanel.getHeight() + 50); } else if("normal".equals(command)) { this.jmi_easy.setText(" 初级"); this.jmi_normal.setText("√ 中级"); this.jmi_hard.setText(" 高级"); this.gamePanel.setLevel(this.gamePanel.NORMAL); this.setSize(this.gamePanel.getWidth() + 6,this.gamePanel.getHeight() + 50); } else if("hard".equals(command)) { this.jmi_easy.setText(" 初级"); this.jmi_normal.setText(" 中级"); this.jmi_hard.setText("√ 高级"); this.gamePanel.setLevel(this.gamePanel.HARD); this.setSize(this.gamePanel.getWidth() + 6,this.gamePanel.getHeight() + 50); } else if("exit".equals(command)) { System.exit(0); } else if("about".equals(command)) { JOptionPane.showMessageDialog(this,"我是小木鱼(Lag)","提示",JOptionPane.INFORMATION_MESSAGE); } } }
上述内容就是Java是如何做带复选框的菜单,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。