Java中怎样在菜单栏中点击一下就弹出一个对话框,怎样设置能不能把它的源代码发送我邮箱254073460@qq.com

2024-12-22 22:43:48
推荐回答(1个)
回答1:

//这是刚写的,不知道是否满足你要求,你试运行下,有问题可以追问
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Test extends JFrame{
static Test t;
public Test() {
init();
}
public void init(){
setSize(300,100);
setContentPane(createContentPane());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private JPanel createContentPane(){
JPanel p=new JPanel();
JMenuBar mb=new JMenuBar();
JMenu m=new JMenu("Test");
JMenuItem mi=new JMenuItem("Click");
mi.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {
JOptionPane.showMessageDialog(t, "测试成功");
}
public void mousePressed(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
});

m.add(mi);
mb.add(m);
p.add(mb);
return p;
}
public static void main(String[] args) {
t=new Test();
}
}