import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Layout extends JFrame{
StringBuffer sBuffer = new StringBuffer();
Double a;//中間變量用于存儲輸入的第一個數
Double b;//中間變量,用于存儲輸入的第二個數
Double double1;//用于接收計算結果
Integer i;// i用于表示加減乘除
@SuppressWarnings("deprecation")
public Layout(){
JPanel panel=new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
setVisible(true);
setBounds(200,200,520,450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("電腦");
//設定菜單
JMenuBar menubar;
JMenu menu;
JMenuItem item1;
menubar=new JMenuBar();
menu=new JMenu("菜單");
item1=new JMenuItem("退出");
item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
menu.add(item1);
menubar.add(menu);
setJMenuBar(menubar);
item1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//
JLabel lab=new JLabel("制作人:李振濤");
lab.setBounds(200, 280, 300, 80);
lab.setFont(new Font("dialog", 1, 30));
panel.add(lab);
//顯示區
JLabel label=new JLabel();
label.setBounds(0, 0, 500, 50);
label.setFont(new Font("dialog", 1, 30));
label.setBackground(Color.white);
panel.add(label);
//定義按鈕
JButton button0=new JButton("0");
JButton button1=new JButton("1");
JButton button2=new JButton("2");
JButton button3=new JButton("3");
JButton button4=new JButton("4");
JButton button5=new JButton("5");
JButton button6=new JButton("6");
JButton button7=new JButton("7");
JButton button8=new JButton("8");
JButton button9=new JButton("9");
JButton buttonadd = new JButton("+");// 加
JButton buttonminus = new JButton("-");// 減
JButton buttontime = new JButton("×");// 乘
JButton buttondivid = new JButton("÷");// 除
JButton buttonper = new JButton("%");// 百分号
JButton buttonequal = new JButton("=");// 等于
JButton buttondecimal = new JButton(".");// 小數點
JButton buttonmi = new JButton("^");// 幂
JButton buttonlog = new JButton("log");// 對數
JButton buttonblank = new JButton("");// 空白
//定義按鈕位置大小
button7.setBounds(0, 50, 100, 60);
button7.setFont(new Font("dialog", 1, 30));
panel.add(button7);
button8.setBounds(100, 50, 100, 60);
button8.setFont(new Font("dialog", 1, 30));
panel.add(button8);
button9.setBounds(200, 50, 100, 60);
button9.setFont(new Font("dialog", 1, 30));
panel.add(button9);
buttonadd.setBounds(300, 50, 100, 60);
buttonadd.setFont(new Font("dialog", 1, 30));
panel.add(buttonadd);
buttonminus.setBounds(400, 50, 100, 60);
buttonminus.setFont(new Font("dialog", 1, 30));
panel.add(buttonminus);
button4.setBounds(0, 110, 100, 60);
button4.setFont(new Font("dialog", 1, 30));
panel.add(button4);
button5.setBounds(100, 110, 100, 60);
button5.setFont(new Font("dialog", 1, 30));
panel.add(button5);
button6.setBounds(200, 110, 100, 60);
button6.setFont(new Font("dialog", 1, 30));
panel.add(button6);
buttontime.setBounds(300, 110, 100, 60);
buttontime.setFont(new Font("dialog", 1, 30));
panel.add(buttontime);
buttondivid.setBounds(400, 110, 100, 60);
buttondivid.setFont(new Font("dialog", 1, 30));
panel.add(buttondivid);
button1.setBounds(0, 170, 100, 60);
button1.setFont(new Font("dialog", 1, 30));
panel.add(button1);
button2.setBounds(100, 170, 100, 60);
button2.setFont(new Font("dialog", 1, 30));
panel.add(button2);
button3.setBounds(200, 170, 100, 60);
button3.setFont(new Font("dialog", 1, 30));
panel.add(button3);
buttonper.setBounds(300, 170, 100, 60);
buttonper.setFont(new Font("dialog", 1, 30));
panel.add(buttonper);
buttonlog.setBounds(400, 170, 100, 60);
buttonlog.setFont(new Font("dialog", 1, 30));
panel.add(buttonlog);
buttonblank.setBounds(0, 230, 100, 60);
buttonblank.setFont(new Font("dialog", 1, 30));
panel.add(buttonblank);
button0.setBounds(100, 230, 100, 60);
button0.setFont(new Font("dialog", 1, 30));
panel.add(button0);
buttondecimal.setBounds(200, 230, 100, 60);
buttondecimal.setFont(new Font("dialog", 1, 30));
panel.add(buttondecimal);
buttonmi.setBounds(300, 230, 100, 60);
buttonmi.setFont(new Font("dialog", 1, 30));
panel.add(buttonmi);
buttonequal.setBounds(400, 230, 100, 60);
buttonequal.setFont(new Font("dialog", 1, 30));
panel.add(buttonequal);
//給數字0~9設定動作監聽器
button0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuffer.append("0");
label.setText(sBuffer.toString());
}
});
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuffer.append("1");
label.setText(sBuffer.toString());
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuffer.append("2");
label.setText(sBuffer.toString());
}
});
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuffer.append("3");
label.setText(sBuffer.toString());
}
});
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuffer.append("4");
label.setText(sBuffer.toString());
}
});
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuffer.append("5");
label.setText(sBuffer.toString());
}
});
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuffer.append("5");
label.setText(sBuffer.toString());
}
});
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuffer.append("6");
label.setText(sBuffer.toString());
}
});
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuffer.append("7");
label.setText(sBuffer.toString());
}
});
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuffer.append("8");
label.setText(sBuffer.toString());
}
});
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuffer.append("9");
label.setText(sBuffer.toString());
}
});
// 輸入運算符操作,需要先判斷a是否為0.0
buttonadd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuffer.toString());
sBuffer = new StringBuffer();
label.setText("+");
i = 0;
}
});
buttonminus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuffer.toString());
sBuffer = new StringBuffer();
label.setText("-");
i = 1;
}
});
buttontime.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuffer.toString());
sBuffer = new StringBuffer();
label.setText("×");
i = 2;
}
});
buttondivid.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuffer.toString());
sBuffer = new StringBuffer();
label.setText("÷");
i = 3;
}
});
buttonper.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuffer.toString());
sBuffer = new StringBuffer();
label.setText("%");
i = 4;
}
});
buttonmi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuffer.toString());
sBuffer = new StringBuffer();
label.setText("^");
i = 5;
}
});
buttonlog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuffer.toString());
sBuffer = new StringBuffer();
label.setText("log");
i = 6;
}
});
buttonequal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 該判斷中間變量是否為空
if (!"".equals(sBuffer.toString()) && (!(a == 0.0)))
{
b = Double.parseDouble(sBuffer.toString());
if (i == 0)
{
double1 = a + b;
label.setText(double1.toString());
sBuffer = new StringBuffer();
sBuffer.append(double1);
}
else if (i == 1)
{
double1 = a - b;
label.setText(double1.toString());
sBuffer = new StringBuffer();
sBuffer.append(double1);
}
else if (i == 2)
{
double1 = a * b;
label.setText(double1.toString());
sBuffer = new StringBuffer();
sBuffer.append(double1);
}
else if (i == 3)
{
double1 = a / b;
label.setText(double1.toString());
sBuffer = new StringBuffer();
sBuffer.append(double1);
}
else if (i == 4)
{
double1 = a % b;
label.setText(double1.toString());
sBuffer = new StringBuffer();
sBuffer.append(double1);
}
else if (i == 5)
{
double1 = Math.pow(a,b);
label.setText(double1.toString());
sBuffer = new StringBuffer();
sBuffer.append(double1);
}
else if (i == 6)
{
double1 = Math.log(b)/Math.log(a);
label.setText(double1.toString());
sBuffer = new StringBuffer();
sBuffer.append(double1);
}
else
{
label.setText(sBuffer.toString());
}
}
}
});
buttondecimal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuffer.append(".");
label.setText(sBuffer.toString());
}
});
}
}