簡體   English   中英

如何從按鈕關閉jdialog?

[英]how to close jdialog from a button?

我有一個Jframe(美因茨),

它有一個按鈕(showDialog),

當用戶點擊按鈕時

jdialog(Dialogz)將展示,

那個jdialog有一個按鈕

  • 如何從該按鈕關閉jdialog(在jdialog內)?
  • 我可以在創建它的實例后更改對話模式嗎?

我需要阻止該jdialog的所有者

繼續我試試......

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;


    public class Mainz extends JFrame implements ActionListener{
        JButton showDialog = new JButton("show dialog");

        public Mainz() {
            setLayout(new FlowLayout());
            showDialog.addActionListener(this);
            add(showDialog);
            setVisible(true);   
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            new Dialogz(this, true);
        }

        public static void main(String[]args){
            new Mainz();
    }
    }
    class Dialogz extends JDialog{
        JButton close = new JButton("close");

        public Dialogz(JFrame owner,boolean modal) {
            super(owner, modal);
            System.out.println(this.getModalityType());
            add(close);
            setLocationRelativeTo(owner);
            setVisible(true);

            close.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae){
                    closez();
                }
            });
        } 

        void closez(){
            setModal(false);
            this.dispose();
            System.out.println("Method Done");

        }
    }

非常感謝任何幫助

我可以在創建它的實例后更改對話模式嗎?

是的,你可以在運行時更改setModalModalityTypes ,但對於這種形式的代碼不會讓我任何意義

如何從該按鈕關閉jdialog(在jdialog內)?

在這種情況下,如果你要調用setVisibledispose()並不重要


  • 只創建一次JDialog

  • 將其創建為局部變量

  • 更改myDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); ,然后buttontoolbar (與X )隱藏JDialog

  • 那么你可以從actionPerformed調用myDialog.setVisible(true)ModalityType如果需要,setVisible應該包裝在invokeLater() ,而不是創建一個新實例( new Dialogz(this, true);

  • 放在JDialog JButton只會調用myDialog.setVisible(false)

  • 與您的邏輯對應的示例

有人告訴我..

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;


    public class Mainz extends JFrame implements ActionListener{
        JButton showDialog = new JButton("show dialog");

        public Mainz() {
            setLayout(new FlowLayout());
            showDialog.addActionListener(this);
            add(showDialog);
            setVisible(true);   
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            new Dialogz(this, false);
            setEnabled(false);
        }

        public static void main(String[]args){
            new Mainz();
    }
    }
    class Dialogz extends JDialog{
        JButton close = new JButton("close");


        public Dialogz(JFrame owner,boolean modal) {
            super(owner, modal);

            add(close);
            setLocationRelativeTo(owner);
            setVisible(true);

            close.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae){
                    closez();
                }
            });
        } 

        void closez(){
            System.out.println("before ="+getModalityType());
            setModal(true);
            System.out.println("after ="+getModalityType());
            getOwner().setEnabled(true);
            Dialogz.this.dispose();
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM