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如何做帶復選框的菜單實例代碼的文章就介紹到這瞭,更多相關Java復選框菜單內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: