该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.JButton; public class calFrame extends JFrame{
protected static String sign="+";
protected static float no1;
protected static int jud=1,jud2=0;
protected static StringBuffer text = new StringBuffer();//可伸缩字符串
protected static JTextField text_out=new JTextField(10);
public calFrame() {
super("简易计算器");
int i;
JButton buttons[]=new JButton[18];
final String fuhao2="0.=+123-456x789÷←C";
text_out.setHorizontalAlignment(JTextField.RIGHT);
text_out.setBounds(0,20,385,50);
text_out.setFont(new Font("黑体",100,50));
text_out.setText("0");
this.add(text_out);
this.setLayout(null);//自由布局
for(i=1;i<19;i++){
buttons[i-1]=new JButton(""+fuhao2.charAt(i-1));//创建按钮若干
this.add(buttons[i-1]);
buttons[i-1].setBounds(100*((i%4==0?4:i%4)-1),50*(9-((i-1)/4))-160,80,40);
buttons[i-1].setFont(new Font("楷体",5,40));//设置按钮字体
}
buttons[0].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
add('0');
}});
buttons[1].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
add(fuhao2.charAt(1));
}});
buttons[2].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal("null",Float.parseFloat(text_out.getText()));
}});
buttons[3].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal("+",Float.parseFloat(text_out.getText()));
}});
buttons[4].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
add('1');
}});
buttons[5].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
add('2');
}});
buttons[6].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
add('3');
}});
buttons[7].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal("-",Float.parseFloat(text_out.getText()));
}});
buttons[8].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
add('4');
}});
buttons[9].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
add('5');
}});
buttons[10].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
add('6');
}});
buttons[11].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal("x",Float.parseFloat(text_out.getText()));
}});
buttons[12].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
add('7');
}});
buttons[13].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
add('8');
}});
buttons[14].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
add('9');
}});
buttons[15].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal("÷",Float.parseFloat(text_out.getText()));
}});
buttons[16].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
text.deleteCharAt(text.length()-1);//退格
text_out.setText(text.toString());
}});
buttons[17].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
text.delete(0,100);
no1=0;
text_out.setText("0");
sign="+";
}});
}
public float calculate(float a,float b,String sign){
if(sign.equals("+")){return a+b;}
if(sign.equals("-")){return a-b;}
if(sign.equals("x")){return a*b;}
if(sign.equals("÷")){return a/b;}
return a;
}
public void deal(String sign2,float no2){//计算方法
if(jud==1){no1=calculate(no1,no2,sign);
text_out.setText(String.valueOf(no1));
text.delete(0,100);
sign=sign2;}
else text_out.setText("输入错误!C清屏");
jud=0;
}
public void add(char c){
text.append(""+c);
text_out.setText(text.toString());
jud=1;
}
}