📄 calculator.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator{
public static void main(String[]a){
MyFrame frm=new MyFrame();
frm.setVisible(true);
}
}
class MyFrame extends JFrame{
double operator1,operator2,result;
char c;
JButton button1,button2,button3,button4,button5,button6;
JLabel label;
JPanel panel1,panel2;
JTextField text1,text2,text3,text4;
Container container;
public MyFrame(){
button1=new JButton("+");
button2=new JButton("-");
button3=new JButton("*");
button4=new JButton("/");
button5=new JButton("=");
button6=new JButton("AC");
label=new JLabel("=");
text1=new JTextField(10);
text2=new JTextField(10);
text3=new JTextField(10);
text4=new JTextField(1);
panel1=new JPanel();
panel2=new JPanel();
container=getContentPane();
container.setLayout(new BorderLayout(10,10));
/* container.add(text1,getConstraints(0,0,5,1,0,0,GridBagConstraints.CENTER,
GridBagConstraints.BOTH,new Insets(0,0,0,0)));
container.add(text2,getConstraints(0,2,5,1,0,0,GridBagConstraints.CENTER,
GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0 ));
container.add(text3,getConstraints(0,3,5,1,0,0,GridBagConstraints.CENTER,
GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0 ));
container.add(text4,getConstraints(4,1,1,1,0,0,GridBagConstraints.CENTER,
GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0 ))
*/
container.add(panel1,BorderLayout.NORTH);
container.add(panel2,BorderLayout.SOUTH);
panel1.setLayout(new FlowLayout(FlowLayout.CENTER,0,10));
panel2.setLayout(new FlowLayout(FlowLayout.CENTER,0,10));
panel1.add(button1);panel1.add(button2);
panel1.add(button3);panel1.add(button4);
panel1.add(button5);panel1.add(button6);
panel2.add(text1);panel2.add(text4);
panel2.add(text2);panel2.add(label);panel2.add(text3);
text3.setEditable(false);
button1.addActionListener(new MyActionListener());
button2.addActionListener(new MyActionListener());
button3.addActionListener(new MyActionListener());
button4.addActionListener(new MyActionListener());
button5.addActionListener(new MyActionListener());
button6.addActionListener(new MyActionListener());
setTitle("Calculator");
setSize(400,200);
setLocation(100,50);
text1.requestFocus();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public double MyGetOperator(JTextField text){
try{
return Double.parseDouble(text.getText());
}
catch(NumberFormatException e)
{ text.setText("Illegal input!");
repaint();
return -9999.9;
}
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==button1)
{
operator1=MyGetOperator(text1);
c='+'; text4.setText("+");
text1.transferFocus();
repaint();
}
else if(e.getSource()==button2)
{
operator1=MyGetOperator(text1);
c='-'; text4.setText("-");
repaint();
}
else if(e.getSource()==button3)
{
operator1=MyGetOperator(text1);
c='*'; text4.setText("*");
repaint();
}
else if(e.getSource()==button4)
{
operator1=MyGetOperator(text1);
c='/'; text4.setText("/");
repaint();
}
else if(e.getSource()==button5)
{
operator2=MyGetOperator(text2);
if(c=='+') result=operator1+operator2;
else if(c=='-') result=operator1-operator2;
else if(c=='*') result=operator1*operator2;
else if(c=='/') result=operator1/operator2;
//String aaa=new String(result);
text3.setText(""+result);
repaint();
}
else if(e.getSource()==button6)
{
operator1=0.0;operator2=0.0;
text1.setText("");
text2.setText("");
text3.setText("");
text4.setText("");
repaint();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -