Java如何做带复选框的菜单实例代码

Java如何做带复选框的菜单实例代码_第1张图片

说明:

        上面是我用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;
 
/**
 * 功能:游戏窗口
* 作者:我是小木鱼(Lag)
*/ public class GameFrame extends JFrame implements ActionListener { private static final long serialVersionUID = 2596945399892762751L; /** 游戏面板 */ private GamePanel gamePanel; /** 菜单控件 */ JMenuItem jmi_easy,jmi_normal,jmi_hard; /** * 功能:构造函数
*/ 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); } } /** * 功能:事件监听
*/ @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如何做带复选框的菜单实例代码的文章就介绍到这了,更多相关Java复选框菜单内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(Java如何做带复选框的菜单实例代码)