廣告廣告
  加入我的最愛 設為首頁 風格修改
首頁 首尾
 手機版   訂閱   地圖  簡體 
您是第 3454 個閱讀者
 
發表文章 發表投票 回覆文章
  可列印版   加為IE收藏   收藏主題   上一主題 | 下一主題   
schumacher76
個人文章 個人相簿 個人日記 個人地圖
路人甲
級別: 路人甲 該用戶目前不上站
推文 x0 鮮花 x0
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片
推文 x0
[Java][求助] 簡易 I/O 四則運算
比一般的小算盤陽春
不需什麼9宮格數字鍵盤跟運算符號
只需4個框框,第一格輸入數字,第二格輸入運算符號,第三格在輸入數字,第四格顯示結果

因為一整個沒什麼頭緒,所以上來跟各位前輩請教一下
只完成了基礎框架而已,差在輸入運算跟輸出
請各位前輩指較一下吧


import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class NewSun extends JFrame implements ActionListener {
static String datamem[]=new String;
JLabel lb1, lb2, lb3;
JButton bt1, bt2;
JTextField tx1, tx2, tx3;
JTextArea  tx4;
static int i=1;

public NewSun() {
setLayout(null);

lb1 = new JLabel("簡易計算機");
lb1.setForeground(Color.red);
lb1.setSize(240,40);
lb1.setLocation(10,20);
add(lb1);


lb2 = new JLabel("輸 ..

訪客只能看到部份內容,免費 加入會員 或由臉書 Google 可以看到全部內容



[ 此文章被schumacher76在2009-04-13 17:35重新編輯 ]



獻花 x0 回到頂端 [樓 主] From:臺灣 | Posted:2009-04-13 17:26 |
overing 手機
個人文章 個人相簿 個人日記 個人地圖
初露鋒芒
級別: 初露鋒芒 該用戶目前不上站
推文 x0 鮮花 x55
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

* 沒有實做 ActionListener 的抽象方法 public void actionPerformed(ActionEvent e)
* 然後 JTextArea並沒有 JTextArea(int) 這個建構子
* public void windowClosing(WindowEvent e) { System.exit(0); } 跟
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 的意義可以說是一樣 留一種就好了
* 還有就是....縮排阿!! 表情 (如果用[TAB]鍵可能會被吃掉 請用3~4個空白來替代)

參考看看
複製程式
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//拿掉沒用到的 import

public class NewSun extends JFrame implements ActionListener {

    //static String datamem[] = new String[10]; //沒用到
    JLabel lb1, lb2, lb3;
    JButton bt1, bt2;
    JTextField tx1, tx2, tx3, tx4; // tx4 由 JTextArea 改為 JTextField
    //static int i = 1; //沒用到

    public NewSun() {
        setLayout(null);

        lb1 = new JLabel("簡易計算機");
        lb1.setForeground(Color.red);
        lb1.setSize(240, 40);
        lb1.setLocation(10, 20);
        add(lb1);


        lb2 = new JLabel("輸入數字:");
        lb2.setForeground(Color.black);
        lb2.setSize(240, 40);
        lb2.setLocation(10, 95);
        add(lb2);

        lb3 = new JLabel("=");
        lb3.setForeground(Color.black);
        lb3.setSize(240, 40);
        lb3.setLocation(435, 95);
        add(lb3);

        tx1 = new JTextField(10);
        tx1.setForeground(Color.red);
        tx1.setSize(100, 30);
        tx1.setLocation(75, 100);
        add(tx1);

        tx2 = new JTextField(10);
        tx2.setForeground(Color.blue);
        tx2.setSize(100, 30);
        tx2.setLocation(200, 100);
        add(tx2);

        tx3 = new JTextField(10);
        tx3.setForeground(Color.red);
        tx3.setSize(100, 30);
        tx3.setLocation(325, 100);
        add(tx3);

        tx4 = new JTextField(15); //由 JTextArea 改為 JTextField
        tx4.setForeground(Color.blue);
        tx4.setSize(100, 30);
        tx4.setLocation(450, 100);
        add(tx4);

        bt1 = new JButton("計算");
        bt1.setSize(100, 30);
        bt1.setLocation(135, 200);
        //bt1.setBorderPainted(true); //預設就是這個狀態了
        //bt1.setEnabled(true); //預設就是這個狀態了
        add(bt1);

        bt2 = new JButton("離開");
        bt2.setSize(100, 30);
        bt2.setLocation(380, 200);
        //bt1.setBorderPainted(true); //預設就是這個狀態了
        //bt1.setEnabled(true); //預設就是這個狀態了
        add(bt2);

        bt1.addActionListener(this);
        bt2.addActionListener(this);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 300);
        setVisible(true);
    }

    /*public void setBackground(Color c) {
    getContentPane().setBackground(c);
    }// 沒用到*/

    /**
     * 實作 ActionListener 比需要複寫的抽象方法
     * 元件對this註冊要處理action事件後
     * 執行期間一旦元件被action就會呼叫這個副程式起來執行
     * 參數:e是整個action事件的內容, 當中包含一些觸發這個事件的資訊
     *     像是"哪個元件觸發這事件"之類的
     */
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(bt1)) { //從事件包中的"來源"判斷該做得事
            double src1 = Double.parseDouble(tx1.getText()),
                    src2 = Double.parseDouble(tx3.getText());
            if (tx2.getText().equals("+")) {
                tx4.setText(String.valueOf(src1 + src2));
            } else if (tx2.getText().equals("-")) {
                tx4.setText(String.valueOf(src1 - src2));
            } else if (tx2.getText().equals("*")) {
                tx4.setText(String.valueOf(src1 * src2));
            } else if (tx2.getText().equals("/")) {
                tx4.setText(String.valueOf(src1 / src2));
            }
        } else if (e.getSource().equals(bt2)) {
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        NewSun sun = new NewSun();
        /*sun.addWindowListener(
            new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            }
         );// 跟 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 功能重複*/
    }
}


[ 此文章被overing在2009-04-14 19:04重新編輯 ]

此文章被評分,最近評分記錄
財富:50 (by 三仙) | 理由: 因為您的參與,讓程式設計更容易!!


Zippy G2 600W
Intel C2Q Q9550
Gigabyte GA-X48-DQ6
Gigabyte GV-NX96T512H-B
G-skill pi 1100MHz 2Gx4
ACARD ANS-9010 32GB (Windows7 10sec boot!!)
WD 150GB(WD1500ADFD)/640GB(6400AASK)
獻花 x0 回到頂端 [1 樓] From:未知地址 | Posted:2009-04-14 18:33 |

首頁  發表文章 發表投票 回覆文章
Powered by PHPWind v1.3.6
Copyright © 2003-04 PHPWind
Processed in 0.017617 second(s),query:16 Gzip disabled
本站由 瀛睿律師事務所 擔任常年法律顧問 | 免責聲明 | 本網站已依台灣網站內容分級規定處理 | 連絡我們 | 訪客留言