?? 9_2_030421217.java
字號:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculate implements ActionListener
{
String s="",s1;
double d1,d2;
JFrame jf = new JFrame("計算器") ;
JTextField tf = new JTextField();
public void init()
{
Container c=jf.getContentPane();
tf.setHorizontalAlignment(JTextField.RIGHT);
c.add(tf,"North");
JPanel pn3 = new JPanel(new BorderLayout());
c.add(pn3,"Center");
JPanel pn2 = new JPanel();
pn2.setLayout(new BorderLayout());
JPanel pn1 = new JPanel();
pn1.setLayout(new GridLayout(4,4));
pn3.add(pn2,"North");
pn3.add(pn1);
//設置按鈕
JButton b = new JButton("CLEAR");
b.setToolTipText("請按清除鍵!");
b.setForeground(Color.RED);
b.setBackground(Color.YELLOW);
b.addActionListener(this);
pn2.add(b,"Center");
b = new JButton("OFF");
b.setToolTipText("請按退出鍵!");
b.addActionListener(this);
b.setForeground(Color.RED);
b.setBackground(Color.ORANGE);
pn2.add(b,"East");
b = new JButton("1");
b.addActionListener(this);
pn1.add(b);
b = new JButton("2");
b.addActionListener(this);
pn1.add(b);
b = new JButton("3");
b.addActionListener(this);
pn1.add(b);
b = new JButton("+");
b.setForeground(Color.BLUE);
b.addActionListener(this);
pn1.add(b);
b = new JButton("4");
b.addActionListener(this);
pn1.add(b);
b = new JButton("5");
b.addActionListener(this);
pn1.add(b);
b = new JButton("6");
b.addActionListener(this);
pn1.add(b);
b = new JButton("-");
b.setForeground(Color.BLUE);
b.addActionListener(this);
pn1.add(b);
b = new JButton("7");
b.addActionListener(this);
pn1.add(b);
b = new JButton("8");
b.addActionListener(this);
pn1.add(b);
b = new JButton("9");
b.addActionListener(this);
pn1.add(b);
b = new JButton("*");
b.setForeground(Color.BLUE);
b.addActionListener(this);
pn1.add(b);
b = new JButton("0");
b.addActionListener(this);
pn1.add(b);
b = new JButton(".");
b.addActionListener(this);
pn1.add(b);
b = new JButton("=");
b.setForeground(Color.RED);
b.addActionListener(this);
pn1.add(b);
b = new JButton("\\");
b.setForeground(Color.BLUE);
b.addActionListener(this);
pn1.add(b);
jf.setSize(300,300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
tf.setText(tf.getText()+command);
if(command.equals("CLEAR"))
{
s1=null;
s="";
tf.setText("");
}
else if(command.equals("OFF")) System.exit(0);
else if(!command.equals("*")&&!command.equals("\\")
&&!command.equals("+")&&!command.equals("-")
&&!command.equals("="))
{
if(s1==null)
s1 = command;
else s1+=command;
d1 = new Double(s1).doubleValue();
try
{
if(s.equals("+")) d1 = d1+d2;//加
else if(s.equals("-")) d1 = d2-d1;//減
else if(s.equals("*")) d1 = d1*d2;//乘
else if(s.equals("\\"))d1 = d2/d1;//除
}
catch(Exception ex)
{
tf.setText("Error");
System.out.println(ex.getMessage());
}
}
else if(!command.equals("=")) //判斷輸入是否為+ - * \
{
s = command;
s1 = null;
d2 = d1;
}
else//輸入=時,顯示運算結果
{
tf.setText(tf.getText()+d1);
}
}
public static void main(String [] args)
{
new Calculate().init();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -