编写一个Java小应用程序,设计4个按钮,分别命名为“加”、“差”、“积”、“除”以及3个文本框。单击相
编写一个Java小应用程序,设计4个按钮,分别命名为“加”、“差”、“积”、“除”以及3个文本框。单击相应的按钮...
编写一个Java小应用程序,设计4个按钮,分别命名为“加”、“差”、“积”、“除”以及3个文本框。单击相应的按钮
展开
2个回答
展开全部
..写了一个,不知是否符合...或者可以参考一下..import java.awt.*;
import java.awt.event.*;
public class JiSuanQi extends Frame implements ActionListener {
TextField c1,c2,c3;//三个文本框
Button btn1,btn2,btn3,btn4;//四个按钮
double x,y,z;//三个变量
public JiSuanQi(){//外观super();setSize(150,200);
setTitle("加减乘除");
//控件创建及注册监听
c1=new TextField(15);
c1.addActionListener(this);
c2=new TextField(15);
c2.addActionListener(this);
c3=new TextField(15);
c3.addActionListener(this);
btn1=new Button("加");
btn1.addActionListener(this);
btn2=new Button("差");
btn2.addActionListener(this);
btn3=new Button("积");
btn3.addActionListener(this);
btn4=new Button("除");
btn4.addActionListener(this);//布局setLayout(new FlowLayout());add(c1);add(c2);add(c3);add(btn1);add(btn2);add(btn3);add(btn4);//关闭按钮addWindowListener(new WindowAdapter(){
y=Double.parseDouble(c2.getText());//加if(e.getSource()==btn1){z=x+y;c3.setText(Double.toString(z));}//差if(e.getSource()==btn2){z=x-y;c3.setText(Double.toString(z));}//积if(e.getSource()==btn3){z=x*y;c3.setText(Double.toString(z));}//除if(e.getSource()==btn4){z=x/y;c3.setText(Double.toString(z));}x=0;y=0;z=0; //清空变量}catch(NumberFormatException ee){
展开全部
package anfang;
import java.awt.*;
import java.awt.event.*;
public class example1 extends Frame{
Frame frm;
Button btn1,btn2,btn3,btn4;
TextField txt1,txt2,txt3;
public example1()
{
setLayout(null);
btn1 = new Button("+");
btn1.setLocation(55,55);
btn1.setSize(80,20);
class BtnClick implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
txt3.setText(""+(Integer.parseInt(txt1.getText())+Integer.parseInt(txt2.getText())));
}
}
BtnClick bc=new BtnClick();
btn1.addActionListener(bc);
add(btn1);
btn2 = new Button("-");
btn2.setLocation(50,100);
btn2.setSize(80,20);
class BtnClick1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
txt3.setText(""+(Integer.parseInt(txt1.getText())-Integer.parseInt(txt2.getText())));
}
}
BtnClick1 bc1=new BtnClick1();
btn2.addActionListener(bc1);
add(btn2);
btn3 = new Button("*");
btn3.setLocation(50,150);
btn3.setSize(80,20);
class BtnClick2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
txt3.setText(""+Integer.parseInt(txt1.getText())*Integer.parseInt(txt2.getText()));
}
}
BtnClick2 bc2=new BtnClick2();
btn3.addActionListener(bc2);
add(btn3);
btn4 = new Button("/");
btn4.setLocation(50,200);
btn4.setSize(80,20);
class BtnClick3 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
txt3.setText(""+Integer.parseInt(txt1.getText())/Integer.parseInt(txt2.getText()));
}
}
BtnClick3 bc3=new BtnClick3();
btn4.addActionListener(bc3);
add(btn4);
txt1 = new TextField();
txt1.setLocation(200,60);
txt1.setSize(80,20);
add(txt1);
txt2 = new TextField();
txt2.setLocation(200,120);
txt2.setSize(80,20);
add(txt2);
txt3 = new TextField();
txt3.setLocation(200,180);
txt3.setSize(80,20);
add(txt3);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
example1 frm=new example1();
frm.setSize(450,300);
frm.show();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |