简体   繁体   English

从JTextfield获取输入

[英]Get input from JTextfield

I was able to make the code do what i want except for the fact i can't get input from this now. 我能够使代码执行我想要的操作,但事实是我现在无法从中获取输入。 What i want to do now is still get text from the JTextfield and return it to the main method that creates the frame object. 我现在想做的仍然是从JTextfield获取文本,并将其返回给创建框架对象的main方法。 Can somebody help me get the input from the following code? 有人可以帮我从以下代码中获取输入吗?

 package assignment5;

    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class Frame2 extends JDialog implements ActionListener 
    {

        private String a;
        private JTextField field = new JTextField(30); 

        public Frame2(JFrame parent, String title, String message) 
        {
            super(parent, title, true);

            if (parent != null) 
            {
                this.setSize(500,150);
                this.setLocation(400,200);
            }

            JPanel messagePane = new JPanel();
            messagePane.add(new JLabel(message));
            getContentPane().add(messagePane, BorderLayout.PAGE_START);

            JPanel buttonPane = new JPanel();
            JPanel button2Pane = new JPanel();

            JButton button = new JButton("OK"); 
            JButton button2 = new JButton("Cancel");

            buttonPane.add(button); 
            button2Pane.add(button2);

            getContentPane().add(buttonPane, BorderLayout.PAGE_END);
            getContentPane().add(button2Pane,BorderLayout.EAST);

            //button.addActionListener(this);
            button.addActionListener(new ActionListener()
            {
                @Override 
                public void actionPerformed( ActionEvent event)
                {
                    parent.dispose();
                }
            });

            button2.addActionListener(new ActionListener()
            {
                @Override 
                public void actionPerformed( ActionEvent event)
                {
                    parent.dispose();
                }
            });

            JPanel textPane = new JPanel();
            JTextField field = new JTextField(30); 
            textPane.add(field);
            getContentPane().add(textPane, BorderLayout.CENTER);

            field.addActionListener(this);


            setDefaultCloseOperation(DISPOSE_ON_CLOSE);

            //pack(); 
            setVisible(true);
}


        public void b ()
        {
            a = field.getText();
        }

        public String g ()
        {
            System.out.println("wasdfsdf" + a);
            return a;
        }

        @Override
        public void actionPerformed(ActionEvent event) 
        {
            a = field.getText();
            System.out.println("wasdfsdfsafddsfsdfsdfsafdsggsdfsdf" + a);
            // TODO Auto-generated method stub

        }

    }

You're creating a non-modal JFrame when you really want to instead use a modal JDialog. 当您真正想使用模式JDialog时,您正在创建非模式JFrame。 Change the MyFrame2 to a modal JDialog and your problems are solved. 将MyFrame2更改为模式JDialog,即可解决问题。

Details as to why: 有关原因的详细信息:

  • When you create and display a non-modal JFrame, program flow continues immediately in the calling code below the code where you display the JFrame. 当创建并显示非模式JFrame时,程序流程将立即在显示JFrame的代码下方的调用代码中继续。
  • This means that you'll be trying to extract data from the JTextField before the user's had nary a chance to add information to it. 这意味着您将在用户没有机会向其添加信息之前尝试从JTextField提取数据。
  • With a modal JDialog on the other hand, code flow halts in the calling code immediately when you display the JDialog, and it does not resume until the JDialog is no longer visible. 另一方面,使用模式JDialog时,在显示JDialog时,代码流立即停止在调用代码中,并且直到JDialog不再可见时它才恢复。
  • Thus the code below where you display the dialog won't get called until the dialog has been dealt with by the user, and now hopefully the JTextField in the dialog will have useful information in it. 因此,显示对话框的下面的代码只有在用户处理完对话框后才会被调用,现在希望对话框中的JTextField会包含有用的信息。

Edit 编辑
I'm surprised that this will even compile: 我很惊讶它甚至可以编译:

many = Integer.parseInt()

You're parsing nothing on this line, and that won't fly. 您在此行上没有进行任何解析,并且不会运行。

You would want to give your MyFrame2 a public method that extracts the String from its JTextField and then call the method after displaying it. 您可能想给MyFrame2一个公共方法,该方法从其JTextField中提取String,然后在显示它之后调用该方法。 Something like: 就像是:

public String getMyTextFieldText() {
    return myTextField.getText();
}

And then in the calling code: 然后在调用代码中:

MyFrame2 myFrame2 = new MyFrame2();
myFrame2.setVisible(true);

String text = myFrame2.getMyTextFieldText();
if (text != null && !text.trim().isEmpty()) {
  int someNumber = Integer.parseInt(text);
}

while modality did help to prevent the dialogs from appearing at the same time however that did not solve my problem I had to add the following lines to make sure that it did what I wanted: 虽然模态确实有助于防止对话框同时出现,但是这并不能解决我的问题,因此我不得不添加以下几行以确保其符合我的要求:

public void actionPerformed(ActionEvent event) 
    {
        setTexts(field.getText());
    }

    public String getTexts() {
        return texts;
    }

    public void setTexts(String text) {
        this.texts = text;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM