計算器及事件監聽


import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;
public class 個人信息{
    public static void main(String arg[])
    {
        Frame f=new Frame("個人信息");
        f.setSize(250,300);
        f.setLocation(300,300);
        f.setBackground(Color.lightGray);
        f.setLayout(new FlowLayout(2));
        f.add(new JLabel("姓名:"));
        f.add(new TextField("馬蓉",20));
        f.add(new JLabel("班級:"));
        f.add(new TextField("計算機科學與技術16(D)",20));
        f.add(new JLabel("學號:"));
        f.add(new TextField("20163311115",20));
        f.add(new JLabel("性別:"));
        f.add(new TextField("女",20));
        JButton button1=new JButton("OK");
        JButton button2=new JButton("CLOSE");
        f.add(button1);
        f.add(button2);
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {        //OK按鈕事件
                JOptionPane.showMessageDialog(null,"really?","提示",JOptionPane.PLAIN_MESSAGE) ;    
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {        //CLOSE按鈕事件
                long d=e.getWhen();  //事件發生的時間
                Date date=new Date(d);   //轉換為相應的時間
                System.out.println(date);
                System.exit(0);
            }
        });
        f.setVisible(true);
    }
}

 

package 圖形界面;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
 
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel; 

 


import javax.swing.JTextField;

public class 計算器 {
      public static class Count extends JApplet implements ActionListener
      {
          private static final long serialVersionUID = 1L;
          private JTextField textField = new JTextField("開始輸入數字"); //單行文本編輯區
          String operator = "";//操作
          String input = "";//輸入的 式子
          boolean flag =  true;
      //  boolean flag1 = true;
      //  boolean flag2 = true;
          public void init()//覆寫Applet里邊的init方法
          {
              Container C = getContentPane();
              JButton b[] = new JButton[16];
              JPanel panel = new JPanel();
              C.add(textField, BorderLayout.NORTH);
              C.add(panel,BorderLayout.CENTER);
              panel.setLayout(new GridLayout(4, 4,5,5));
              String name[]={"1","2","3","+","4","5","6","-","7","8","9","*","0","C","=","/"};//設置 按鈕
              for(int i=0;i<16;i++)//添加按鈕
              {
                  b[i] = new JButton(name[i]);
                  b[i].setBackground(new Color(0,0,0));  //設置背景顏色為黑色
                  b[i].setForeground(Color.pink);//數字鍵 設置為粉色
                  if(i%4==3)
                      b[i].setForeground(Color.yellow); //設置旁邊運算符的顏色
                  b[i].setFont(new Font("楷書",Font.PLAIN,25));//設置字體格式
                  panel.add(b[i]);
                  b[i].addActionListener(this); //監聽事件
              }
              b[13].setForeground(Color.blue);//非數字鍵,即運算鍵設置為紅顏色
          }
          public void actionPerformed(ActionEvent e)  //動作事件處理方法 實現 ActionListener接口
          {
              int cnt = 0;
              String actionCommand = e.getActionCommand();  //從鍵盤獲得這個數
              if(actionCommand.equals("+")||actionCommand.equals("-")||actionCommand.equals("*") ||actionCommand.equals("/"))
                  input +=" "+actionCommand+" ";//設置輸入,把輸入的樣式改成 需要的樣子
              else if(actionCommand.equals("C"))
                  input = "";
              else if(actionCommand.equals("="))//當監聽到等號時,則處理 input
              {
                  input+= "="+compute(input);
                  textField.setText(input);//顯示對應的字符
                  input="";
                  cnt = 1;
              }
              else
                  input += actionCommand;//數字為了避免多位數的輸入 不需要加空格
              if(cnt==0)  //沒有這句不能輸出結果
              textField.setText(input);  //得到數字
          }
          private String compute(String input)//即1237 的 樣例
          {
              String str[];
              str = input.split(" ");
              Stack<Double> s = new Stack<Double>();
              double m = Double.parseDouble(str[0]);
              s.push(m);
              for(int i=1;i<str.length;i++)
              {
                  if(i%2==1)   
                  {   
                      if(str[i].compareTo("+")==0)   
                      {   
                          double help = Double.parseDouble(str[i+1]);   
                          s.push(help);   
                      }   
                         
                      if(str[i].compareTo("-")==0)   
                      {   
                          double help = Double.parseDouble(str[i+1]);   
                          s.push(-help);   
                      }   
                         
                      if(str[i].compareTo("*")==0)   
                      {   
                          double help = Double.parseDouble(str[i+1]);   
                          double ans = s.peek();//取出棧頂元素   
                          s.pop();//消棧   
                          ans*=help;   
                          s.push(ans);   
                      }   
                         
                      if(str[i].compareTo("/")==0)   
                      {   
                          double help = Double.parseDouble(str[i+1]);   
                          double ans = s.peek();   
                          s.pop();   
                          ans/=help;   
                          s.push(ans);   
                      }   
                  }   
              }   
              double ans = 0d;   
              while(!s.isEmpty())   
              {   
                  ans+=s.peek();   
                  s.pop();   
              }   
              String result = String.valueOf(ans);
              return result;
          }
          public static void main(String args[])
          {
              JFrame frame = new JFrame("計算器");
              Count applet = new Count();
              frame.getContentPane().add(applet, BorderLayout.CENTER);
              applet.init();//applet的init方法
              applet.start();//線程開始
              frame.setSize(350, 350);//設置窗口大小
              frame.setVisible(true);//設置窗口可見
          }
       
      }
    
}


免責聲明!

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



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