首页 > 编程知识 正文

java菜单,Java菜单窗口

时间:2024-04-27 11:08:47 阅读:334842 作者:NIOB

本文目录一览:

JAVA游戏菜单

java设置游戏菜单可以很朴素 ,也可以比较华丽,简单的写了两个参考效果

分析菜单导航到游戏的过程, 我们可以在同一个容器里实现, 也可以在不同的窗口里实现.

我们要根据具体 需求分析是切换窗口还是切换容器;

朴素版本 使用了的不同窗口的切换来实现 参考的代码如下

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

//这个代表扫雷窗口

class SL extends JFrame {

public SL() {

getContentPane().setBackground(Color.BLUE);

setTitle("扫雷");

setSize(MenuFrame.W, MenuFrame.H);

setLocationRelativeTo(null);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(true);

}

}

//这个代表围棋窗口

class WQ extends JFrame {

public WQ() {

getContentPane().setBackground(Color.ORANGE);

setTitle("围棋");

setSize(MenuFrame.W, MenuFrame.H);

setLocationRelativeTo(null);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(true);

}

}

//这个代表菜单选择窗口

public class MenuFrame extends JFrame implements ActionListener {

public static final int W = 300;

public static final int H = 200;

JButton jb1, jb2;

public MenuFrame() {

JPanel jp = new JPanel();

BoxLayout box = new BoxLayout(jp, BoxLayout.Y_AXIS);//垂直方向的布局

jp.setLayout(box);

jb1 = new JButton("益智扫雷");

jb1.addActionListener(this);

jb2 = new JButton("围棋春秋");

jb2.addActionListener(this);

JButton jb3=new JButton("再续前缘");

JButton jb4=new JButton("退隐江湖");

JButton jb5=new JButton("帮助文档");

jp.add(jb1);

jp.add(jb2);

jp.add(jb3);

jp.add(jb4);

jp.add(jb5);

add(jp);

setLayout(new FlowLayout());

setTitle("java Game Center");

setSize(W, H);

setLocationRelativeTo(null);//窗口居中

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(() - {

new MenuFrame().setVisible(true);//启动菜单窗口

});

}

@Override

public void actionPerformed(ActionEvent e) {

JButton jb = (JButton) e.getSource();

if (jb == jb1) {

//隐藏关闭菜单窗口

this.setVisible(false);

this.dispose();

//打开扫雷窗口

new SL().setVisible(true);

} else if (jb == jb2) {

this.setVisible(false);

this.dispose();

new WQ().setVisible(true);

}

}

}

如何给Java窗体添加菜单栏

5步。

1、创建菜单(如文件)

2、创建菜单项(如新建、打开、保存)

3、将菜单项添加到菜单(如将新建、打开、保存菜单项添加到文件菜单)

4、创建菜单栏,将菜单添加到菜单栏

5、设置窗口的菜单栏

核心代码:

//创建窗口

JFrame w=new JFrame("包含菜单栏的窗口");

//创建文件菜单

JMenu file=new JMenu("文件");

//创建新建菜单项

JMenuItem n=new JMenuItem("新建");

//创建打开菜单项

JMenuItem o=new JMenuItem("打开");

//创建保存菜单项

JMenuItem s=new JMenuItem("保存");

//将新建菜单项添加到文件菜单

file.add(n);

//将打开菜单项添加到文件菜单

file.add(o);

//将保存菜单项添加到文件菜单

file.add(s);

//创建菜单栏

JMenuBar br=new JMenuBar();

//将文件菜单添加到菜单栏

br.add(file);

//为窗口设置菜单栏

w.setJMenuBar(br);

Java中菜单组件的类是什么?

JMenuBar菜单栏

JMenu 菜单

将菜单add到菜单栏,最后将菜单栏添加到面板上(setJMenuBar(菜单栏对象);)

例子:

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

public class MyMenu extends JFrame {

    private static final long serialVersionUID = -367679212385637764L;

    /**

     * 创建一个新的实例 MyMenu.

     */

    public MyMenu() {

        // TODO Auto-generated constructor stub

        /**

         * 菜单名称

         */

        String[] buttons = new String[]{"提交", "保存", "清除", "退出"};

        String[] colors = new String[] {"红色", "黄色", "默认"};

        /**

         *  菜单栏

         */

        JMenuBar menubar = new JMenuBar();

        JMenu jmManager = new JMenu("管理");

        for(String text : buttons) {

            JMenuItem item = new JMenuItem(text);

            jmManager.add(item);

        }

                             

        JMenu jmColor = new JMenu("颜色");

        for(String text : colors) {

            JMenuItem item = new JMenuItem(text);

            jmColor.add(item);

        }

        /**

         * 添加菜单

         */

        menubar.add(jmManager);

        menubar.add(jmColor);

        this.setJMenuBar(menubar);

        /**

         * 面板基本设置

         */

        this.setTitle("Menu Demo");

        this.setSize(200, 200);

        this.setResizable(false);

        this.setVisible(true);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

                         

    public static void main(String[] args) {

        new MyMenu();

    }

}

运行结果如下:

java的菜单代码怎么写?

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MyMenu extends JFrame{

JMenuBar jmbar=new JMenuBar();

JMenu jmenu=new JMenu("颜色");

JMenuItem jmt1=new JMenuItem("红色"),

jmt2=new JMenuItem("黄色"),

jmt3=new JMenuItem("蓝色");

JPanel jp=new JPanel();

MyMenu(){

setTitle("菜单测试");

setSize(400,300);

setJMenuBar(jmbar);

jmbar.add(jmenu);

jmenu.add(jmt1);

jmenu.add(jmt2);

jmenu.add(jmt3);

add(jp);

jmt1.addActionListener(new MenuAction(this));

jmt2.addActionListener(new MenuAction(this));

jmt3.addActionListener(new MenuAction(this));

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

public static void main(String[] args) {

new MyMenu();

}

}

class MenuAction implements ActionListener{

MyMenu m;

MenuAction(MyMenu m){

this.m=m;

}

public void actionPerformed(ActionEvent e){

String color=e.getActionCommand();

if(color=="红色")m.jp.setBackground(Color.red);

else if(color=="黄色")m.jp.setBackground(Color.yellow);

else if(color=="蓝色")m.jp.setBackground(Color.blue);

}

}

不知道你要什么事件代码,我写了个比较简单的你看适合不。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。