简体   繁体   English

我想创建一个具有3个文本字段和2个按钮的JAVA窗口

[英]I want to create a JAVA window with 3 textfields and 2 buttons

I want to create a simple Java window with 3 text fields and 2 buttons. 我想创建一个具有3个文本字段和2个按钮的简单Java窗口。 I want the buttons to perform subtraction and division operations taking input from the user. 我希望按钮执行减法和除法运算,以吸收用户的输入。 I need help in the actionPerformed method with the if-else conditions. 我在if-else条件下的actionPerformed方法中需要帮助。 I can't figure out what condition to write in the if-else parentheses. 我不知道在if-else括号中写什么条件。

I wrote the following code: 我写了以下代码:

import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
class Event extends Frame implements ActionListener
{
 TextField tf, tf1, tf2;
 Event()
 {
  tf=new TextField();
  tf.setBounds(60,50,170,20);
  tf1=new TextField();
  tf1.setBounds(60,70,170,20);
  tf2=new TextField();
  tf2.setBounds(60,90,170,20);
  Button b=new Button("Subtraction");
  b.setBounds(100,120,80,30);
  b.addActionListener(this);
  Button b1=new Button("Division");
  b1.setBounds(100,160,80,30);
  b1.addActionListener(this);
  add(b);
  add(b1);
  add(tf);
  add(tf1);
  add(tf2);
  setSize(300,300);
  setLayout(null);
  setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
  if(ActionListener(Subtraction))
  {
   int a,b,c;
   Scanner sc=new Scanner(System.in);
   a=sc.nextInt();
   tf.setText("Enter first value: "+a);
   b=sc.nextInt();
   tf1.setText("Enter second value: "+b);
   c=b-a;
   tf2.setText("Result is: "+ c);
  }
  else
  {
   int d,f,g;
   Scanner sc= new Scanner(System.in);
   d=sc.nextInt();
   tf.setText("Enter first value: "+d);
   f=sc.nextInt();
   tf1.setText("Enter second value: "+f);
   g=d/f;
   tf2.setText("Result is: "+g);
  }
 }
 public static void main(String args[])
 {
  new Event();
 }
}

You should be checking on ActionEvent which is being passed into your actionPerformed method. 您应该检查要传递到actionPerformed方法中的ActionEvent The ActionEvent contains information about the source of the action. ActionEvent包含有关操作源的信息。 A possible way to use this would be with an 'action command': 一种可能的方式是使用“操作命令”:

final String subtractionCommand = "Subtraction";

...

Button b=new Button("Subtraction");
b.setActionCommand(subtractionCommand);
b.setBounds(100,120,80,30);
b.addActionListener(this);

...

public void actionPerformed(ActionEvent e)
{
  if(e.getActionCommand().equals(subtractionCommand))
  {
    ...
  }
}

Or by using the getSource() method, this is a bit uglier, but as you'll see it returns you the actual object on which the listener is invoked: 或通过使用getSource()方法,这有点难看,但是如您所见,它将返回您在其上调用侦听器的实际对象:

public void actionPerformed(ActionEvent e)
{
  if(((JButton) e.getSource()).getText().equals("Subtraction")) {
  {
    ...
  }
}

As a final suggestion I would say to drop the implementation of ActionListener on your main class and create 2 different listeners for both buttons.This way you can separate the code for each action which is a bit cleaner certainly if you would plan to add additional buttons, or invoke the same action from within other places in your application. 最后的建议是在主类上放下ActionListener的实现,并为两个按钮创建2个不同的侦听器,这样您就可以为每个动作分离代码,如果您打算添加其他按钮的话,这肯定会更干净一些,或从应用程序中其他位置调用相同的操作。

Here is a solution using two different action listeners: 这是使用两个不同的动作侦听器的解决方案:

import java.awt.*;
import java.awt.event.*;

public class Event extends Frame {
 TextField tf, tf1, tf2;
 Event()
 {
  tf=new TextField();
  tf.setBounds(60,50,170,20);
  tf1=new TextField();
  tf1.setBounds(60,70,170,20);
  tf2=new TextField();
  tf2.setBounds(60,90,170,20);
  Button b=new Button("Subtraction");
  b.setBounds(100,120,80,30);
  b.addActionListener(new ActionListener()
  { 
  public void actionPerformed(ActionEvent e)
  {
  int num1 = Integer.parseInt(tf.getText());
  int num2 = Integer.parseInt(tf1.getText());

   tf2.setText(Integer.toString(num1 - num2));
  }
   });
  Button b1=new Button("Division");
  b1.setBounds(100,160,80,30);
  b1.addActionListener(new ActionListener()
  {
  public void actionPerformed(ActionEvent e)
  {
  int num1 = Integer.parseInt(tf.getText());
  int num2 = Integer.parseInt(tf1.getText());

   tf2.setText(Integer.toString(num1 / num2));
  }
   });

  add(b);
  add(b1);
  add(tf);
  add(tf1);
  add(tf2);
  setSize(300,300);
  setLayout(null);
  setVisible(true);
 }

 public static void main(String args[])
 {
  new Event();
 }
}

When you click on subtract it subtracts the numbers in text box 1 and text box 2 and displays the result in text box 3. 当您单击减法时,它将减去文本框1和文本框2中的数字,并在文本框3中显示结果。

When click on divide it divides the numbers. 当单击除法时,它将对数字进行除法。

在此处输入图片说明

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

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