随意创建一个JFrame窗口,再以它为父窗口创建一个JDialog,JDialog(Frame owner, String title, boolean modal)如果JDialog为模式对话框的话它上面的按钮的监听会失效(按上去没反应),而如果改为非模式对话框,监听就有反应了,这是为什么啊???

解决方案 »

  1.   

    不会,你写的代码有问题import javax.swing.*;
    import java.awt.event.*;
    public class Test {
      private JFrame frame = null;
      private JDialog dialog = null;
      private JButton button = null;
      private JButton b_Dialog = null;
      private JButton b_close = null;
      private JLabel label = null;
      private int i = 0;  public Test(){
        frame = new JFrame("JDialog");
        button = new JButton("测试窗口");
        button.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e) {
            dialogFrame();
          }
        });
        frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        frame.getContentPane().add(button);
        frame.setSize(300, 200);
        frame.setVisible(true);  }  public void dialogFrame(){
        i = 0;
        dialog = new JDialog(frame, true);
        JPanel pane = new JPanel();
        b_Dialog = new JButton("开始测试");
        b_close = new JButton("关闭窗口");
        label = new JLabel(i + "");
        b_Dialog.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e) {
            label.setText(++i + "");
          }
        });
        b_close.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e) {
            dialog.dispose();
          }
        });
        pane.add(b_Dialog);
        pane.add(label);
        pane.add(b_close);
        dialog.getContentPane().add(pane);
        dialog.setSize(230, 80);
        dialog.setVisible(true);
      }  public static void main(String[] args) {
        new Test();
      }
    }
      

  2.   

    哈,我发现原来setVisible(true);必须放在JDialog构造函数中所有监听的最后,否则没有效果的,谢谢了!!!!