Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Java make menus with check boxes

Shulou Source: shulou.com Published: 2022-06-02 06:03:10 09月29日 Update

This article shows you how Java makes a menu with a check box, which is concise and easy to understand. I hope you can learn something through the detailed introduction of this article.

Description:

Above is the minesweeper game I do with Java, which uses a menu with a check box, which is also done with JCheckBoxMenuItem, but found that there are a lot of problems, and then simply use ordinary JMenuItem to do, the effect is also good. In fact, to put it bluntly, it is very simple to write an article on the text of the menu, preceded by a √. By comparing the text content to determine whether to show selected or unselected, with or without √ in front, and how other text content changes, just like the difficulty of minesweeping, primary, intermediate, advanced can only choose one.

Code: 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; / * function: game window

* author: I am Lag.

* / public class GameFrame extends JFrame implements ActionListener {private static final long serialVersionUID = 2596945399892762751L; / * * Game panel * / private GamePanel gamePanel; / * * menu control * / JMenuItem jmi_easy,jmi_normal,jmi_hard; / * * function: constructor

* / public GameFrame () {try {/ / window this.setTitle ("mine clearance"); this.setLayout (null); this.setResizable (false); this.setLocationRelativeTo (null) This.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); / / menu JMenuBar jmb_minesweeper = new JMenuBar (); JMenu jm_game = new JMenu ("games"); jm_game.setFont (new Font ("Microsoft Yahi", Font.PLAIN,12)) JMenuItem jmi_new = jm_game.add ("opening"); jmi_new.setFont (new Font ("Microsoft Acer", Font.PLAIN,12)); jmi_new.addActionListener (this); jmi_new.setActionCommand ("new"); jm_game.addSeparator () This.jmi_easy = jm_game.add ("√ entry"); this.jmi_easy.setFont (new Font ("Microsoft Yahi", Font.PLAIN,12)); this.jmi_easy.addActionListener (this); this.jmi_easy.setActionCommand ("easy") This.jmi_normal = jm_game.add ("intermediate"); this.jmi_normal.setFont (new Font ("Microsoft Yahi", Font.PLAIN,12)); this.jmi_normal.addActionListener (this); this.jmi_normal.setActionCommand ("normal") This.jmi_hard = jm_game.add ("Advanced"); this.jmi_hard.setFont (new Font ("Microsoft Yahi", Font.PLAIN,12)); this.jmi_hard.addActionListener (this); this.jmi_hard.setActionCommand ("hard"); jm_game.addSeparator () JMenuItem jmi_exit = jm_game.add ("quit"); jmi_exit.setFont (new Font ("Microsoft Yahi", Font.PLAIN,12)); jmi_exit.addActionListener (this); jmi_exit.setActionCommand ("exit") Jmb_minesweeper.add (jm_game); JMenu jm_help = new JMenu ("help"); jm_help.setFont (new Font ("Microsoft Yahi", Font.PLAIN,12); JMenuItem jmi_about = jm_help.add ("about") Jmi_about.setFont (new Font (Microsoft Acer, Font.PLAIN,12); jmi_about.addActionListener (this); jmi_about.setActionCommand ("about"); jmb_minesweeper.add (jm_help); this.setJMenuBar (jmb_minesweeper) / / Panel this.gamePanel = new GamePanel (); this.add (this.gamePanel); / / display this.gamePanel.setLevel (this.gamePanel.EASY) This.setSize (this.gamePanel.getWidth () + 6 catch. GamePanel.getHeight () + 50); this.setVisible (GamePanel.getHeight ());} catch (GamePanel.getHeight) {JOptionPane.showMessageDialog (this, "the program has encountered an abnormal error and is about to exit! \ r\ n\ r\ n "+ e.toString ()," prompt, JOptionPane.ERROR_MESSAGE); System.exit (0);}} / * function: event monitoring

* / @ 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 ("√ Primary"); this.jmi_normal.setText ("Intermediate"); this.jmi_hard.setText ("Advanced"); this.gamePanel.setLevel (this.gamePanel.EASY) This.setSize (this.gamePanel.getWidth () + 6 normal. GamePanel.getHeight () + 50);} else if ("normal" .equals (command)) {this.jmi_easy.setText ("Junior"); this.jmi_normal.setText ("Junior") This.jmi_hard.setText (Advanced); this.gamePanel.setLevel (this.gamePanel.NORMAL); this.setSize (this.gamePanel.getWidth () + 6 math.gamePanel.getHeight () + 50) } else if ("hard" .equals (command)) {this.jmi_easy.setText ("primary"); this.jmi_normal.setText ("intermediate"); this.jmi_hard.setText ("√ advanced"); this.gamePanel.setLevel (this.gamePanel.HARD) This.setSize (this.gamePanel.getWidth () + 6 System.exit. GamePanel.getHeight () + 50);} else if ("exit" .equals (command)) {System.exit (0) } else if ("about" .equals (command)) {JOptionPane.showMessageDialog (this, "I am Lag", "hint", JOptionPane.INFORMATION_MESSAGE);} the above is how Java makes menus with check boxes. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Microsoft menus advanced content check boxes functions text minesweeping that is skills wooden fish knowledge or panels tips good ordinary concise concise events Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MariaDB vpn macOS OPPO Reno