java怎么通过点击菜单弹出对话框
2个回答
展开全部
import java.awt.GridLayout;
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;
public class testMenuItem {
public static void main(String[] args) {
int x=200,y=200,width=400,height=400;
final JFrame j=new JFrame();
j.setLayout(new GridLayout(10, 1));
JMenuBar jb=new JMenuBar();
j.add(jb);
JMenu jm=new JMenu("菜单");
jb.add(jm);
JMenuItem jm1=new JMenuItem("选项1");
JMenuItem jm2=new JMenuItem("选项2");
JMenuItem jm3=new JMenuItem("选项3");
ActionListener al =new ActionListener(){
public void actionPerformed(ActionEvent e) {
String buttonString=e.getActionCommand();
if(buttonString.equals("选项1"))
JOptionPane.showMessageDialog(j, "您选择了 选项1");
else if(buttonString.equals("选项2"))
JOptionPane.showMessageDialog(j, "您选择了 选项2");
else if(buttonString.equals("选项3"))
JOptionPane.showMessageDialog(j, "您选择了 选项3");
}
};
jm1.addActionListener(al);
jm2.addActionListener(al);
jm3.addActionListener(al);
jm.add(jm1);
jm.add(jm2);
jm.add(jm3);
j.setBounds(x, y, width, height);
j.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
}
}
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;
public class testMenuItem {
public static void main(String[] args) {
int x=200,y=200,width=400,height=400;
final JFrame j=new JFrame();
j.setLayout(new GridLayout(10, 1));
JMenuBar jb=new JMenuBar();
j.add(jb);
JMenu jm=new JMenu("菜单");
jb.add(jm);
JMenuItem jm1=new JMenuItem("选项1");
JMenuItem jm2=new JMenuItem("选项2");
JMenuItem jm3=new JMenuItem("选项3");
ActionListener al =new ActionListener(){
public void actionPerformed(ActionEvent e) {
String buttonString=e.getActionCommand();
if(buttonString.equals("选项1"))
JOptionPane.showMessageDialog(j, "您选择了 选项1");
else if(buttonString.equals("选项2"))
JOptionPane.showMessageDialog(j, "您选择了 选项2");
else if(buttonString.equals("选项3"))
JOptionPane.showMessageDialog(j, "您选择了 选项3");
}
};
jm1.addActionListener(al);
jm2.addActionListener(al);
jm3.addActionListener(al);
jm.add(jm1);
jm.add(jm2);
jm.add(jm3);
j.setBounds(x, y, width, height);
j.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
}
}
追问
那如果是弹出另一个存在的对话框,不是这种消息对话框!
追答
改这句JOptionPane.showMessageDialog(j, "您选择了 选项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();
}
}
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();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询