简体   繁体   English

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

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

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

    }
}

Define a button and add the actionListener. 定义一个按钮并添加actionListener。

Define textfield and your variable as static fields and assign textarea.getText() to your variable: 将textfield和变量定义为静态字段,并将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();

    }
}

You can attach an ActionListener to the JTextField which, when the user presses the Enter button, will trigger the actionPerfomed method. 您可以将ActionListener附加到JTextField ,当用户按下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 JButton to the UI and attach the same ActionListener to it, as it would give a better visual cue to the user 您还可以将JButton添加到UI并附加相同的ActionListener ,因为它将为用户提供更好的视觉提示

I would also suggest you use a JSpinner or JFormattedTextField instead, as they have functionality to validate the data automatically. 我还建议您改用JSpinnerJFormattedTextField ,因为它们具有自动验证数据的功能。

Have a look at How to Use Text Fields , How to Use Formatted Text Fields , How to Use Spinners and How to Write an Action Listeners for more details 有关更多详细信息,请参阅如何使用文本字段如何使用带格式的文本字段如何使用微调框以及如何编写动作侦听器

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

相关问题 将来自JTextField的输入存储在Variable中 - Store input from JTextField in Variable 如何从其他面板从 JTextField 获取输入 - How do I get input from JTextField from other panels 如何从JTextField获取用户输入? - How do I get user input from a JTextField? 如何将用户输入字符串(jtextfield)更改为int变量? - how do I change a user input string (jtextfield) into an int variable? 如何将文本输入从一个JTextField传递到另一个类中的变量? - How do I pass text input from one JTextField to a variable in another class? 我如何获得多个双输入分隔的; 从一个JtextField并将其存储在一个数组上? - How do i get multiple double inputs separated by ; from one JtextField and store them on an array? 如何从一个类中的JtextField输入到另一个类中的ArrayList的输入。 爪哇 - How do I get input from a JtextField in one class to an ArrayList in another class. Java 如何将值从JTextField存储到局部变量? - How to store value from JTextField to a local variable? 如何获取从 JTextField 收集的变量并将其提供给 getter 方法? - How do I get a variable collected from a JTextField and give it to a getter method? 如何将函数作为Java中JTextField的输入? - How do I take a function as an input from a JTextField in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM