JAVA專案一:圖形化介面計算器
阿新 • • 發佈:2019-01-27
最終效果圖:
專案流程:
第一步:實現圖形化介面(新增計算器的 Button 和 用於顯示輸入數字、輸出結果的JTextField等)
第二步:給按鈕和文字框新增滑鼠監聽事件。
第三步:實現加減乘除、開方、平方、清零和退格功能。
開方運算:
平方運算:
加法運算:
減法運算:
乘法運算:
除法運算:
完整專案程式碼:
package First_App;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Caculator extends JFrame{
/*
* 圖形化介面設計
* */
private static final long serialVersionUID = 4907149509182425824L;
public Caculator(){
Container c = getContentPane(); //定義一個頂級容器c
setLayout(new GridLayout(2,1));//新建網格佈局管理器,2行1列
JTextField jtf = new JTextField("0",40);//構造一個用指定文字和列初始化的新文字框--jtf
jtf.setHorizontalAlignment(JTextField.RIGHT);//設定水平對齊方式:居右對齊
JButton data0 = new JButton("0");
JButton data1 = new JButton("1");
JButton data2 = new JButton("2");
JButton data3 = new JButton("3");
JButton data4 = new JButton("4");
JButton data5 = new JButton("5");
JButton data6 = new JButton("6");
JButton data7 = new JButton("7");
JButton data8 = new JButton("8");
JButton data9 = new JButton("9");
JButton point = new JButton(".");
JButton equ = new JButton("=");
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton mtp = new JButton("*");
JButton dvd = new JButton("/");
JButton sqr = new JButton("sqrt");
JButton root = new JButton("x^2");
JButton tg = new JButton("退格");
JButton ql = new JButton("清零");
JPanel jp = new JPanel(); //新建JPanel面板--jp
jp.setLayout(new GridLayout(4,5,5,5));//新建網格佈局管理器(行數,列數,元件間的水平垂直間距)
jp.add(data7);
jp.add(data8);
jp.add(data9);
jp.add(plus);
jp.add(sqr);
jp.add(data4);
jp.add(data5);
jp.add(data6);
jp.add(minus);
jp.add(root);
jp.add(data1);
jp.add(data2);
jp.add(data3);
jp.add(mtp);
jp.add(ql);
jp.add(data0);
jp.add(point);
jp.add(equ);
jp.add(dvd);
jp.add(tg);
c.add(jtf);//將文字框jtf新增到頂級容器c中
c.add(jp);//將JPanel面板jp新增到頂級容器c中
setSize(400,300);
setTitle("計算器");
setVisible(true);
setResizable(false);//窗體大小由程式設計師決定,使用者不能自由改變大小
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
/*
* *********************************************************
*
相關計算功能的實現
* *********************************************************
* */
data0.addActionListener(new ActionListener(){//數字0的輸入
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){//將按鈕值與0作比較
jtf.requestFocus();//把輸入焦點放在呼叫這個方法的控制元件上(即把游標放在文字框jtf裡)
}
else{
String str = jtf.getText();//取得當前按鈕的按鈕值
jtf.setText(str+"0"); //將文字內容後加上字元0
}
}
});
data1.addActionListener(new ActionListener(){//數字1的輸入
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){//將按鈕值與0作比較
jtf.setText("");//將文字框初始化為空
jtf.setText("1");//將文字框內容置為 1
jtf.requestFocus();//把輸入焦點放在呼叫這個方法的控制元件上(即把游標放在文字框jtf裡)
}
else{
String str = jtf.getText();//取得當前按鈕的按鈕值
jtf.setText(str+"1"); //將文字內容後加上字元1
}
}
});
data2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){
jtf.setText("");
jtf.setText("2");
jtf.requestFocus();
}
else{
String str = jtf.getText();
jtf.setText(str+"2");
}
}
});
data3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){
jtf.setText("");
jtf.setText("3");
jtf.requestFocus();
}
else{
String str = jtf.getText();
jtf.setText(str+"3");
}
}
});
data4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){
jtf.setText("");
jtf.setText("4");
jtf.requestFocus();
}
else{
String str = jtf.getText();
jtf.setText(str+"4");
}
}
});
data5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){
jtf.setText("");
jtf.setText("5");
jtf.requestFocus();
}
else{
String str = jtf.getText();
jtf.setText(str+"5");
}
}
});
data6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){
jtf.setText("");
jtf.setText("6");
jtf.requestFocus();
}
else{
String str = jtf.getText();
jtf.setText(str+"6");
}
}
});
data7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){
jtf.setText("");
jtf.setText("7");
jtf.requestFocus();
}
else{
String str = jtf.getText();
jtf.setText(str+"7");
}
}
});
data8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){
jtf.setText("");
jtf.setText("8");
jtf.requestFocus();
}
else{
String str = jtf.getText();
jtf.setText(str+"8");
}
}
});
data9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){
jtf.setText("");
jtf.setText("9");
jtf.requestFocus();
}
else{
String str = jtf.getText();
jtf.setText(str+"9");
}
}
});
point.addActionListener(new ActionListener(){ //點號的輸入
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){
jtf.setText("");
jtf.setText(".");
jtf.requestFocus();
}
else{
String str = jtf.getText();
jtf.setText(str+".");
}
}
});
plus.addActionListener(new ActionListener(){ //+號的輸入
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){
jtf.setText("");
jtf.setText("+");
jtf.requestFocus();
}
else{
String str = jtf.getText();
jtf.setText(str+"+");
}
}
});
minus.addActionListener(new ActionListener(){ //-號的輸入
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){
jtf.setText("");
jtf.setText("-");
jtf.requestFocus();
}
else{
String str = jtf.getText();
jtf.setText(str+"-");
}
}
});
mtp.addActionListener(new ActionListener(){ //*號的輸入
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){
jtf.setText("");
jtf.setText("*");
jtf.requestFocus();
}
else{
String str = jtf.getText();
jtf.setText(str+"*");
}
}
});
dvd.addActionListener(new ActionListener(){ //除號的輸入
public void actionPerformed(ActionEvent arg0){
if(jtf.getText().equals("0")){
jtf.setText("");
jtf.setText("/");
jtf.requestFocus();
}
else{
String str = jtf.getText();
jtf.setText(str+"/");
}
}
});
//【**退格功能如下**】
tg.addActionListener(new ActionListener(){//監聽退格鍵
public void actionPerformed(ActionEvent arg0){//處理退格鍵被按下的事件
String text = jtf.getText();
int i = text.length();
if(i>0){
text = text.substring(0,i-1);//去掉最後一個字元
if (text.length() == 0) {// 如果文字沒有了內容,則初始化計算器的各種值
jtf.setText("0");
} else { // 顯示新的文字
jtf.setText(text);
}
}
}
});
//【**清零功能如下**】
ql.addActionListener(new ActionListener(){//監聽清零鍵
public void actionPerformed(ActionEvent e) {
jtf.setText("0");//將文字框置為0(清零功能)
}
});
//【**平方功能如下**】
root.addActionListener(new ActionListener(){//監聽root鍵
public void actionPerformed(ActionEvent e){//root鍵被按事件
String i = jtf.getText();
Double j = Double.parseDouble(i);//將字串i轉換成對應的double型別的數值
double ans = j*j; //求平方
String answer =String.valueOf(ans);//將int型資料轉換成String型別
jtf.setText(answer);//將文字框設定為平方後的結果
}
});
//【**開方功能如下**】
sqr.addActionListener(new ActionListener(){//監聽sqrt鍵
public void actionPerformed(ActionEvent e){//sqrt鍵被按事件
String i = jtf.getText();
Double j = Double.parseDouble(i);//將字串轉換成對應的double型別的數值
double ans = (double)Math.sqrt(j);//求開方
String answer = String.valueOf(ans);//將double型資料轉換成String型別
jtf.setText(answer);//將文字框設定為開方後的結果
}
});
//【等號實現 加減乘除 功能】
equ.addActionListener(new ActionListener(){ //監聽 “等號” 按鍵
public void actionPerformed(ActionEvent arg0){//處理“等號” 按鍵被按下事件
//【**加法運算**】
if(jtf.getText().indexOf("+")!= -1){
//將字串分割為子字串,然後將結果作為字串陣列返回
String[] s = jtf.getText().split("[+]");//轉義字元,要用"[+]"或者"\+"
Double d1 = Double.parseDouble(s[0]);//返回一個指定字串表示的double值
Double d2 = Double.parseDouble(s[1]);
double ans = d1 + d2;
String answer = String.valueOf(ans);//將結果轉換為字串
jtf.setText(answer);//將加法運算的結果以字串形式在文字框中顯示
}
//【**減法運算**】
else if(jtf.getText().indexOf("-")!= -1){
String[] s = jtf.getText().split("-");
jtf.setText("");
Double d1 = Double.parseDouble(s[0]);
Double d2 = Double.parseDouble(s[1]);
double ans = d1-d2;
String answer =String.valueOf(ans);
jtf.setText(answer);
}
//【**乘法運算**】
else if(jtf.getText().indexOf("*")!= -1){
String[] s = jtf.getText().split("[*]");//*是轉義字元,要用"[*]",或者"\*"
jtf.setText("");
Double d1 = Double.parseDouble(s[0]);
Double d2 = Double.parseDouble(s[1]);
double ans = d1*d2;
String answer =String.valueOf(ans);
jtf.setText(answer);
}
//【**除法運算**】
else if(jtf.getText().indexOf("/")!= -1){
String[] s = jtf.getText().split("/");
jtf.setText("");
Double d1 = Double.parseDouble(s[0]);
Double d2 = Double.parseDouble(s[1]);
double ans = d1/d2;
String answer =String.valueOf(ans);
jtf.setText(answer);
}
else{
jtf.setText("請選擇要進行的運算");
}
}
});
}
public static void main(String[] args) {
new Caculator();
}
}
總結:
1.掌握基本的GUI新增按鈕、文字框的方法
2.掌握字串的處理,這裡用到了indexOf()、split()等方法
3.注意Java中遇到的轉義字元。
歡迎轉載,討論,共同進步!!!