[英]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. 我还建议您改用
JSpinner
或JFormattedTextField
,因为它们具有自动验证数据的功能。
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.