Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch9;
- import java.awt.event.*;
- import java.awt.*;
- import javax.swing.*;
- public class Q1 extends JFrame implements ActionListener{
- static Q1 frm=new Q1();
- static JLabel label =new JLabel("java");
- static JTextField txt =new JTextField();
- static JButton b0=new JButton("0");
- static JButton b1=new JButton("1");
- static JButton b2=new JButton("2");
- static JButton b3=new JButton("3");
- static JButton b4=new JButton("4");
- static JButton b5=new JButton("5");
- static JButton b6=new JButton("6");
- static JButton b7=new JButton("7");
- static JButton b8=new JButton("8");
- static JButton b9=new JButton("9");
- static JButton b10 = new JButton("+");
- static JButton b11 = new JButton("-");
- static JButton b12 = new JButton("*");
- static JButton b13 = new JButton("/");
- static JButton b14 = new JButton("C");
- static JButton b15 = new JButton("=");
- static int tmp =0;//sum
- static char tmp2;//紀錄上一個符號
- public static void main(String arg[])
- {
- Container container = frm.getContentPane();
- frm.setLayout(null);
- //各元件基礎設定
- b1.setBounds(20,200,50,50);
- b1.addActionListener(new Actlis());
- b2.setBounds(70,200,50,50);
- b2.addActionListener(new Actlis());
- b3.setBounds(120,200,50,50);
- b3.addActionListener(new Actlis());
- b13.setBounds(170,200,50,50);
- b13.addActionListener(new Actlis());
- b4.setBounds(20,150,50,50);
- b4.addActionListener(new Actlis());
- b5.setBounds(70,150,50,50);
- b5.addActionListener(new Actlis());
- b6.setBounds(120,150,50,50);
- b6.addActionListener(new Actlis());
- b12.setBounds(170,150,50,50);
- b12.addActionListener(new Actlis());
- b7.setBounds(20,100,50,50);
- b7.addActionListener(new Actlis());
- b8.setBounds(70,100,50,50);
- b8.addActionListener(new Actlis());
- b9.setBounds(120,100,50,50);
- b9.addActionListener(new Actlis());
- b14.setBounds(170,100,50,50);
- b14.addActionListener(new Actlis());
- b0.setBounds(20,250,50,50);
- b0.addActionListener(new Actlis());
- b10.setBounds(70,250,50,50);
- b10.addActionListener(new Actlis());
- b11.setBounds(120,250,50,50);
- b11.addActionListener(new Actlis());
- b15.setBounds(170,250,50,50);
- b15.addActionListener(new Actlis());
- txt.setBounds(20, 40, 200, 30);
- txt.setHorizontalAlignment(JTextField.RIGHT);
- label.setForeground(Color.BLUE);
- label.setFont(new Font("defalut",Font.ITALIC+Font.BOLD,40));
- frm.setLocation(5, 5);
- frm.add(label);
- frm.setVisible(true);
- frm.setTitle("計算機");
- frm.setSize(260,380);
- frm.addWindowListener(new Actlis());
- container.add(txt);
- container.add(label);
- container.add(b0);
- container.add(b1);
- container.add(b2);
- container.add(b3);
- container.add(b4);
- container.add(b5);
- container.add(b6);
- container.add(b7);
- container.add(b8);
- container.add(b9);
- container.add(b10);
- container.add(b11);
- container.add(b12);
- container.add(b13);
- container.add(b14);
- container.add(b15);
- }
- static class Actlis extends WindowAdapter implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource()==b1) //b0~9是顯示數字
- {
- txt.setText(txt.getText()+"1");
- }
- if (e.getSource()==b2)
- {
- txt.setText(txt.getText()+"2");
- }
- if (e.getSource()==b3)
- {
- txt.setText(txt.getText()+"3");
- }
- if (e.getSource()==b4)
- {
- txt.setText(txt.getText()+"4");
- }
- if (e.getSource()==b5)
- {
- txt.setText(txt.getText()+"5");
- }
- if (e.getSource()==b6)
- {
- txt.setText(txt.getText()+"6");
- }
- if (e.getSource()==b7)
- {
- txt.setText(txt.getText()+"7");
- }
- if (e.getSource()==b8)
- {
- txt.setText(txt.getText()+"8");
- }
- if (e.getSource()==b9)
- {
- txt.setText(txt.getText()+"9");
- }
- if (e.getSource()==b0)
- {
- txt.setText(txt.getText()+"0");
- }
- if (e.getSource()==b10) //+ 以下由加號做解釋依此類推
- {
- if(tmp2=='+') //上一個符號是+就做加法
- {
- tmp+=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else if(tmp2=='-')//上一個符號是-就做減法
- {
- tmp-=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else if(tmp2=='*')//上一個符號是*就做乘法
- {
- tmp*=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else if(tmp2=='/')//上一個符號是/就做除法
- {
- tmp/=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else
- {
- tmp=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- tmp2='+';// 存入tmp2做後續判斷
- }
- if (e.getSource()==b11)//-
- {
- if(tmp2=='+')
- {
- tmp+=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else if(tmp2=='-')
- {
- tmp-=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else if(tmp2=='*')
- {
- tmp*=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else if(tmp2=='/')
- {
- tmp/=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else
- {
- tmp=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- tmp2='-';// 存入tmp2做後續判斷
- }
- if (e.getSource()==b12)//*
- {
- if(tmp2=='+')
- {
- tmp+=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else if(tmp2=='-')
- {
- tmp-=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else if(tmp2=='*')
- {
- tmp*=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else if(tmp2=='/')
- {
- tmp/=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else
- {
- tmp=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- tmp2='*';// 存入tmp2做後續判斷
- }
- if (e.getSource()==b13)// /
- {
- if(tmp2=='+')
- {
- tmp+=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else if(tmp2=='-')
- {
- tmp-=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else if(tmp2=='*')
- {
- tmp*=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else if(tmp2=='/')
- {
- tmp/=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- else
- {
- tmp=Integer.parseInt(txt.getText());
- txt.setText(null);
- }
- tmp2='/';
- }
- if (e.getSource()==b14)// C 歸零
- {
- txt.setText(null);
- tmp=0;
- tmp2='c';
- }
- if (e.getSource()==b15)// =
- {
- if(tmp2=='+')
- {
- tmp+=Integer.parseInt(txt.getText());
- txt.setText(Integer.toString(tmp));
- }
- else if(tmp2=='-')
- {
- tmp-=Integer.parseInt(txt.getText());
- txt.setText(Integer.toString(tmp));
- }
- else if(tmp2=='*')
- {
- tmp*=Integer.parseInt(txt.getText());
- txt.setText(Integer.toString(tmp));
- }
- else if(tmp2=='/')
- {
- tmp/=Integer.parseInt(txt.getText());
- txt.setText(Integer.toString(tmp));
- }
- else
- {
- txt.setText(txt.getText());
- }
- tmp2='=';// 存入tmp2做後續判斷
- }
- }
- @Override
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- }
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement