代码块里面的注释问题,以及文章里面的问题,详解必采纳。关于Swing容器里面的JDialog使用


package JAVA;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Label;
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.JRadioButton;
import javax.swing.JScrollBar;

public class moni {

    public static void main(String[] args) {
        JFrame jr=new JFrame("Dialogdemo");
        jr.setSize(300, 200);
        jr.setLocation(300, 200);
        jr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jr.setVisible(true);
        jr.setLayout(new FlowLayout());
        JButton btn1=new JButton("模态对话框");
        JButton btn2=new JButton("非模态对话框");
        jr.add(btn1);
        jr.add(btn2);
        //下面为什么都用final修饰?
        final JLabel label=new JLabel();
        final JDialog dialog=new JDialog(jr, "Dialog");
        
        dialog.setSize(200, 320);
        dialog.setVisible(true);
        dialog.setLocation(350, 210);
        dialog.setLayout(new FlowLayout());
        final JButton btn3=new JButton("确定");
        dialog.add(btn3);
        //为什么为按钮一的模态对话框设置单击点击事件
        btn1.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                //为什么在这里设置对话框为静态?
                dialog.setModal(true);
                // 如果JDialog窗口中没有添加了JLabel标签,就把JLabel标签加上
               if (dialog.getComponents().length == 1) {
                   dialog.add(label);
               }
                label.setText("模态对话框,堆积确定按钮关闭");
                dialog.setVisible(true);
            }
        });
        
btn2.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                //为什么在这里设置对话框为静态?
                dialog.setModal(true);
                // 如果JDialog窗口中没有添加了JLabel标签,就把JLabel标签加上
               if (dialog.getComponents().length == 1) {
                   dialog.add(label);
               }
                label.setText("非模态对话框,堆积确定按钮关闭");
                dialog.setVisible(true);
            }
        });
        //为对话框中的按钮添加单击事件
        btn3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        });

    }

}

img


在这里为什么我的按钮三还显示在外面啊?我要的结果最后也是出来的,但是按钮三又出现在外面是怎么回事?我是想要点击按钮一或者按钮二才让他显示出来,如下

img


但是我结果是可以的,只是按钮三又出现在外面是怎么回事?