簡體   English   中英

如何從 JFrame 獲取 JDialog textFiels 值

[英]How to get JDialog textFiels Value from JFrame

嗨,我是 Java 編碼新手,並試圖在 JFrame 和 JDialog 的幫助下設計一個用戶友好的桌面應用程序 -

我的 JFrame 是 -

package com.myapp.ui;
    
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JCheckBox;

public class MyFrame extends JFrame { 

private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MyFrame frame = new MyFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MyFrame() {

        setTitle("MyFrame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JButton btnClickMe = new JButton("Click Me");
        btnClickMe.setBounds(271, 171, 115, 29);
        contentPane.add(btnClickMe);
        
        JCheckBox chckbxOpenDialog = new JCheckBox("Open Dialog");      
        chckbxOpenDialog.setBounds(25, 171, 139, 29);
        contentPane.add(chckbxOpenDialog);
        
        chckbxOpenDialog.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(chckbxOpenDialog.isSelected()== true){
                    MyDialog MD = new MyDialog();
                    MD.setModal(true);
                    MD.setVisible(true);
                }
            }
        });
        
        btnClickMe.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                 //Here I want to get the value of textFieldName and textFieldEmail from JDialog
                 //after Click on Confirm Button in JDialog and closing/disposing it
            }
        });
    }
}

我的 JDialog 是 -

package com.myapp.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MyDialog extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField textFieldName;
    private JTextField textFieldEmail;
    private JButton btnConfirm;

    public static void main(String[] args) {
        try {
            MyDialog dialog = new MyDialog();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public MyDialog() {
        setTitle("MyDialog");
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        
        textFieldName = new JTextField();
        textFieldName.setBounds(108, 26, 146, 26);
        contentPanel.add(textFieldName);
        textFieldName.setColumns(10);
        
        textFieldEmail = new JTextField();
        textFieldEmail.setBounds(108, 68, 146, 26);
        contentPanel.add(textFieldEmail);
        textFieldEmail.setColumns(10);
        
        btnConfirm = new JButton("Confirm");
        btnConfirm.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //passing the Name and Email field value in JFrame
            }
        });
        btnConfirm.setBounds(132, 141, 115, 29);
        contentPanel.add(btnConfirm);
        
        MyFrame MF = new MyFrame();
        
    }
}

JFrame 和 JDialog 都在同一個包中。 我試圖獲得價值

從 JDialog 到 JFrame 的 textFieldName 和 textFieldEmail

如果有人能以最好的方式指導我,那就太好了。

你可以:

  1. 將 setter 添加到 MyFrame
  2. ActionListener創建時將MyFrame引用傳遞給MyDialog (使用this
  3. MyDialogActionListener中設置MyFrame字段

可能有更好的方法來做到這一點。 內置的JOptionPane生成等待用戶輸入的模式對話框窗口。 這些會比將 JFrame 的引用傳遞給 JDialog 更好。

另請注意, JavaFX已在很大程度上取代了桌面 UI 的 Java Swing

暫無
暫無

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

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