java用awt 点击菜单或按钮弹出一个对话框 对话框dialog我创建了 为什么就是点了后没反应
packageChinesePoker;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;class...
package ChinesePoker;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Dialogs extends JDialog{//制作对话框
public Dialogs(JFrame parent){
super(parent,"My dialog",true);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(new JLabel("Here is my dialog"));
JButton ok=new JButton("OK");
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
cp.add(ok);
setSize(150,125);
}
}
class PokerFrame extends JFrame implements ActionListener{
private Dialogs dlg=new Dialogs (null);
private MenuItem openMI, exitMI;
private Button b1=new Button("洗 牌");
private Button b2=new Button("发 牌");
private Button b3=new Button("猜大");
private Button b4=new Button("猜小");
private MenuBar mbar =new MenuBar();
private Menu fileMenu =new Menu("菜单");
private Menu fileMenu1 =new Menu("帮助");
PokerFrame()
{
Panel pan= new Panel();
pan.add(b1);
pan.add(b2);
pan.add(b3);
b3.setLocation(900, 56);
b3.setLocation(940, 56);
pan.add(b4);
TextField tf=new TextField("12");
Font font =new Font("Monospaced",Font.PLAIN,15);
tf.setFont(font);
Label lb= new Label("分 数:");
pan.add(lb);
lb.setFont(font);
pan.add(tf,"North");
add(pan,"East");
openMI=new MenuItem("开始");
openMI.addActionListener(this);
fileMenu.add(openMI);
exitMI=new MenuItem("退出");
exitMI.addActionListener(this);
fileMenu.add(exitMI);
mbar.add(fileMenu);
mbar.add(fileMenu1);
setMenuBar(mbar);
setSize(1024,730);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
}
public void init(){
fileMenu1.addActionListener(new ActionListener(){//这里增加事件
public void actionPerformed(ActionEvent e){
dlg.setVisible(true);//显示对话框
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==exitMI)
System.exit(0);
if(e.getSource()==openMI)
System.out.println("open menu item");
}
public static void main(String[] args)
{
new PokerFrame();
}
} 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Dialogs extends JDialog{//制作对话框
public Dialogs(JFrame parent){
super(parent,"My dialog",true);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(new JLabel("Here is my dialog"));
JButton ok=new JButton("OK");
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
cp.add(ok);
setSize(150,125);
}
}
class PokerFrame extends JFrame implements ActionListener{
private Dialogs dlg=new Dialogs (null);
private MenuItem openMI, exitMI;
private Button b1=new Button("洗 牌");
private Button b2=new Button("发 牌");
private Button b3=new Button("猜大");
private Button b4=new Button("猜小");
private MenuBar mbar =new MenuBar();
private Menu fileMenu =new Menu("菜单");
private Menu fileMenu1 =new Menu("帮助");
PokerFrame()
{
Panel pan= new Panel();
pan.add(b1);
pan.add(b2);
pan.add(b3);
b3.setLocation(900, 56);
b3.setLocation(940, 56);
pan.add(b4);
TextField tf=new TextField("12");
Font font =new Font("Monospaced",Font.PLAIN,15);
tf.setFont(font);
Label lb= new Label("分 数:");
pan.add(lb);
lb.setFont(font);
pan.add(tf,"North");
add(pan,"East");
openMI=new MenuItem("开始");
openMI.addActionListener(this);
fileMenu.add(openMI);
exitMI=new MenuItem("退出");
exitMI.addActionListener(this);
fileMenu.add(exitMI);
mbar.add(fileMenu);
mbar.add(fileMenu1);
setMenuBar(mbar);
setSize(1024,730);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
}
public void init(){
fileMenu1.addActionListener(new ActionListener(){//这里增加事件
public void actionPerformed(ActionEvent e){
dlg.setVisible(true);//显示对话框
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==exitMI)
System.exit(0);
if(e.getSource()==openMI)
System.out.println("open menu item");
}
public static void main(String[] args)
{
new PokerFrame();
}
} 展开
1个回答
展开全部
我不是很清楚你的dialog监听是想加在哪?好像是想加在fileMenu1上,不过你把加载监听放到init里是走不到的,而且你的fileMenu1里连item都没有也加不了呀。我给你改了下:
public class PokerFrame extends JFrame implements ActionListener{
private Dialogs dlg=new Dialogs (null);
private MenuItem openMI, exitMI, helpMI;
private Button b1=new Button("洗 牌");
private Button b2=new Button("发 牌");
private Button b3=new Button("猜大");
private Button b4=new Button("猜小");
private MenuBar mbar =new MenuBar();
private Menu fileMenu =new Menu("菜单");
private Menu fileMenu1 =new Menu("帮助");
public PokerFrame()
{
Panel pan= new Panel();
pan.add(b1);
pan.add(b2);
pan.add(b3);
b3.setLocation(900, 56);
b3.setLocation(940, 56);
pan.add(b4);
TextField tf=new TextField("12");
Font font =new Font("Monospaced",Font.PLAIN,15);
tf.setFont(font);
Label lb= new Label("分 数:");
pan.add(lb);
lb.setFont(font);
pan.add(tf,"North");
add(pan,"East");
openMI=new MenuItem("开始");
openMI.addActionListener(this);
fileMenu.add(openMI);
exitMI=new MenuItem("退出");
exitMI.addActionListener(this);
fileMenu.add(exitMI);
helpMI=new MenuItem("帮助");
helpMI.addActionListener(this);
fileMenu1.add(helpMI);
mbar.add(fileMenu);
mbar.add(fileMenu1);
setMenuBar(mbar);
setSize(1024,730);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==exitMI)
System.exit(0);
if(e.getSource()==openMI)
System.out.println("open menu item");
if(e.getSource()==helpMI)
dlg.setVisible(true);//显示对话框
}
public static void main(String[] args)
{
new PokerFrame();
}
}
public class PokerFrame extends JFrame implements ActionListener{
private Dialogs dlg=new Dialogs (null);
private MenuItem openMI, exitMI, helpMI;
private Button b1=new Button("洗 牌");
private Button b2=new Button("发 牌");
private Button b3=new Button("猜大");
private Button b4=new Button("猜小");
private MenuBar mbar =new MenuBar();
private Menu fileMenu =new Menu("菜单");
private Menu fileMenu1 =new Menu("帮助");
public PokerFrame()
{
Panel pan= new Panel();
pan.add(b1);
pan.add(b2);
pan.add(b3);
b3.setLocation(900, 56);
b3.setLocation(940, 56);
pan.add(b4);
TextField tf=new TextField("12");
Font font =new Font("Monospaced",Font.PLAIN,15);
tf.setFont(font);
Label lb= new Label("分 数:");
pan.add(lb);
lb.setFont(font);
pan.add(tf,"North");
add(pan,"East");
openMI=new MenuItem("开始");
openMI.addActionListener(this);
fileMenu.add(openMI);
exitMI=new MenuItem("退出");
exitMI.addActionListener(this);
fileMenu.add(exitMI);
helpMI=new MenuItem("帮助");
helpMI.addActionListener(this);
fileMenu1.add(helpMI);
mbar.add(fileMenu);
mbar.add(fileMenu1);
setMenuBar(mbar);
setSize(1024,730);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==exitMI)
System.exit(0);
if(e.getSource()==openMI)
System.out.println("open menu item");
if(e.getSource()==helpMI)
dlg.setVisible(true);//显示对话框
}
public static void main(String[] args)
{
new PokerFrame();
}
}
追问
只有添加 itme才能显示吗 直接的menu 点击下 弹不出对话框的?
追答
menu的监听是当它的item被按下的时候触发的。没有item时就没办法触发的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询