请问各位,如何实现在Main窗口中点击按钮弹出用户另一个窗口RequestValueDialog,让用户在两个textfield中输入数据,点击OK后将输入数据回传给原来窗口显示在文本框中?
因为类中有其他方法,可以不使用静态变量吗?
貌似不能传图片,那就传简单的示例代码。
Main.javapublic class Main extends JFrame (){
        private JPanel contentPane;
private JButton btnClick;
private JTextField textField;
        public static void main(String[] args) {
                new Main().setVisible(true);
        }
        public Main() {
initGUI();
}
private void initGUI() {
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(new BorderLayout(0, 0));

btnClick = new JButton("click");
contentPane.add(btnClick, BorderLayout.NORTH);

textField = new JTextField();
contentPane.add(textField, BorderLayout.CENTER);
}
}RequestValueDialog.javapublic class  RequestValueDialog extends JDialog(){
        public RequestValueDialog() {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
{
textField = new JTextField();
contentPanel.add(textField);
textField.setColumns(10);
}
{
textField_1 = new JTextField();
textField_1.setColumns(10);
contentPanel.add(textField_1);
}
{
textField_2 = new JTextField();
textField_2.setColumns(10);
contentPanel.add(textField_2);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
}

解决方案 »

  1.   

    M-VC 
    共享 一个 PlainDocument
      

  2.   

    JDialog本身有构造方法new JDialog(Frame owner, String title, boolean modal),你直接写  public RequestValueDialog(Frame owner, String title, boolean modal) {
        super(owner,title, modal);
        ………………
    }
    就行了
      

  3.   

    第一个JTextField的内容可以回显到主Frame上面,你参考看看吧
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;public class Main extends JFrame implements ActionListener {
        private JPanel contentPane;
        private JButton btnClick;    public JTextField getTextField() {
            return textField;
        }    public void setTextField(JTextField textField) {
            this.textField = textField;
        }    private JTextField textField;
        
        public static void main(String[] args) {
            new Main().setVisible(true);
        }
        public Main() {
            initGUI();
        }
        private void initGUI() {
            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(new BorderLayout(0, 0));        btnClick = new JButton("click");
            btnClick.addActionListener(this);
            contentPane.add(btnClick, BorderLayout.NORTH);        textField = new JTextField();
            contentPane.add(textField, BorderLayout.CENTER);
        }    @Override
        public void actionPerformed(ActionEvent e) {
            //To change body of implemented methods use File | Settings | File Templates.
            if(e.getSource()==btnClick){
                RequestValueDialog dialog=new RequestValueDialog(this);            dialog.setVisible(true);
               
            }
        }
    }
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;public class  RequestValueDialog extends JDialog implements ActionListener {
        private JPanel contentPanel=new JPanel();
        private JPanel buttonPane=null;
        private JTextField textField=null;
        private JTextField textField_1=null;    public JTextField getTextField() {
            return textField;
        }    public JTextField getTextField_1() {
            return textField_1;
        }    public JTextField getTextField_2() {
            return textField_2;
        }    private JTextField textField_2=null;
        private JButton okButton=null;
        private JButton cancelButton=null;
        private JFrame parent=null;    public RequestValueDialog(JFrame parent) {
            this.parent=parent;
            setBounds(100, 100, 450, 300);
            getContentPane().setLayout(new BorderLayout());
            contentPanel.setLayout(new FlowLayout());
            contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
            getContentPane().add(contentPanel, BorderLayout.CENTER);        textField = new JTextField();
            contentPanel.add(textField);
            textField.setColumns(10);        textField_1 = new JTextField();
            textField_1.setColumns(10);
            contentPanel.add(textField_1);        textField_2 = new JTextField();
            textField_2.setColumns(10);
            contentPanel.add(textField_2);        buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);        okButton = new JButton("OK");
            okButton.addActionListener(this);
            okButton.setActionCommand("OK");
            buttonPane.add(okButton);
            getRootPane().setDefaultButton(okButton);        cancelButton = new JButton("Cancel");
            cancelButton.setActionCommand("Cancel");
            buttonPane.add(cancelButton);
        }    @Override
        public void actionPerformed(ActionEvent e) {
            //To change body of implemented methods use File | Settings | File Templates.
            if(e.getSource()==okButton){
                 Main main=(Main)parent;
                 main.getTextField().setText(textField.getText());
                 main.getTextField().updateUI();
                 dispose();
            }
        }
    }
      

  4.   

    感谢still_rain和ioe_gaoyong,已经成功。