import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class GUI extends JFrame implements ActionListener {//实现监听接口
JTextField field = new JTextField(15);
double num1=0,num2;
char op;
public GUI(){
setLayout(new BorderLayout(10,10));
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p1.add(new JLabel("计算框:"));
p1.add(field);
add(p1,BorderLayout.NORTH);
JPanel p2 = new JPanel(new GridLayout(4,1));
//添加按钮
JButton bt1 = new JButton("1");
JButton bt2 = new JButton("2");
JButton bt3 = new JButton("3");
JButton bt4 = new JButton("4");
JButton bt5 = new JButton("5");
JButton bt6 = new JButton("6");
JButton bt7 = new JButton("7");
JButton bt8 = new JButton("8");
JButton bt9 = new JButton("9");
JButton bt0 = new JButton("0");
JButton btDel = new JButton("Del");
JButton btEqual = new JButton("=");
p2.add(bt1);p2.add(bt2);
p2.add(bt3); p2.add(bt4);
p2.add(bt5); p2.add(bt6);
p2.add(bt7);p2.add(bt8);
p2.add(bt9);p2.add(bt0);
p2.add(btDel);p2.add(btEqual);
add(p2,BorderLayout.CENTER);
JPanel p3 = new JPanel(new GridLayout(0,1));
JButton bta = new JButton("+");
JButton btb = new JButton("-");
JButton btc = new JButton("*");
JButton btd = new JButton("/");
p3.add(bta);p3.add(btb);
p3.add(btc);p3.add(btd);
add(p3,BorderLayout.EAST);
//添加监听器
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
bt7.addActionListener(this);
bt8.addActionListener(this);
bt9.addActionListener(this);
bt0.addActionListener(this);
bta.addActionListener(this);
btb.addActionListener(this);
btc.addActionListener(this);
btd.addActionListener(this);
btDel.addActionListener(this);
btEqual.addActionListener(this);
}
//事件的响应
public void actionPerformed(ActionEvent e){
String str =e.getActionCommand();
String s1 = field.getText();
if(str!="+"&&str!="-"&&str!="*"&&str!="/"&&str!="="&&str!="Del"){//为数字按钮时
field.setText(s1+str);
}
else if(str=="Del"){//删除键
field.setText("");
str=null;
}
else if(str=="="){//等号按钮
s1 =field.getText();
String t2 ="";
int flag=0;//标记操作符的位置
if(s1.indexOf("+")!=-1)
flag = s1.indexOf("+");
if(s1.indexOf("-")!=-1)
flag = s1.indexOf("-");
if(s1.indexOf("*")!=-1)
flag = s1.indexOf("*");
if(s1.indexOf("/")!=-1)
flag = s1.indexOf("/");
for(int i=flag+1;i<s1.length();i++)
{
t2+=s1.charAt(i);//获得第二个操作数
}
num2 =Double.parseDouble(t2);//将第二个操作数转换为double
switch(op){
case '+': num1+=num2;break;
case '-': num1-=num2;break;
case '*': num1*=num2;break;
case '/': num1/=num2;break;
}
field.setText(s1+"="+num1+"");//显示在文本框里
}
else{//加减乘除符号
num1 = Double.parseDouble(field.getText());
field.setText(s1+str);
op = str.charAt(0);//获得操作符
}
}
public static void main(String[] args){
GUI frame = new GUI();
frame.setTitle("简易计算器");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,200);
//frame.pack();
frame.setVisible(true);
}
}
不懂再问
追问有没有400行左右的,更多一点的??
追答你基础怎么样?你可以再我的基础上增加一些按钮,然后响应它的功能就行了
追问基础很差..
追答写代码还是要靠自己不然没有进步