-1

感谢你们的帮助……现在程序可以正常运行了……但我还有 2 个问题。1.如何将输出放入 JTestField t4 或 t5 2.如何使用 JButton Button3 关闭应用程序

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

  }

}

4

2 回答 2

3

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

String t1TextBox = t1.getText();

尝试从尚未添加到尚未创建的 Panel 的 TextBox 中获取文本。

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

  • 避免在方法之外赋值。而不是写

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

    });

于 2013-04-29T19:06:50.113 回答
2

添加main方法:

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

宣言

JTextField t1 = new JTextField(20);

不设置JTextFieldto中的值20。相反,它设置了的列数,JTextComponent但为空String。因此线

double side1 = Double.parseDouble(t1TextBox);

NumberFormatException在启动时抛出。

于 2013-04-29T18:49:33.210 回答