使用JTextField进行用户输入

时间:2013-04-29 18:46:24

标签: java swing jframe jbutton jtextfield

感谢您的帮助......现在该程序可以正常运行并运行...但我还有2个问题。  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.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);

  }

}

2 个答案:

答案 0 :(得分:3)

除了缺少主要方法,正如Reimeus指出的那样,你的指示顺序是错误的。在屏幕上显示任何内容之前,甚至在创建对象之前,您都试图读取用户输入。例如,这一行:

String t1TextBox = t1.getText();

尝试从TextBox中获取一个文本,该文本甚至没有添加到尚未创建的Panel中。

要解决此问题,您需要重新考虑程序的逻辑。以下是一些提示:

  • 避免在方法之外进行分配。而不是写

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

    在类体中声明变量,如下所示:

    JFrame Triangle;
    

    并将其分配到构造函数中,如下所示:

    Triangle = new JFrame("Triangle Calculator");
    
  • 构建整个UI,然后担心听众。这样,您可以确保在获取用户输入时不引用不存在的UI元素。

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

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

    });

答案 1 :(得分:2)

添加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设置列数,但为空String。因此行

double side1 = Double.parseDouble(t1TextBox);

将在启动时抛出NumberFormatException