java 如何从 JTextField 获取输入,然后将其存储在变量中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28735798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:05:34  来源:igfitidea点击:

How Do I Get Input From A JTextField, Then Store It In A Variable?

javaswinginputlistener

提问by Mrunited12

I've setup a JTextField, for users to input the base of the triange (It's a triangle area calculator) and I need to know how to store their input into a variable.

我已经设置了一个 JTextField,供用户输入三角形的底数(它是一个三角形面积计算器),我需要知道如何将他们的输入存储到一个变量中。

I tried an action listener but it game an error.

我尝试了一个动作监听器,但它游戏出错了。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.*;

public class Triangle extends JFrame implements ActionListener{

static Scanner sc = new Scanner(System.in);

public static void findingTriangle(){

    JFrame jf = new JFrame();
    JTextField textfield = new JTextField("", 30);
    JPanel panel = new JPanel();
    JLabel jl = new JLabel("Triangle Area Calculator");
    jf.setSize(500, 500);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.add(panel);
    panel.add(jl);
    panel.add(textfield);
    }

    public static void main(String[] args){

        findingTriangle();

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}

回答by Jens

Define a button and add the actionListener.

定义一个按钮并添加 actionListener。

Define textfield and your variable as static fields and assign textarea.getText()to your variable:

将文本字段和您的变量定义为静态字段并分配textarea.getText()给您的变量:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Triangle extends JFrame implements ActionListener {

    static Scanner sc = new Scanner(System.in);
    static String s;
    static JTextField textfield;
    static JButton jButton;

    public static void findingTriangle() {

        JFrame jf = new JFrame();
        textfield = new JTextField("", 30);
        jButton = new JButton("Click");
        jButton.addActionListener(new Triangle());
        JPanel panel = new JPanel();
        JLabel jl = new JLabel("Triangle Area Calculator");
        jf.setSize(500, 500);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(panel);
        panel.add(jl);
        panel.add(textfield);
        panel.add(jButton);
    }

    public static void main(String[] args) {

        findingTriangle();

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        s = textfield.getText();

    }
}

回答by MadProgrammer

You can attach an ActionListenerto the JTextFieldwhich, when the user presses the Enterbutton, will trigger the actionPerfomedmethod.

您可以将 附加ActionListenerJTextField,当用户按下Enter按钮时,将触发该actionPerfomed方法。

textfield.addActionListener(this);

From there, you can extract the value entered by the user using JTextField#getText.

从那里,您可以使用 提取用户输入的值JTextField#getText

String value = textField.getText();

You could also add JButtonto the UI and attach the same ActionListenerto it, as it would give a better visual cue to the user

您还可以添加JButton到 UI 并将相同的内容附加ActionListener到它,因为它会给用户提供更好的视觉提示

I would also suggest you use a JSpinneror JFormattedTextFieldinstead, as they have functionality to validate the data automatically.

我还建议您使用 a JSpinnerorJFormattedTextField代替,因为它们具有自动验证数据的功能。

Have a look at How to Use Text Fields, How to Use Formatted Text Fields, How to Use Spinnersand How to Write an Action Listenersfor more details

查看如何使用文本字段如何使用格式化文本字段如何使用微调器如何编写动作侦听器以获取更多详细信息