Java如何做帶復(fù)選框的菜單實例代碼

說明:
????????上面是我用Java做的掃雷游戲,其中就用到了帶復(fù)選框式的菜單,原來也是用JCheckBoxMenuItem做的,但發(fā)現(xiàn)實在是問題多多,后干脆就用普通的JMenuItem來做,效果也不錯。實際上說穿了很簡單,就是在菜單的文本上做文章,前面加上一個 √ 即可。通過比較文本內(nèi)容來判斷是顯示選中還是未選中,前面加還是不加 √ ,同時其他的文本內(nèi)容如何變化,就好像掃雷的難度,初級、中級、高級只能選中一個。
代碼:
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;
/**
* 功能:構(gòu)造函數(shù)<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("關(guān)于");
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,"程序出現(xiàn)異常錯誤,即將退出!\r\n\r\n"+e.toString(),"提示",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
/**
* 功能:事件監(jiān)聽<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);
}
}
}
? ? ? ? 上面是掃雷的部分代碼,游戲不是重點,重點看建立菜單和點擊的代碼。有時候解決問題的辦法有很多,換個思路就好!
到此這篇關(guān)于Java如何做帶復(fù)選框的菜單實例代碼的文章就介紹到這了,更多相關(guān)Java復(fù)選框菜單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mac Book中Java環(huán)境變量設(shè)置的方法
本文給大家介紹mac book 中設(shè)置java環(huán)境變量的方法,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-04-04
SpringMVC整合,出現(xiàn)注解沒有起作用的情況處理
這篇文章主要介紹了SpringMVC整合,出現(xiàn)注解沒有起作用的情況及處理,具有很好的參考價值,希望對大家有所幫助。2023-05-05
java商城項目實戰(zhàn)之購物車功能實現(xiàn)
這篇文章主要為大家詳細介紹了java商城項目實戰(zhàn)之購物車功能實現(xiàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
Java中stream處理中map與flatMap的比較和使用案例
這篇文章主要介紹了Java中stream處理中map與flatMap的比較和使用案例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

