使用JTextField进行用户输入 [英] Using JTextField for user input

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

问题描述

感谢您的帮助……现在程序可以正常运行了,但是我还有2个问题. 1,如何将输出获取到JTestField t4或t5 2.如何使用JButton Buton3关闭应用程序

Thanks for your help guys...now the program works and runs like it should.. but I have 2 more question. 1.How can I get the output into a JTestField t4 or t5 2.How can I close the application using the 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.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);

  }

}

推荐答案

正如Reimeus指出的,除了缺少主要方法外,您的指令顺序也是错误的.您试图在屏幕上什至没有显示任何内容之前,甚至在创建对象之前,先阅读用户输入.例如,这一行:

In addition to missing a main method, as Reimeus pointed out, your order of instructions is wrong. You are trying to read the user input before anything is even shown on the screen, and even before an object is created. For example, this line:

String t1TextBox = t1.getText();

尝试从尚未添加到尚未创建的面板的TextBox中获取文本.

tries to obtain a text from a TextBox that wasn't even added to a Panel that wasn't yet created.

要解决此问题,您需要重新考虑程序的逻辑.这里有一些提示:

To solve this, you need to rethink the logic of your program. Here are a few hints:

  • 避免方法外的赋值.不用写

  • avoid assignments outside methods. Instead of writing

JFrame Triangle = new JFrame("Triangle Calculator");

像这样在类主体中声明变量:

declare the variable in the class body like this:

JFrame Triangle;

并像下面这样在构造函数中分配它:

and assign it inside the constructor like this:

Triangle = new JFrame("Triangle Calculator");

  • 构建整个UI,然后担心侦听器.这样,可以确保在获取用户输入时不会引用不存在的UI元素.

  • Build the whole UI, then worry about listeners. This way you can be sure that you are not referencing an UI element that does not exist when getting the user input.

    在侦听器内部获取用户输入,如下所示:

    Get the user input inside the listeners, like this:

     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进行用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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