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("20163311106",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,"你是誰,我是誰?","提示",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);
}
}
計算器:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Stack; //Stack是一個後進先出的堆棧
public class 計算器 extends JFrame implements ActionListener {
String []An= {"7","8","9","+","4","5","6","-",
"1","2","3","*","0",".","=","/"};
JButton b[]=new JButton[16];
JTextField jt;
String input=""; //輸入的字符串
public 計算器() {
Container P=getContentPane(); //用getContentPane()方法獲得JFrame的內容面板
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
JButton b1=new JButton("清零");
JButton b2=new JButton("退格");
GridLayout g=new GridLayout(4,4,3,3); //4*4網格,間距3
jp1.setLayout(new BorderLayout()); //邊布局
jp2.setLayout(g);
jp3.setLayout(new GridLayout(1, 2,3,3));
jt=new JTextField();
jt.setPreferredSize(new Dimension(160,30)); //文本大小
jt.setHorizontalAlignment(SwingConstants.LEFT); //左對齊
P.add(jp1,BorderLayout.NORTH); //北
jp1.add(jt,BorderLayout.WEST); //西
jp1.add(jp3,BorderLayout.EAST); //東
jp3.add(b1);
jp3.add(b2);
b1.setBackground(Color.lightGray);
b2.setBackground(Color.lightGray);
b1.addActionListener(this); //定義處理事件的方法
b2.addActionListener(this);
for(int i=0;i<16;i++) //添加按鈕
{
b[i]=new JButton(An[i]);
jp2.add(b[i]);
b[i].addActionListener(this);
}
P.add(jp2,BorderLayout.CENTER);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(300, 250);
setTitle("計算器");
}
@Override
public void actionPerformed(ActionEvent e) {
int t=0;
String s=e.getActionCommand();
if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")) {
input+=" "+s+" "; //如果碰到運算符,就在運算符前後分別加一個空格
}else if(s.equals("清零")) {
input="";
}else if(s.equals("退格")) {
if((input.charAt(input.length()-1))==‘ ‘) //檢測字符串的最後一個字符是否為空格
{
input=input.substring(0,input.length()-3); //如果是則刪除末尾3個字符
}
else //否則刪除1個字符
{
input=input.substring(0,input.length()-1);
}
}
else if(s.equals("=")) {
input=compute(input);
jt.setText(input);
input="";
t=1;
}
else
input += s;
if(t==0) {
jt.setText(input);
}
}
private String compute(String str) {
String array[];
array=str.split(" ");
Stack<Double> s=new Stack<Double>();
Double a=Double.parseDouble(array[0]);
s.push(a);
for(int i=1;i<array.length;i++) {
if(i%2==1) {
if(array[i].compareTo("+")==0)
{
double b= Double.parseDouble(array[i+1]);
s.push(b);
}
if(array[i].compareTo("-")==0)
{
double b= Double.parseDouble(array[i+1]);
s.push(-b);
}
if(array[i].compareTo("*")==0)
{
double b= Double.parseDouble(array[i+1]);
double c=s.pop();
c*=b;
s.push(c);
}
if(array[i].compareTo("/")==0)
{
double b= Double.parseDouble(array[i+1]);
double c=s.peek();
s.pop();
c/=b;
s.push(c);
}
}
}
double sum=0;
while(!s.isEmpty()) {
sum+=s.pop();
}
String result=String.valueOf(sum);
return result;
}
public static void main(String[] args) {
new 計算器();
}
}
java圖形界面編程及計算器