处理后JDialog中的JTextField保留值

时间:2012-03-13 20:45:12

标签: java swing actionlistener jtextfield jdialog

我遇到了JTextField问题。我需要从JTextField获取文本,然后在JDialog中修改它。我正在为JTextField使用相同的变量。只要您不进入对话框,打印的字符串就是您在文本字段中输入的内容。如果在对话框中输入一个字符串,它将只打印该字符串,直到您在对话框中再次更改它(使主文本字段无效)。我可以通过添加一个单独的变量来解决这个问题,但是我想尝试避免不必要的声明。我的印象是,这应该无关紧要,因为我正在创建一个新的JTextField对象并且还处理对话框。我错过了什么吗?有什么想法吗?

这是我的问题的模拟。

import java.awt.event.*;
import javax.swing.*;

public class textfield extends JPanel {

    private JTextField textfield;
    private JButton printButton, dialogButton, okayButton;
    private static JFrame frame;

    public static void main(String[] args) {
        frame = new JFrame();
        frame.setSize(200,200);
        frame.getContentPane().add(new textfield());
        frame.setVisible(true);
    }

    private textfield() {
        textfield = new JTextField(10);
        add(textfield);
        ((AbstractButton) add(printButton = new JButton("Print"))).addActionListener(new printListener());
        ((AbstractButton) add(dialogButton = new JButton("Dialog"))).addActionListener(new dialogListener());

    }

    private class printListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String string = null;
            string = textfield.getText();
            System.out.println(string);
        }
    }

    private class dialogListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            final JDialog dialog = new JDialog(frame, "Dialog", true);
            JPanel p = new JPanel();
            textfield = new JTextField(10);
            p.add(textfield);
            p.add(okayButton = new JButton(new AbstractAction("Okay") {
                public void actionPerformed(ActionEvent e) {
                    String string = null;
                    string = textfield.getText();
                    System.out.println(string);
                    dialog.dispose();
                }
            }));
            dialog.add(p);
            dialog.pack();
            dialog.setVisible(true);

        }
    }   
}

2 个答案:

答案 0 :(得分:2)

您需要在对话框中创建JTextField,因为当您使用一个文本字段时,您的主面板将指向在按下okey按钮时销毁的子对话框中创建的新JTextField(销毁其所有组件)。所以不要将面板文本字段指针更改为处理窗口中的新文本字段对象。

答案 1 :(得分:1)

您的变量私有JTextField textfield;使用了两次,首先用于JFrame,第二次用于JDialod,稍微改变一下..

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MyTextfield extends JPanel {

    private static final long serialVersionUID = 1L;
    private JTextField textfield, textfield1; //added new variable
    private JButton printButton, dialogButton, okayButton;
    private static JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {//added initial thread

            @Override
            public void run() {
                frame = new JFrame();
                frame.setSize(200, 200);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//added default close operation
                frame.getContentPane().add(new MyTextfield());
                frame.setVisible(true);
            }
        });
    }

    private MyTextfield() {
        textfield = new JTextField(10);
        add(textfield);
        ((AbstractButton) add(printButton = new JButton("Print"))).addActionListener(new printListener());
        ((AbstractButton) add(dialogButton = new JButton("Dialog"))).addActionListener(new dialogListener());

    }

    private class printListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String string = null;
            string = textfield.getText();
            System.out.println(string);
        }
    }

    private class dialogListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            final JDialog dialog = new JDialog(frame, "Dialog", true);
            JPanel p = new JPanel();
            textfield1 = new JTextField(10);
            p.add(textfield1);
            p.add(okayButton = new JButton(new AbstractAction("Okay") {

                private static final long serialVersionUID = 1L;

                public void actionPerformed(ActionEvent e) {
                    String string = null;
                    textfield.setText(textfield1.getText());
                    System.out.println(string);
                    dialog.dispose();
                }
            }));
            dialog.add(p);
            dialog.pack();
            dialog.setVisible(true);
        }
    }
}