伊莉討論區
標題:
發一個GUI的簡易計算機
[打印本頁]
作者:
闇冰
時間:
2012-2-20 03:55 PM
標題:
發一個GUI的簡易計算機
這個計算機是我在學GUI時寫出來的
但寫完之後才發現...
沒有加上ESC的功能==
記得的時候都太遲了...
因為很想睡的關係就沒有再更新了...
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FracCalc implements ActionListener {
int fill[] = {
GridBagConstraints.BOTH,
GridBagConstraints.VERTICAL,
GridBagConstraints.HORIZONTAL,
GridBagConstraints.NONE };
int anchor[] = { GridBagConstraints.CENTER,
GridBagConstraints.EAST,
GridBagConstraints.SOUTH,
GridBagConstraints.SOUTHEAST,
GridBagConstraints.SOUTHWEST,
GridBagConstraints.WEST,
GridBagConstraints.NORTH,
GridBagConstraints.NORTHEAST,
GridBagConstraints.NORTHWEST };
int att[] [] = { {0, 0, 4, 1, fill[2], anchor[0],1, 1, 0, 0, 3, 0}, // use GridBagLayout
{0, 1, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{2, 1, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{3, 1, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{0, 2, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{1, 2, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{2, 2, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{3, 2, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{0, 3, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{1, 3, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{2, 3, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{3, 3, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{0, 4, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{1, 4, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{2, 4, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
{3, 4, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
};
JFrame frame = new JFrame("calculater");
Label text=new Label("0",Label.RIGHT);
JButton button[]={ new JButton("7"),new JButton("8"), new JButton("9"),new JButton("+"),
new JButton("4"),new JButton("5"), new JButton("6"),new JButton("-"),
new JButton("1"),new JButton("2"), new JButton("3"),new JButton("*"),
new JButton("0"),new JButton("."), new JButton("="),new JButton("/"),
};
private void add(JFrame jf, Component b, int a[]){ //
GridBagConstraints cons = new GridBagConstraints();
cons.gridx = a[0]; // X
cons.gridy = a[1]; // Y
cons.gridwidth = a[2]; //
cons.gridheight = a[3]; //
cons.fill = a[4]; //
cons.anchor = a[5]; //
cons.weightx = a[6]; //
cons.weighty = a[7]; //
cons.insets = new Insets(a[8], a[9], a[10], a[11]); //
jf.add(b, cons); //
}
char tempChar = '+'; // save +-*/
boolean specialChar = true; // to know +-*/
String nowNumber = "0"; // show 0 in the text when it start
double tempNumber = 0; // the number for now
Boolean point = false; // "."
Boolean typeTF = false; // to find there are word to enter
public FracCalc() {
frame.setLayout(new GridBagLayout()); // using GridBagLayout to put things on the frame
text.setBackground(new Color (0xFF,0xFF,0xCC)); // color of the text
text.setText("0"); // label shows "0"
add(frame, text, att[0]); // put text in the frame
for (int i=0; i<button.length; i++){ // use for to put the button in the frame
add(frame, button[i], att[i+1]);
button[i].addActionListener(this);
}
frame.pack(); // make it shows the button and text when it start
frame.setVisible(true); // show the frame
}
public static void main(String[] args) {
new FracCalc();
}
private void compute(String num){ // for compute
point = cmoparePoint(); // is a point already there or not
char [] nextNumber = num.toCharArray(); // make the char to array
if(!typeTF && (nextNumber[0] >= '0' && nextNumber[0] <= '9')){ // make the nowNumber become 0 if no word enter in
nowNumber = ""; // clear nowNumber
}
switch(nextNumber[0]){
case '1':
changeNowNumber("1"); //change label number
break;
case '2':
changeNowNumber("2");
break;
case '3':
changeNowNumber("3");
break;
case '4':
changeNowNumber("4");
break;
case '5':
changeNowNumber("5");
break;
case '6':
changeNowNumber("6");
break;
case '7':
changeNowNumber("7");
break;
case '8':
changeNowNumber("8");
break;
case '9':
changeNowNumber("9");
break;
case '0':
if(!nowNumber.equalsIgnoreCase("0")){
changeNowNumber("0");
}
break;
default:
// is special char and some number to enter in
if(specialChar && typeTF){
specialChar = false;
switch(tempChar){
case '/':
if(tempNumber != 0){
tempNumber /= Double.parseDouble(text.getText());
}
break;
case '*':
tempNumber *= Double.parseDouble(text.getText());
break;
case '+':
tempNumber += Double.parseDouble(text.getText());
break;
case '-':
tempNumber -= Double.parseDouble(text.getText());
break;
}
text.setText("" + tempNumber);
typeTF = false;
}
else{
nowNumber = "";
}
specialChar = true;
}
// special char
if(specialChar){
switch(nextNumber[0]){
case '/':
tempChar = '/';
tempNumber = Double.parseDouble(text.getText());
break;
case '*':
tempChar = '*';
tempNumber = Double.parseDouble(text.getText());
break;
case '+':
tempChar = '+';
tempNumber = Double.parseDouble(text.getText());
break;
case '-':
tempChar = '-';
tempNumber = Double.parseDouble(text.getText());
break;
case '=':
tempChar = '=';
text.setText("" + tempNumber);
break;
}
}
}
public void changeNowNumber(String nowNum){
if(nowNumber.equals("0")){
nowNumber="";
}
nowNumber+=nowNum;
text.setText(""+nowNumber);
typeTF=true;
}
public Boolean cmoparePoint(){ // define there are the point or more than one point
char str[] = nowNumber.toCharArray();
for (int i=0;i<str.length;i++){
if (str[i]=='.'){return true;}
}
return false;
}
public void actionPerformed(ActionEvent e) {
JButton number= (JButton)e.getSource();
compute(number.getText());
}
}
複製代碼
[attach]70747971[/attach]
歡迎光臨 伊莉討論區 (http://s03.p02.eyny.com/)
Powered by Discuz!