我是用netbeans GUI设计器做的一个JFrame,然后点击一个按钮,弹出子对话框,这个对话框在JFrame框口intite的时候就已经存在(设计器在JFrame中添加的额外组件),在这种情况下,怎么使自对话框模态化,或者说让子对话框总在JFrame窗口的前面?

解决方案 »

  1.   

    JDialog dlg = new JDialog(frame);
    dlg.setModel(false);          //设置非模态,后台还能相应
    dlg.show();
      

  2.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class Test { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    TestFrame frame = new TestFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }}class TestFrame extends JFrame {
    public TestFrame() {
    setTitle("ButtonTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    TestPanel panel = new TestPanel();
    add(panel);
    d = new JDialog(this);
    d.setSize(300, 200);
    }
    public static final int DEFAULT_WIDTH = 600;
    public static final int DEFAULT_HEIGHT = 400;
    private JDialog d;

    class TestPanel extends JPanel {
    public TestPanel() {
    JButton button = new JButton("model dialog");
    add(button);
    button.addActionListener(new TestActionListener());
    }
    }

    class TestActionListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
    d.setModal(true);
    d.setVisible(true);
    }
    }
    }
      

  3.   

    JDialog dlg = new JDialog(frame,true);