Java 使用JTextField进行用户输入
谢谢你们的帮助,伙计们…现在程序正常运行了。。但我还有两个问题。 1.如何将输出输入JTestField t4或t5 2.如何使用JButton Buton3关闭应用程序Java 使用JTextField进行用户输入,java,swing,jframe,jbutton,jtextfield,Java,Swing,Jframe,Jbutton,Jtextfield,谢谢你们的帮助,伙计们…现在程序正常运行了。。但我还有两个问题。 1.如何将输出输入JTestField t4或t5 2.如何使用JButton Buton3关闭应用程序 import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.JButton; import javax.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TriangleFarfan{
JFrame Triangle = new JFrame("Triangle Calculator");
JButton Button1 = new JButton ("Area");
JButton Button2 = new JButton("Perimeter");
JButton Button3 = new JButton("Close");
JTextField t1 = new JTextField(20);
String t1TextBox = t1.getText();
double side1 = Double.parseDouble(t1TextBox);
JPanel j1 = new JPanel (new FlowLayout());
JLabel l1 = new JLabel("Enter side 1:");
JTextField t2 = new JTextField();
String t2TextBox = t2.getText();
double side2 = Double.parseDouble(t2TextBox);
JPanel j2 = new JPanel (new FlowLayout());
JLabel l2 = new JLabel("Enter side 2:");
JTextField t3 = new JTextField();
String t3TextBox = t3.getText();
double side3 = Double.parseDouble(t3TextBox);
JPanel j3 = new JPanel (new FlowLayout());
JLabel l3 = new JLabel("Enter side 3:");
JTextField t4 = new JTextField();
JPanel j4 = new JPanel (new FlowLayout());
JLabel l4 = new JLabel("Area Result");
JTextField t5 = new JTextField(20);
JPanel j5 = new JPanel (new FlowLayout());
JLabel l5 = new JLabel("Perimeter Result");
public TriangleFarfan()
{
j1.add(l1);
j1.add(t1);
j2.add(l2);
j2.add(t2);
j3.add(l3);
j3.add(t3);
j4.add(l4);
j4.add(t4);
j5.add(l5);
j5.add(t5);
Triangle.add(j1);
Triangle.add(j2);
Triangle.add(j3);
Triangle.add(j4);
Triangle.add(j5);
Triangle.add(Button1);
Button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
double Area = (side1 * side2)/2;
//Execute when button is pressed
System.out.println(Area);
}
});
Triangle.add(Button2);
Button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("You clicked the Perimeter Button");
}
});
Triangle.add(Button3);
Button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("You clicked the Close Button");
}
});
Triangle.setLayout(new FlowLayout());
Triangle.setSize(450,400);
Triangle.setVisible(true);
Triangle.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}添加
main
方法:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TriangleFarfan();
}
});
}
宣言
JTextField t1 = new JTextField(20);
不将JTextField
中的值设置为20
。相反,它设置JTextComponent
的列数,但带有空字符串。所以这条线
double side1 = Double.parseDouble(t1TextBox);
将在启动时抛出一个NumberFormatException
。除了缺少一个主方法之外,正如Reimeus指出的,您的指令顺序是错误的。您试图在屏幕上显示任何内容之前,甚至在创建对象之前读取用户输入。例如,这一行:
String t1TextBox=t1.getText()代码>
尝试从尚未添加到尚未创建的面板的文本框中获取文本
要解决这个问题,您需要重新思考程序的逻辑。以下是一些提示:
- 避免方法之外的赋值。而不是写作
JFrame Triangle = new JFrame("Triangle Calculator");
在类主体中声明变量,如下所示:
JFrame Triangle;
Triangle = new JFrame("Triangle Calculator");
Button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// get the size of side1 from the textbox
String t1TextBox = t1.getText();
double side1 = Double.parseDouble(t1TextBox);
// get the size of side2 from the textbox
String t2TextBox = t2.getText();
double side2 = Double.parseDouble(t2TextBox);
// now we can calculate the area
double Area = (side1 * side2)/2;
//Execute when button is pressed
System.out.println(Area);
}
并在构造函数中指定它,如下所示:
JFrame Triangle;
Triangle = new JFrame("Triangle Calculator");
Button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// get the size of side1 from the textbox
String t1TextBox = t1.getText();
double side1 = Double.parseDouble(t1TextBox);
// get the size of side2 from the textbox
String t2TextBox = t2.getText();
double side2 = Double.parseDouble(t2TextBox);
// now we can calculate the area
double Area = (side1 * side2)/2;
//Execute when button is pressed
System.out.println(Area);
}
- 构建整个UI,然后担心侦听器。通过这种方式,您可以确保在获取用户输入时没有引用不存在的UI元素
- 在侦听器中获取用户输入,如下所示:
JFrame Triangle;
Triangle = new JFrame("Triangle Calculator");
Button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// get the size of side1 from the textbox
String t1TextBox = t1.getText();
double side1 = Double.parseDouble(t1TextBox);
// get the size of side2 from the textbox
String t2TextBox = t2.getText();
double side2 = Double.parseDouble(t2TextBox);
// now we can calculate the area
double Area = (side1 * side2)/2;
//Execute when button is pressed
System.out.println(Area);
}
}))
感谢您的快速重播,但一旦我测试了无法打开的程序,它几乎会立即关闭。代码有什么问题吗?这是因为您试图解析JTextField
中的空字符串。看看这条线索。它显示了应用程序失败的地方。谢谢你的帮助,再见。我遵照你的指示,程序运行得很好。