当前位置: 代码迷 >> Java相关 >> java 自定义对话框的有关问题
  详细解决方案

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

热度:1067   发布时间:2013-02-25 21:50:10.0
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);
  相关解决方案