首页 > 其他 > 详细

试验十

时间:2019-06-03 19:05:45      阅读:85      评论:0      收藏:0      [点我收藏+]

package jisuanqi;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;

class counter1 extends JFrame 
{
public counter1()
{
super("计算器");
this.setSize(400,100);
this.setLocation(300,240);
this.setLayout(new FlowLayout());
TextField text1=new TextField(4);
text1.setText("1");
this.add(text1);

String proList[] = { "+","-","x" ,"%"};
TextField text;
JComboBox comboBox;
Container conPane = getContentPane();
comboBox = new JComboBox(proList);
comboBox.setEditable(true);
conPane.add(comboBox);

TextField text2=new TextField(4);
text2.setText("1");
this.add(text2);
JButton button = new JButton("=");
this.add(button);
TextField text3=new TextField(4);
text3.setText("2");

button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)

String s=comboBox.getEditor().getItem().toString();
double a= Integer.parseInt(text1.getText());
double b= Integer.parseInt(text2.getText());
if(s.equals("+")) {
double t=a+b;
String m=String.valueOf(t);

text3.setText(m);
}
else if(s.equals("-"))
{double t=a-b;
String m=String.valueOf(t);

text3.setText(m);}
else if(s.equals("x"))
{double t=a*b;
String m=String.valueOf(t);

text3.setText(m);}
else
{double t=a/b;
String m=String.valueOf(t);

text3.setText(m);}

}});
conPane.add(text3);
this.setVisible(true);
}

 

}

public class Counter {
public static void main(String[] args)
{
new counter1();
}
}

试验十

原文:https://www.cnblogs.com/yanqianying1/p/10968639.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!