Java swing从JTextField获取输入 [英] Java swing getting input from a JTextField

查看:865
本文介绍了Java swing从JTextField获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多类似的问题,但是并没有解决我的问题.我是Java的新手.我试图从JTextField获取一些输入并将其作为String返回,以便可以将其用于不同类中的比较.我想这就是答案,我希望能够在类的任何其他部分中使用str.

I see something like this asked around a lot, but it hasn't answered my problem. I am fairly new at Java. I am trying to get some input from a JTextField and return it as a String so I can use it for comparison in a different class. This is what I see as an answer, I'd like to be able to use str in any other part of the class.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class ClassFrame extends JFrame {

private static final long serialVersionUID = 2451829341034438685L;

public static JButton inputButton = new JButton("Send");
public static JTextArea editTextArea = new JTextArea("Type Here!");
public static JTextArea uneditTextArea = new JTextArea();

public ClassFrame(String title) {
    //SET LAYOUT MANAGER (How it arranges components)
setLayout(new BorderLayout());
//////CREATE SWING COMPONENTS////////////
//OUTPUT TEXT AREA
uneditTextArea.setEditable(false);

//INPUT TEXT AREA
editTextArea.setBackground(Color.BLUE);
editTextArea.setForeground(Color.WHITE);

//SET CONTENT PANE
Container c = getContentPane();

//ADD COMPONENTS TO CONTENT PANE        
c.add(uneditTextArea, BorderLayout.CENTER);
c.add(editTextArea, BorderLayout.SOUTH);
c.add(inputButton, BorderLayout.WEST);

ClassFrame.inputButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        String str = editTextArea.getText();
        editTextArea.setText(" ");
        System.out.println(str);                
    }
});
}
}

推荐答案

查看我的评论.

package applet;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;


public class ClassFrame extends JFrame {


    private static final long serialVersionUID = 2451829341034438685L;

    public static JButton inputButton = new JButton("Send");
    public static JTextArea editTextArea = new JTextArea("Type Here!");
    public static JTextArea uneditTextArea = new JTextArea();

    // MA - Your String, defined here and usable throughout the class

    private String myString;

    public ClassFrame(String title) {

        // MA - Indent your code properly so that it's more readable to both you
        // and others

        //SET LAYOUT MANAGER (How it arranges components)
        setLayout(new BorderLayout());
        //////CREATE SWING COMPONENTS////////////
        //OUTPUT TEXT AREA
        uneditTextArea.setEditable(false);

        //INPUT TEXT AREA
        editTextArea.setBackground(Color.BLUE);
        editTextArea.setForeground(Color.WHITE);

        //SET CONTENT PANE
        Container c = getContentPane();

        //ADD COMPONENTS TO CONTENT PANE        
        c.add(uneditTextArea, BorderLayout.CENTER);
        c.add(editTextArea, BorderLayout.SOUTH);
        c.add(inputButton, BorderLayout.WEST);

        ClassFrame.inputButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                // MA - Using the class field myString to receive text from text area

                myString = editTextArea.getText();

                // MA - Don't use a space when you actually want an empty string.
                // As it stands, your code will test for a single space character.
                // You really want it to test whether the text area is empty.

                //editTextArea.setText(" ");

                // MA - Do this instead.  An empty string means the text area has
                // no input at all.

                editTextArea.setText("");

                System.out.println(myString);                
            }
        });
    }
}

我建议您对Java的变量作用域进行一些阅读.您可以使用它.

I would suggest you do some reading on variable scoping in Java. You can Google it.

这篇关于Java swing从JTextField获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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