恬然自得.活在當下: [功課]Java Swing練習使用KeyListener

2011年6月7日 星期二

[功課]Java Swing練習使用KeyListener

這次的功課,老師最主要是讓我們練習寫鍵盤事件,可是老師又沒有在智慧大師上貼範例- 請大家同樣去看我之前分享的網址,上面都有教學唷,當然大家也可以直接連結到以下網址:http://download.oracle.com/javase/tutorial/uiswing/events/index.html
 
我覺得大家真的可以多玩玩Swing,個人是覺得還滿有趣的,至少像是當時在學VB一樣,會去排版一些元件,或是隨興加加自己喜歡的功能XD 這一次我寫了兩個範例,因為重複性的Label還有程式碼很多,所以我利用陣列與迴圈的方式,節省了重複性的程式碼,請各位在觀看兩段程式碼就會發現差別了。

按下「Launch」鈕,可直接操縱此次功課的程式,歡迎試試看唷!
P.S.家裡用IE可以成功執行。


執行結果:




底下分享我自己寫的部分,另外也歡迎平時有光顧我網誌的讀者,可以在底下留言自己的寫法。^-^

寫法僅供參考,畢竟Java才學不到一年吧 >”<
(大家記得註解和程式碼要自己重新打過唷,工讀生會檢查;請大家要多多修改唷,尤其在版面的樣子,這是最好改,也是最不容易被老師抓到的地方,請多下點功夫!)
程式碼第一種寫法:

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


@SuppressWarnings("serial")
public class b11967046 extends JFrame implements ActionListener, KeyListener{
 /* 11967046  公版
  * 請使用Swing元件設計一換鈔機,
  * 在JTextField元件輸入金額,
  * 按下Enter或一個JButton元件,
  * 可以顯示換成多少張1000、500、200、100元紙鈔
  * 和50、10、5、1元硬幣
  * (以換成最多張大面額鈔票方式進行計算),
  * 每一個面額對應一個JLabel元件,
  * 分別顯示各種面額轉換的張數或個數。
  */

 JLabel tip1 = new JLabel();
 JLabel tip2 = new JLabel();
 
 //宣告紀錄鈔票張數的Label
 JLabel thousand_lb = new JLabel();
 JLabel five_hundred_lb = new JLabel();
 JLabel two_hundred_lb = new JLabel();
 JLabel hundred_lb = new JLabel();
 JLabel fifty_lb = new JLabel();
 JLabel ten_lb = new JLabel();
 JLabel five_lb = new JLabel();
 JLabel one_lb = new JLabel();
 
 //宣告輸入數值的文字框和運算按鈕
 JTextField pushMoney_field = new JTextField(15);
 JButton transform_btn = new JButton();
 
 
 public b11967046(){
  super("換鈔機--幫你換好最大張的面額");
  Container contain = this.getContentPane();
  //使用了三個Panel。因為將畫面分成上半部和下半部
  //再將兩個Panel放到parent_panel中
  JPanel parent_panel = new JPanel();
  JPanel top_panel = new JPanel();
  JPanel bottom_panel = new JPanel();
  
  //設定為GridLayout來排版
  parent_panel.setLayout(new GridLayout(2,0));
  bottom_panel.setLayout(new GridLayout(0,2));
  
  tip1.setText("換鈔機會自動幫您換好最大張的面額");
  tip2.setText("換鈔的金額會顯示如下");
  tip2.setBorder(BorderFactory.createMatteBorder(
    2, 2, 2, 2, Color.blue));
  pushMoney_field.setText("請將換鈔的金額輸入於此欄");
  pushMoney_field.selectAll();
  transform_btn.setText("轉換");
   
  thousand_lb.setText("1000元紙鈔:");
  five_hundred_lb.setText("500元紙鈔:");
  two_hundred_lb.setText("200元紙鈔:");
  hundred_lb.setText("100元紙鈔:");
  fifty_lb.setText("50元紙鈔:");
  ten_lb.setText("10元紙鈔:");
  five_lb.setText("5元紙鈔:");
  one_lb.setText("1元紙鈔:");
  
  
  top_panel.add(pushMoney_field);
  top_panel.add(transform_btn);
  top_panel.add(tip1);
  top_panel.add(tip2);
  
  bottom_panel.add(thousand_lb);
  bottom_panel.add(five_hundred_lb);
  bottom_panel.add(two_hundred_lb);
  bottom_panel.add(hundred_lb);
  bottom_panel.add(fifty_lb);
  bottom_panel.add(ten_lb);
  bottom_panel.add(five_lb);
  bottom_panel.add(one_lb);
  
  
  parent_panel.add(top_panel);
  parent_panel.add(bottom_panel);
  
  contain.add(parent_panel);
  
  transform_btn.addActionListener(this);
  //設定文字框的鍵盤事件
  pushMoney_field.addKeyListener(this);
  
 }
 public static void main(String[] args) {
  a11967046 frame = new a11967046();
  frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  frame.setSize(300,300);
  frame.setVisible(true);
  
 }

 @Override
 public void actionPerformed(ActionEvent arg0) {
  changeMoney();
 }

 @Override
 public void keyPressed(KeyEvent event) {
 
 }

 @Override
 public void keyReleased(KeyEvent event) {
  //當鍵盤按下Enter時,觸發換鈔的事件
  if(event.getKeyCode() == 10){
   changeMoney();
  }
 }

 @Override
 public void keyTyped(KeyEvent event) { 
  
 }
 
 public void changeMoney(){
  int amount = Integer.parseInt(
    pushMoney_field.getText());
  int thousand = 0,
   five_hundred = 0,
   two_hundred = 0,
   hundred = 0,
   fifty = 0,
   ten = 0,
   five = 0,
   one = 0;
  int remainder = amount;
  
  if(amount <= 0){
   JOptionPane.showMessageDialog(null,"您輸入了0或負值唷!","錯誤訊息>U<"
     ,JOptionPane.INFORMATION_MESSAGE);
  }
  else if (amount > 0){
   
   //繁雜的計算過程
   //實際上這個過程可以透過陣列搭配迴圈簡單解決
   thousand = remainder / 1000;
   remainder = remainder % 1000;
   five_hundred = remainder / 500;
   remainder = remainder % 500;
   two_hundred = remainder / 200;
   remainder = remainder % 200;
   hundred = remainder / 100;
   remainder = remainder % 100;
   fifty = remainder / 50;
   remainder = remainder % 50;
   ten = remainder / 10;
   remainder = remainder % 10;
   five = remainder / 5;
   remainder = remainder % 5;
   one = remainder / 1;
   remainder = remainder % 1;
   
   
   
  }
  
  thousand_lb.setText("1000元紙鈔:" + thousand);
  five_hundred_lb.setText("500元紙鈔:" + five_hundred);
  two_hundred_lb.setText("200元紙鈔:" + two_hundred);
  hundred_lb.setText("100元紙鈔:" + hundred);
  fifty_lb.setText("50元紙鈔:" + fifty);
  ten_lb.setText("10元紙鈔:" + ten);
  five_lb.setText("5元紙鈔:" + five);
  one_lb.setText("1元紙鈔:" + one);
  
 }
}

程式第二種寫法:

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


@SuppressWarnings("serial")
public class a11967046 extends JFrame implements ActionListener, KeyListener{
  /* 11967046  公版
   * 請使用Swing元件設計一換鈔機,
   * 在JTextField元件輸入金額,
   * 按下Enter或一個JButton元件,
   * 可以顯示換成多少張1000、500、200、100元紙鈔
   * 和50、10、5、1元硬幣
   * (以換成最多張大面額鈔票方式進行計算),
   * 每一個面額對應一個JLabel元件,
   * 分別顯示各種面額轉換的張數或個數。
   */
 
  JLabel tip1 = new JLabel();
  JLabel tip2 = new JLabel();
  JTextField pushMoney_field = new JTextField(15);
  JButton transform_btn = new JButton();
  
  int number [] = {1000,500,200,100,50,10,5,1};
  ArrayList<Jlabel> GUIComponent ;
  
 public a11967046(){
  super("換鈔機--幫你換好最大張的面額");
  Container contain = this.getContentPane();
  JPanel parent_panel = new JPanel();
  JPanel top_panel = new JPanel();
  JPanel bottom_panel = new JPanel();
  
  parent_panel.setLayout(new GridLayout(2,0));
  bottom_panel.setLayout(new GridLayout(0,2));
  
  tip1.setText("換鈔機會自動幫您換好最大張的面額");
  tip2.setText("換鈔的金額會顯示如下");
  tip2.setBorder(BorderFactory.createMatteBorder(
    2, 2, 2, 2, Color.blue));
  pushMoney_field.setText("請將換鈔的金額輸入於此欄");
  pushMoney_field.selectAll();
  transform_btn.setText("轉換");
   
  GUIComponent = new ArrayList<Jlabel>();
  for(int i = 0; i < number.length;i++){
   JLabel nLabel = new JLabel(number[i]+"元紙鈔:");
   GUIComponent.add(nLabel);
   bottom_panel.add(GUIComponent.get(i));
  }
  
  
  top_panel.add(pushMoney_field);
  top_panel.add(transform_btn);
  top_panel.add(tip1);
  top_panel.add(tip2);
  
  parent_panel.add(top_panel);
  parent_panel.add(bottom_panel);
  
  contain.add(parent_panel);
  
  transform_btn.addActionListener(this);
  pushMoney_field.addKeyListener(this);
  
 }
  
 public static void main(String[] args) {
  a11967046 frame = new a11967046();
  frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  frame.setSize(300,300);
  frame.setResizable(false);
  frame.setVisible(true);
  
 }

 @Override
 public void actionPerformed(ActionEvent arg0) {
  changeMoney();
 }

 @Override
 public void keyPressed(KeyEvent event) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void keyReleased(KeyEvent event) {
  // TODO Auto-generated method stub
  
  if(event.getKeyCode() == 10){
   changeMoney();
  }
 }

 @Override
 public void keyTyped(KeyEvent event) {
  // TODO Auto-generated method stub
  
 }
 
 public void changeMoney(){
  int amount = Integer.parseInt(
    pushMoney_field.getText());
  int remainder = amount;
  
  int quantity [] = {0,0,0,0,0,0,0,0};
  
  
  if(amount <= 0){
   JOptionPane.showMessageDialog(null,"您輸入了0或負值唷!","錯誤訊息>U<"
     ,JOptionPane.INFORMATION_MESSAGE);
  }
  else if (amount > 0){
  
   for(int i = 0;i < number.length;i++){
    quantity[i] = remainder / number[i];
    remainder = remainder % number[i];
   }
   
   for(int i = 0;i < number.length;i++){
    JLabel nLabel = GUIComponent.get(i);
    nLabel.setText(number[i] + "元紙鈔:" + quantity[i]);
   }
   
  }
 }
}

沒有留言:

張貼留言

歡迎提供良性建議^_^