import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SimpleCalculator extends JFrame implements ActionListener{
JPanel mypanel;
JLabel num1;
JLabel num2;
JTextField field1;
JTextField field2;
JTextField result;
JLabel author;
JButton exit;
JButton ope1,ope2,ope3,ope4,ope5,cls;
double results;
double number1;
double number2;
public SimpleCalculator(){
this.setLayout(null);
this.setFont(new Font("宋体",Font.BOLD,12));
mypanel = new JPanel();
mypanel.setLayout(null);
mypanel.setBounds(0, 0, 300, 300);
mypanel.setBackground(Color.orange);
ope1 = new JButton("+");
ope2 = new JButton("-");
ope3 = new JButton("*");
ope4 = new JButton("/");
ope5 = new JButton("=");
cls = new JButton("Cls");
JButton[] mybutton ={ope1,ope2,ope3,ope4,ope5,cls};
for(int i=0;i<5;i++){
mybutton[i].setBounds(10+55*i,130, 41, 41);
mybutton[i].addActionListener(this);
mypanel.add(mybutton[i]);
}
num1 = new JLabel("请输入number1:");
num2 = new JLabel("请输入number2:");
field1 = new JTextField("");
field2 = new JTextField("");
result = new JTextField("");
exit = new JButton("关闭");
author = new JLabel("制作者 杨");
field1.setBackground(Color.white);
field2.setBackground(Color.white);
num1.setOpaque(true);
num1.setBackground(new Color(193,255,193));
num2.setOpaque(true);
num2.setBackground(Color.green);
result.setBounds(10, 180, 140, 40);
field1.setBounds(150, 20, 60, 30);
field2.setBounds(150,70,60,30);
num1.setBounds(10, 10, 120, 40);
num2.setBounds(10, 60, 120, 40);
author.setBounds(10, 230, 120, 40);
exit.setBounds(160, 180, 70, 40);
cls.setBounds(235,180,55,40);
cls.addActionListener(this);
exit.addActionListener(this);
num1.setBackground(Color.LIGHT_GRAY);
num2.setBackground(Color.LIGHT_GRAY);
mypanel.add(num1);
mypanel.add(num2);
mypanel.add(exit);
mypanel.add(field1);
mypanel.add(field2);
mypanel.add(result);
mypanel.add(author);
mypanel.add(cls);
this.add(mypanel);
this.setBounds(100, 100, 300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SimpleCalculator mySimpleCalculator = new SimpleCalculator();
}
@Override
public void actionPerformed(ActionEvent e) {
number1 = Double.valueOf(field1.getText());
number2 = Double.valueOf(field2.getText());
if(e.getSource()==ope1){
results = number1 +number2;
}
else if(e.getSource()==ope2){
results = number1 - number2;
}
else if(e.getSource()==ope3){
results = number1 * number2;
}
else if(e.getSource()==ope4){
DecimalFormat format1 = new DecimalFormat("0.00");
results = number1/number2;
}
else if(e.getSource()==ope5){
result.setText(Double.toString(results));
}
else if(e.getSource()==cls){
field1.setText("");
field2.setText("");
result.setText("");
}
else if(e.getSource()==exit){
this.setVisible(false);
this.dispose();
System.exit(0);
}
}
}
关闭按钮不能用了怎么?非常感谢,太厉害了,我就不该学计算机专业,变成啥也不会,能不能再修改一下,把关闭按钮让它可以关闭