Java-簡單的計算器(只能進行加法運算)


image

有兩個關鍵的地方:

其一: JTextField field=new JTextField(10);

這是一個文本輸入框,里面的參數10的意思是,這個輸入框的長度為10列

其二:點擊求和按鈕,出結果 第一:對求和按鈕設置監聽 第二對按下求和按鈕(按鈕動作的實現)

設置監聽:bt = new JButton("求和")

bt.addActionListener(new ActionListener() {      // 設置監聽有圓括號(關鍵詞是addActionListener)
 
         @Override  // 接下來就是按鈕動作的實現 (關鍵詞ActionEvent )
         public void actionPerformed(ActionEvent e) {
             int value1 = Integer.parseInt(t1.getText());
             int value2 = Integer.parseInt(t2.getText());
             t3.setText(Integer.toString(value1+value2));  //把加號換成乘,相應的變成乘法計算器
         }
     });

自己修改的一個模式,盡可能的接近綉花的小PS軟件

bt = new JButton("求和")

bt.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
         jiafa(e);
        }   
    });

//接下來獨立在上面的程序,在后面寫了一個程序

void jiafa(ActionEvent e) {
        int value1 = Integer.parseInt(t1.getText());
           int value2 = Integer.parseInt(t2.getText());
           t3.setText(Integer.toString(value1+value2));   
     }

 

 

經典的用java扣藍技術也是這個模式:

JMenuItem Item1;  

Item1 = new JMenuItem("摳藍");   //, undoIcon);
                Item1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            Item1_process(e);       //子菜單1處理程序
            }
        });

 

void Item1_process(ActionEvent e) {
BufferedImage image1=null;
       
           chooser.setDialogType(JFileChooser.OPEN_DIALOG);
           if(chooser.showDialog(this, null) == JFileChooser.APPROVE_OPTION) {
            try { image1 = ImageIO.read(chooser.getSelectedFile()); }
            catch(Exception ex) { return ;}
        }      
           image=koulanImage.image_add(image,image1);
        imagePanel.setImage(image);
        imagePanel.repaint();          
    }

 

在同一個包下有koulanImage.java 綜合起來就能扣藍了

 

 

加法計算器的源程序如下

package dsfa;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
 
class AddDemo extends JFrame {
 
    JLabel b1, b2;
    JTextField t1, t2, t3;
    JButton bt;
 
    public AddDemo() {
        b1 = new JLabel("請你輸入第一個數", JLabel.CENTER);
        b2 = new JLabel("請你輸入第二個數", JLabel.CENTER);
        b1.setBorder(BorderFactory.createEtchedBorder());
        b2.setBorder(BorderFactory.createEtchedBorder());
        t1 = new JTextField(2);
        t2 = new JTextField(2);
        t3 = new JTextField(2);
        t3.setEditable(false);
        bt = new JButton("求和");
        setLayout(new GridLayout(3, 2));
        add(b1);
        add(t1);
        add(b2);
        add(t2);
        add(bt);
        add(t3);
        bt.addActionListener(new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent e) {
                int value1 = Integer.parseInt(t1.getText());
                int value2 = Integer.parseInt(t2.getText());
                t3.setText(Integer.toString(value1+value2));
            }
        });
        setSize(500, 500);
        setVisible(true);
    //    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    public static void main(String arg[]) {
        new AddDemo();
    }
}

 


下面這個程序在遲靜老師最后一節的幾何產品設計課前做的

package dsfa;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
 
class AddDemo extends JFrame {
 
    JLabel b1, b2;
    JTextField t1, t2, t3;
    JButton bt;
 
    public AddDemo() {
        b1 = new JLabel("請你輸入第一個數", JLabel.CENTER);
        b2 = new JLabel("請你輸入第二個數", JLabel.CENTER);
        b1.setBorder(BorderFactory.createEtchedBorder());
        b2.setBorder(BorderFactory.createEtchedBorder());
        t1 = new JTextField(2);
        t2 = new JTextField(2);
        t3 = new JTextField(2);
        t3.setEditable(false);
        bt = new JButton("求和");
        setLayout(new GridLayout(3, 2));
        add(b1);
        add(t1);
        add(b2);
        add(t2);
        add(bt);
        add(t3);
        bt.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e) {
           
             jiafa(e); 
            }  
            
        });

      
        setSize(500, 500);
        setVisible(true);

    }
 
        void jiafa(ActionEvent e) {   //遺留問題
        int value1 = Integer.parseInt(t1.getText());
           int value2 = Integer.parseInt(t2.getText());
           t3.setText(Integer.toString(value1+value2));    
     }

    public static void main(String arg[]) {
        new AddDemo();
    }
}

 

 

遺留問題:void jiafa(ActionEvent e)  將這個拿進構造方法里面去,編譯顯示錯誤


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM