package com.progrm.counter;
//導入相關的包
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Counter extends JFrame implements ActionListener,KeyListener,MouseListener{
//
//定義相關組件
private JTextField oneJT=null;
private JTextField twoJT=null;
private JTextField threeJT=null;
private JTextField fourJT=null;
private JTextField fiveJT=null;
private JTextField sixJT=null;
private JTextArea oneJTA=null;
private JButton oneJB=null;
private JButton twoJB=null;
private JButton threeJB=null;
private JButton fourJB=null;
private JPanel oneJPL=null;
private JPanel twoJPL=null;
private JPanel threeJPL=null;
private JPanel fourJPL=null;
private JPanel fiveJPL=null;
private JPanel sixJPL=null;
private JPanel sevenJPL=null;
//創建窗體的構造方法
public Counter(String title){
super(title);
//
//添加相關組件
oneJT=new JTextField(6); //存放第一個運算數的文本框
twoJT=new JTextField(6); //存放第二個運算數的文本框
oneJPL=new JPanel();
oneJPL.add(oneJT);
oneJPL.add(twoJT);
threeJT=new JTextField(6); //存放 + 運算結果的文本框
threeJT.setEditable(false);
oneJB=new JButton("+"); //顯示 + 號的按鈕
oneJB.addActionListener(this); //給按鈕添加動作監聽
twoJPL=new JPanel();
twoJPL.add(threeJT);
twoJPL.add(oneJB);
fourJT=new JTextField(6); //存放-運算結果的文本框
fourJT.setEditable(false);
twoJB=new JButton("-"); //顯示 - 的按鈕
twoJB.addActionListener(this); //給按鈕添加動作監聽
threeJPL=new JPanel();
threeJPL.add(fourJT);
threeJPL.add(twoJB);
fiveJT=new JTextField(6);
fiveJT.setEditable(false);
threeJB=new JButton("*"); //顯示 * 的按鈕
threeJB.addActionListener(this); //給按鈕添加動作監聽
fourJPL=new JPanel();
fourJPL.add(fiveJT);
fourJPL.add(threeJB);
sixJT=new JTextField(6);
sixJT.setEditable(false);
fourJB=new JButton("/"); //顯示 / 的按鈕
fourJB.addActionListener(this); //給按鈕添加動作監聽
fiveJPL=new JPanel();
fiveJPL.add(sixJT);
fiveJPL.add(fourJB);
sixJPL=new JPanel(new GridLayout(4,1));
sixJPL.add(twoJPL);
sixJPL.add(threeJPL);
sixJPL.add(fourJPL);
sixJPL.add(fiveJPL);
oneJTA=new JTextArea("說明:分別向前面的兩個輸入框中輸入第一個,第二個\n 運算數.點擊不同的按鈕會進行相對應的運算,\n 并把結果顯示在按鈕前面的結果框中" );
oneJTA.setColumns(5);
oneJTA.setRows(5);
oneJTA.setEditable(false); //設置文本域內容不可改變
sevenJPL=new JPanel(new GridLayout(1,0));
sevenJPL.add(oneJTA);
this.add(oneJPL);
this.add(sixJPL);
this.add(sevenJPL);
//
//設置窗體
this.setBounds(280,380,350,290); //設置窗體的大小和彈出位置
//this.setLayout(new GridLayout(3,0)); //設置窗體的布局模式
this.setLayout(new FlowLayout());
this.setDefaultCloseOperation(EXIT_ON_CLOSE); //設置關閉窗體的同時釋放內存
this.setResizable(false); //設置窗體大小不可調整
this.setVisible(true); //設置窗體為可見的
}
//
//實現動作監聽的方法
public void actionPerformed(ActionEvent e){
if(e.getSource()==oneJB){ //當窗體監測到 + 按鈕被點擊時執行這個if的子代碼
double jt=Integer.parseInt(oneJT.getText());
double jt1=Integer.parseInt(twoJT.getText());
threeJT.setText(jt+jt1+""); //把運算的結果顯示在結果文本框中
}
if(e.getSource()==twoJB){ //當窗體監測到 - 按鈕被點擊時執行這個if的子代碼
double jt=Integer.parseInt(oneJT.getText());
double jt1=Integer.parseInt(twoJT.getText());
fourJT.setText(jt-jt1+""); //把運算的結果顯示在結果文本框中
}
if(e.getSource()==threeJB) { //當窗體監測到 * 按鈕被點擊時執行這個if的子代碼
double jt=Integer.parseInt(oneJT.getText());
double jt1=Integer.parseInt(twoJT.getText());
fiveJT.setText(jt*jt1+""); //把運算的結果顯示在結果文本框中
}
if(e.getSource()==fourJB) { //當窗體監測到 / 按鈕被點擊時執行這個if的子代碼
if(twoJT.getText().equals("0")) {
JOptionPane.showMessageDialog(this,"被除數不能為0");
}else {
double jt=Integer.parseInt(oneJT.getText());
double jt1=Integer.parseInt(twoJT.getText());
sixJT.setText(jt/jt1+""); //把運算的結果顯示在結果文本框中
}
}
}
//
//實現鍵盤監聽的方法
public void keyPressed(KeyEvent e){
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
//
//實現鼠標監聽的方法
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
}
public class TsetCounter {
public static void main(String[] args) {
new Counter("計算器");
//new test("測試");
}
}