展开全部
向下边用java开发的一个计32313133353236313431303231363533e78988e69d8331333236356634数器的程序
它的文件名用计算器.java
用记事本打开代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class jsq {
JFrame f = new JFrame("计数器");
JMenuBar menubar1 = new JMenuBar();
JMenu menu1 = new JMenu("编辑");
JMenu menu2 = new JMenu("帮助");
JTextField text = new JTextField("", 10);
JButton bc = new JButton("清除");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
FlowLayout fl = new FlowLayout(10, 10, 10);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton bx = new JButton("*");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton bch = new JButton("/");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton bjj = new JButton("+");
JButton b0 = new JButton("0");
JButton bd = new JButton(".");
JButton bj = new JButton("-");
JButton bdy = new JButton("=");
public String shu1,shu2;
double add1;
char op;
private boolean hasdot=false ,hasfuhao=false; // 判断小数点
//boolean hasfuhao = false; // 判断是否有运算符号
private boolean panduan=false;
public jsq() {
}
public static void main(String args[]) {
jsq j1 = new jsq();
j1.go();
}
public void go() {
text.setHorizontalAlignment(JTextField.RIGHT);
menubar1.add(menu1);
menubar1.add(menu2);
f.setJMenuBar(menubar1);
f.getContentPane().add("North", p1);
f.getContentPane().add("Center", p2);
p1.add(text);
p1.add(bc);
p2.setLayout(fl);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bx);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bch);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bjj);
p2.add(b0);
p2.add(bd);
p2.add(bj);
p2.add(bdy);
bc.addActionListener(new JMenuHandler(10));
b1.addActionListener(new JMenuHandler(1));
b2.addActionListener(new JMenuHandler(2));
b3.addActionListener(new JMenuHandler(3));
b4.addActionListener(new JMenuHandler(4));
b5.addActionListener(new JMenuHandler(5));
b6.addActionListener(new JMenuHandler(6));
b7.addActionListener(new JMenuHandler(7));
b8.addActionListener(new JMenuHandler(8));
b9.addActionListener(new JMenuHandler(9));
b0.addActionListener(new JMenuHandler(0));
bd.addActionListener(new JMenuHandler2());
if (!panduan) { //第一次按这些按钮,就进行JMenuhandler1()监听
bx.addActionListener(new JMenuHandler1());
bch.addActionListener(new JMenuHandler1());
bjj.addActionListener(new JMenuHandler1());
bj.addActionListener(new JMenuHandler1());
}
else { //第二次按这些按钮,等同于等于的作用
bx.addActionListener(new JMenuHandler3());
bch.addActionListener(new JMenuHandler3());
bjj.addActionListener(new JMenuHandler3());
bj.addActionListener(new JMenuHandler3());
}
// bdy.addActionListener(new JMenuHandler3());
f.setSize(220, 280);
f.setVisible(true);
}
class JMenuHandler
implements ActionListener {
private int ch;
public JMenuHandler(int select) {
ch = select;
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();//得到所按的按钮对应的值
switch (ch) {
case 10:
text.setText("");
hasdot = false;
hasfuhao = false;
panduan=false;
break;
case 0:
text.setText(text.getText() + s);
break;
case 1:
text.setText(text.getText() + s);
break;
case 2:
text.setText(text.getText() + s);
break;
case 3:
text.setText(text.getText() + s);
break;
case 4:
text.setText(text.getText() + s);
break;
case 5:
text.setText(text.getText() + s);
break;
case 6:
text.setText(text.getText() + s);
break;
case 7:
text.setText(text.getText() + s);
break;
case 8:
text.setText(text.getText() + s);
break;
case 9:
text.setText(text.getText() + s);
break;
}
}
}
class JMenuHandler1
implements ActionListener {//对运算符进行监听
public JMenuHandler1() {
}
public void actionPerformed(ActionEvent e) {
if (text.getText().equals("")) {
text.setText("");
}
else
if(!hasfuhao){
op = e.getActionCommand().charAt(0); //取得输入的运算符
if (!panduan) { //是第一次按此按钮
shu1 = text.getText(); //对第一次输入的数据进行记录
panduan = true;
text.setText("");
}
else{
shu2=text.getText();
System.out.println(shu2);
}
//System.out.println(shu1);
hasfuhao = true; //不能再输入运算符
hasdot = false; //可以再次输入小数点
}
}
}
class JMenuHandler2
implements ActionListener {//对小数点进行监听
//String ch1=text.getText();
public JMenuHandler2() {
}
public void actionPerformed(ActionEvent e) {
if(!text.getText().equals(""))
if (!hasdot) { //数中没有小数点
String str;
str = text.getText() + e.getActionCommand();
text.setText(str); //显示小数点
hasdot = true; //有小数点
}
}
}
class JMenuHandler3
implements ActionListener { //对等于号进行监听
public JMenuHandler3() {
double add1 = Double.parseDouble(shu1); //把shu1由string型转换成double型
System.out.println(add1);
}
double add1 = Double.parseDouble(shu1);
double shu2 = Double.parseDouble(text.getText());//类型转换,同上
public void actionPerformed(ActionEvent e) {
switch (op) {
case '+':
add1 = add1 + shu2;
break;
case '-':
add1 = add1 - shu2;
break;
case '*':
add1 = add1 * shu2;
break;
case '/':
add1 = add1 / shu2;
break;
}
if(panduan){
hasfuhao = false; //可以重新输入运算符
panduan = false;
}else{
op=e.getActionCommand().charAt(0);//新的运算符放到op中
hasfuhao=true;
}
hasdot = false; //可以重新输入小数点
text.setText(add1 + ""); //强制类型转换
}
}
}
在public static void main(String args[])出现main我们就可肯定它就main函数,一般出现main的是主函数。
已赞过
已踩过<
你对这个回答的评价是?
评论
收起