JDialog中的JTextField处理后保留值 [英] JTextField in JDialog retaining value after dispose

查看:88
本文介绍了JDialog中的JTextField处理后保留值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having a JTextField problem. I am needing to get text from a JTextField and modify it later in a JDialog. I am using the same variable for the JTextField. As long as you don't enter the dialog, the string printed is whatever you entered in the text field. If you enter a string in the dialog, it will only print that string until you change it again in the dialog (renders the primary text field useless). I can fix this by adding a separate variable, but would like to try to avoid unnecessary declarations. I was under the impression that this shouldn't matter since I'm creating a new JTextField object and also disposing of the dialog. Am I missing something? Any thoughts?

这里是我的问题的模型.

Here is a mock up of my problem.

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);

        }
    }   
}

推荐答案

您需要在对话框内创建JTextField,因为当您使用一个文本字段时,主面板将指向在子对话框中创建的新JTextField.按下okey按钮时将其丢弃(销毁了其所有组件).因此请勿在已布置的窗口中将面板文本字段指针更改为新的文本字段对象.

you need to make JTextField inside the dialog, because when you are using one textfield, your main panel will point to new JTextField that was created in child dialog that was disposed when you press okey button (destroyed all its components). so don't change panel textfield pointer to new textfield object in disposed window.

这篇关于JDialog中的JTextField处理后保留值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆