【问题标题】:JTextField in JDialog retaining value after disposeJDialog中的JTextField在处理后保留值
【发布时间】:2012-03-13 20:45:12
【问题描述】:

我遇到了 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);

        }
    }   
}

【问题讨论】:

    标签: java swing actionlistener jtextfield jdialog


    【解决方案1】:

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

    【讨论】:

    • 啊,所以基本上每次新建一个同名的JTextField,程序都会指向最后一个创建的不管位置等等?
    • 是的,如果您使用相同的变量。所以在对话框中新建一个。所以每个人都会有自己的一生。
    【解决方案2】:

    您的变量 private 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);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      相关资源
      最近更新 更多