java计算器监听_计算器及事件监听
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 s = new Stack();
double m = Double.parseDouble(str[0]);
s.push(m);
for(int i=1;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);//設置窗口可見
}
}
}
總結
以上是生活随笔為你收集整理的java计算器监听_计算器及事件监听的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql创建的是拉丁_将MySQL数据
- 下一篇: java项目close wait_jav