「java点击菜单」java点击菜单项,出现另一个窗口

博主:adminadmin 2022-12-27 17:12:06 61

今天给各位分享java点击菜单的知识,其中也会对java点击菜单项,出现另一个窗口进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

问下各位大神,在java编写的程序中点击菜单栏的一个菜单项,比如在格式菜单下的字体和颜色,弹出一个窗口

你说的是打开非模态对话框。

如果模态对话框不关闭,无法操作主窗体。如果非模态对话框不关闭,仍然可以操作主窗体。

//构造模态对话框

final Dialog d = new Dialog(this, "模态对话框", true);

//构造非模态对话框

final Dialog d = new Dialog(this, "模态对话框", false);

样例程序如下:

import java.awt.Dialog;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JButton;

import javax.swing.JFrame;

public class Main extends JFrame implements ActionListener {

JButton btnModel, btnNonModel;

public Main() {

super("对话框");

this.setLayout(new FlowLayout());

btnModel = new JButton("打开模态对话框");

btnNonModel = new JButton("打开非模态对话框");

this.add(btnModel);

this.add(btnNonModel);

btnModel.addActionListener(this);

btnNonModel.addActionListener(this);

this.setSize(200, 200);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

    new Main();

}

@Override

public void actionPerformed(ActionEvent arg0) {

JButton btn = (JButton) arg0.getSource();

if(btn == btnModel) { //打开模态对话框

final Dialog d = new Dialog(this, "模态对话框", true);

            d.setSize(800, 600);

            d.addWindowListener(new WindowAdapter() {

                public void windowClosing(WindowEvent evt) {

                    d.setVisible(false);

                }

            });

            d.setVisible(true);

}

else if(btn == btnNonModel) { //打开非模态对话框

final Dialog d = new Dialog(this, "非模态对话框", false);

            d.setSize(800, 600);

            d.addWindowListener(new WindowAdapter() {

                public void windowClosing(WindowEvent evt) {

                    d.setVisible(false);

                }

            });

            d.setVisible(true);

}

}

}

java点击菜单项弹出窗口怎么弄

代码缺一行:

。。。

authorTextArea.setPreferredSize(new Dimension(40, 80));

authorFrame.add(authorTextArea);

。。。

以上完了后,需要加一个

authorFrame.setVisible(true);

至于这个框的大小,你再调调哈,相互学习~,三年没做过了~

java创建菜单点击后没反应

JMenuItem 菜单项 可以使用addActionListener

JMenu 菜单 需要使用的是 addMenuListener

我写了个完整的参考代码. 图例如下

3个画蓝圆圈的地方, 都可以点击后退出

参考代码如下

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

public class Practice1 extends JFrame implements ActionListener {

public Practice1() {

JMenuBar jmb = new JMenuBar(); //语句结束需要有分号

JMenu jm = new JMenu("退出");

JMenu jm2 = new JMenu("功能");// 创建第二个菜单

JMenuItem jmi = new JMenuItem("退出");// 创建一个菜单项

jm2.add(jmi);

jmi.addActionListener(this);// 该菜单项添事件响应

jmb.add(jm);

jmb.add(jm2);

this.setJMenuBar(jmb);

this.setSize(200, 200);

this.setLocationRelativeTo(null);// 居中

this.setVisible(true);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);// 当点击右上角的X按钮时,结束程序

// 如果是JMenuItem 那么需要添加ActionListener

// 注意如果是JMenu,那么需要添加是MenuListener

jm.addMenuListener(new MenuListener() {

@Override

public void menuSelected(MenuEvent e) {//当菜单被选中

System.exit(0);

}

@Override

public void menuDeselected(MenuEvent e) {

}

@Override

public void menuCanceled(MenuEvent e) {

}

});

}

public static void main(String[] args) {

new Practice1();

}

@Override

public void actionPerformed(ActionEvent e) {

switch (e.getActionCommand()) {

case "退出":

System.exit(0);

break;

default:

break;

}

}

}

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);

}

}

关于java点击菜单和java点击菜单项,出现另一个窗口的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。