我想创建一个包含3个文本字段和2个按钮的JAVA窗口
我想创建一个包含3个文本字段和2个按钮的简单Java窗口。我想让按钮执行减法和除法操作,从用户的输入。我需要关于if-else条件的actionPerformed方法的帮助。我不知道在if-else括号中写什么条件 我编写了以下代码:我想创建一个包含3个文本字段和2个按钮的JAVA窗口,java,Java,我想创建一个包含3个文本字段和2个按钮的简单Java窗口。我想让按钮执行减法和除法操作,从用户的输入。我需要关于if-else条件的actionPerformed方法的帮助。我不知道在if-else括号中写什么条件 我编写了以下代码: import java.util.Scanner; import java.awt.*; import java.awt.event.*; class Event extends Frame implements ActionListener { TextFie
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();
}
}
您应该检查正在传递到
actionPerformed
方法中的ActionEvent
。ActionEvent
包含有关操作源的信息。一种可能的使用方法是使用“操作命令”:
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))
{
...
}
}
或者通过使用getSource()
方法,这有点难看,但正如您将看到的,它会返回调用侦听器的实际对象:
public void actionPerformed(ActionEvent e)
{
if(((JButton) e.getSource()).getText().equals("Subtraction")) {
{
...
}
}
作为最后一个建议,我建议您在主类上放弃ActionListener的实现,并为两个按钮创建两个不同的侦听器。通过这种方式,您可以为每个操作分离代码,这会更干净一些,如果您计划添加其他按钮,或者从应用程序中的其他位置调用相同的操作。您应该检查传递到
actionPerformed
方法中的ActionEvent
。ActionEvent
包含有关操作源的信息。一种可能的使用方法是使用“操作命令”:
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))
{
...
}
}
或者通过使用getSource()
方法,这有点难看,但正如您将看到的,它会返回调用侦听器的实际对象:
public void actionPerformed(ActionEvent e)
{
if(((JButton) e.getSource()).getText().equals("Subtraction")) {
{
...
}
}
作为最后一个建议,我建议您在主类上放弃ActionListener的实现,并为两个按钮创建两个不同的侦听器。通过这种方式,您可以为每个操作分离代码,这会更干净一些,如果您计划添加其他按钮,或者从应用程序中的其他位置调用相同的操作。下面是一个使用两个不同操作侦听器的解决方案:
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();
}
}
单击“减法”时,将减去文本框1和文本框2中的数字,并在文本框3中显示结果
当单击divide时,它将数字分开
以下是一个使用两种不同操作侦听器的解决方案:
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();
}
}
单击“减法”时,将减去文本框1和文本框2中的数字,并在文本框3中显示结果
当单击divide时,它将数字分开
新手提示:变量名使用多个字符。a、 b,c。。。然后是tf,tf1,嘿,另一个b是按钮,而不是int。千万不要写这样的代码,即使是一个简单的例子。始终编写代码,就好像你的生活依赖于其他人能够快速阅读和理解代码一样。使用他们所说的意思的名称,说出他们的意思。谢谢,我以后会记住这一点。新手提示:对变量名称使用多个字符。a、 b,c。。。然后是tf,tf1,嘿,另一个b是按钮,而不是int。千万不要写这样的代码,即使是一个简单的例子。始终编写代码,就好像你的生活依赖于其他人能够快速阅读和理解代码一样。使用他们所说的意思的名字,说出他们的意思。谢谢,我以后会记住的。