首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

java 自定义对话框的有关问题

2012-01-05 
java 自定义对话框的问题这是我写的一个自定义的对话框,但是为什么在事件处理的时候没有反应呢?我点击那2

java 自定义对话框的问题
这是我写的一个自定义的对话框,但是为什么在事件处理的时候没有反应呢?我点击那2个JButton都不管用。是怎么回事呢?求助。。

Java code
import java.awt.Container;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class selfDefinedDialog1 implements ActionListener{    JFrame f;    JDialog dialog;    public selfDefinedDialog1(JFrame f){        this.f= f;        dialog=new JDialog(f,"自定义对话框",true);        Container cp=dialog.getContentPane();        cp.setLayout(new FlowLayout());        JLabel lb1=new JLabel("你是2b");        JButton yes=new JButton("yes"),no=new JButton("no");        JPanel jpl=new JPanel(new FlowLayout());        jpl.add(lb1);        jpl.add(yes);        jpl.add(no);        cp.add(jpl);        dialog.setBounds(150,150,300,300);        dialog.setVisible(true);        no.addActionListener(this);        yes.addActionListener(this);    }    public void actionPerformed(ActionEvent e){        String str=e.getActionCommand();        if(str.equals("yes")){            Graphics g = null;            g.drawString("汉昌宁是2b", 100, 100);        }        else if(str.equals("no")) {            //dialog.dispose();            System.exit(0);        }            }}


[解决办法]
你的事件不响应 
是因为
setVisible(true);
这个写在了添加事件之前,所以展示出来的窗口的UI没有完全刷新
记住将setVisible(true)放在最后就好了
这样,你的代码,这样改就行了
Java code
        no.addActionListener(this);        yes.addActionListener(this);        addWindowListener(new MyWindowAdapter2());        setVisible(true); 

热点排行