简体   繁体   English

使用JTextField进行用户输入

[英]Using JTextField for user input

Thanks for your help guys...now the program works and runs like it should.. but I have 2 more question. 感谢您的帮助...现在程序可以正常运行了,但是我还有2个问题。 1.How can I get the output into a JTestField t4 or t5 2.How can I close the application using the JButton Buton3 1.如何将输出获取到JTestField t4或t5中2.如何使用JButton Buton关闭应用程序

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);

  }

} }

In addition to missing a main method, as Reimeus pointed out, your order of instructions is wrong. 正如Reimeus所指出的,除了缺少主要方法外,您的指令顺序也是错误的。 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();

tries to obtain a text from a TextBox that wasn't even added to a Panel that wasn't yet created. 尝试从尚未添加到尚未创建的面板的TextBox中获取文本。

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"); 
  • Build the whole UI, then worry about listeners. 构建整个UI,然后担心侦听器。 This way you can be sure that you are not referencing an UI element that does not exist when getting the user input. 这样,可以确保在获取用户输入时不会引用不存在的UI元素。

  • 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); } 

    }); });

Add a main method: 添加一个main方法:

public static void main(String[] args) {
   SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
         new TriangleFarfan();
      }
   });
}

The declaration 报关单

JTextField t1 = new JTextField(20);

doesn't set the value in the JTextField to 20 . 不会将JTextField的值设置为20 Instead it sets the number of columns for the JTextComponent but with an empty String . 而是设置JTextComponent的列数,但使用空String Therefore the line 因此线

double side1 = Double.parseDouble(t1TextBox);

will throw an NumberFormatException on startup. 在启动时将抛出NumberFormatException

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM