请高手帮忙写一个JAVA程序
编写一个Java程序,在程序中建立一个窗口,窗口中有四个按钮,分别为加、减、乘、除;窗口上还有三个文本框,单击任一按钮,将两个文本框的数字进行相应的运算,在第三个文本框中...
编写一个Java程序,在程序中建立一个窗口,窗口中有四个按钮,分别为加、减、乘、除;窗口上还有三个文本框,单击任一按钮,将两个文本框的数字进行相应的运算,在第三个文本框中显示结果。单击关闭窗口按钮,能把窗口关闭。
展开
1个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener{
private JTextField jtf1 = new JTextField(5);
private JTextField jtf2 = new JTextField(5);
private JTextField jtf3 = new JTextField(20);
private JButton jb1 = new JButton("加");
private JButton jb2 = new JButton("减");
private JButton jb3 = new JButton("乘");
private JButton jb4 = new JButton("除");
public Test()
{
JPanel jp1 = new JPanel();
jp1.add(jtf1);
jp1.add(jtf2);
jp1.add(jtf3);
this.add(jp1,BorderLayout.NORTH);
addButtons();
}
public void addButtons()
{
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
JPanel jp2 = new JPanel();
jp2.add(jb1);
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
this.add(jp2,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
try
{
String str1 = this.jtf1.getText().trim();
String str2 = this.jtf2.getText().trim();
if(e.getSource() == jb1)
{
jtf3.setText((Double.parseDouble(str1)+Double.parseDouble(str2))+"");
}
if(e.getSource() == jb2)
{
jtf3.setText((Double.parseDouble(str1)-Double.parseDouble(str2))+"");
}
if(e.getSource() == jb3)
{
jtf3.setText((Double.parseDouble(str1)*Double.parseDouble(str2))+"");
}
if(e.getSource() == jb4)
{
jtf3.setText((Double.parseDouble(str1)/Double.parseDouble(str2))+"");
}
}catch(Exception ex)
{
}
}
public static void main(String[] args)
{
Test t = new Test();
t.setSize(400, 200);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true);
}
}
把弹窗的代码去掉了
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener{
private JTextField jtf1 = new JTextField(5);
private JTextField jtf2 = new JTextField(5);
private JTextField jtf3 = new JTextField(20);
private JButton jb1 = new JButton("加");
private JButton jb2 = new JButton("减");
private JButton jb3 = new JButton("乘");
private JButton jb4 = new JButton("除");
public Test()
{
JPanel jp1 = new JPanel();
jp1.add(jtf1);
jp1.add(jtf2);
jp1.add(jtf3);
this.add(jp1,BorderLayout.NORTH);
addButtons();
}
public void addButtons()
{
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
JPanel jp2 = new JPanel();
jp2.add(jb1);
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
this.add(jp2,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
try
{
String str1 = this.jtf1.getText().trim();
String str2 = this.jtf2.getText().trim();
if(e.getSource() == jb1)
{
jtf3.setText((Double.parseDouble(str1)+Double.parseDouble(str2))+"");
}
if(e.getSource() == jb2)
{
jtf3.setText((Double.parseDouble(str1)-Double.parseDouble(str2))+"");
}
if(e.getSource() == jb3)
{
jtf3.setText((Double.parseDouble(str1)*Double.parseDouble(str2))+"");
}
if(e.getSource() == jb4)
{
jtf3.setText((Double.parseDouble(str1)/Double.parseDouble(str2))+"");
}
}catch(Exception ex)
{
}
}
public static void main(String[] args)
{
Test t = new Test();
t.setSize(400, 200);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true);
}
}
把弹窗的代码去掉了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询