JAVA-GUI AWT中的三種佈局 事件監聽 輸入框 標籤 用三種不同的程式碼形式搞定計算器2021.2.8
阿新 • • 發佈:2021-02-09
面板 panel
可以看作一個空間,但是不能單獨存在,要放在
使用介面卡模式,寫關閉視窗辦法
//監聽視窗 System.exit(e) ,
this.addWindowListener(new WindowAdapter() {
@Override
//視窗關閉時需要做的事情
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
panel的相關內容
要把面板放在窗口裡,所以必須先建立一個視窗
package com.xiucai;
import java.awt.*;
public class textpanel {
public static void main(String[] args) {
//先例項化一個視窗
Myframe myframe = new Myframe(200, 200, 200, 200, Color.RED);
myframe.setLayout(null);
//佈局的概念
Panel panel = new Panel();
//設定佈局
panel.setLayout(null);
//座標,相對於frame,東西都是放在面板裡的
panel.setBounds(100,100,100,100);
panel.setBackground(Color.GREEN);
// 把這個面板放到窗口裡
myframe.add(panel);
// panel.setVisible(true);
}
}
三種管理佈局
setLayout
- 流失佈局:從左到右
- 上下結構的
- 表格佈局
FlowLayout,流式佈局
myframe.setLayout(new FlowLayout (FlowLayout.LEFT));
物件.setLayout(佈局方式(所在位置))
東西南北中 BorderLayout
關鍵程式碼:
myframe.setLayout(new BorderLayout());
myframe.add(east,BorderLayout.EAST);
物件.add(元件名,元件所在位置)
表格佈局 GridLayout
自動排布
小練習
package com.xiucai;
//使用表格跟流式佈局完成的
import java.awt.*;
public class testdomo1 {
public static void main(String[] args) {
//新建一個視窗
Myframe myframe = new Myframe(300, 300, 300, 300, Color.white);
//先分為上下兩個部分
myframe.setLayout(new GridLayout(2,1));
// 上面部分
Panel panel_up = new Panel();
panel_up.setBackground(Color.red);
panel_up.setLayout(new FlowLayout());
myframe.add(panel_up);//載入面板
//載入左右中結構部分
Button left = new Button("lefr");
Button right = new Button("right");
Panel mid = new Panel(); //中部結構
mid.setLayout(new GridLayout(2,1));
mid.setBackground(Color.BLACK);
panel_up.add(left);
panel_up.add(mid);//中間
panel_up.add(right);
//往整個上面中間結構裡邊新增按鈕
Button mid_up = new Button("up");
Button mid_down = new Button("down");
mid.add(mid_up);
mid.add(mid_down);
//下面部分
// 下面三個大布局完成
Panel panel_down = new Panel();
myframe.add(panel_down);
panel_down.setLayout(new FlowLayout());
panel_down.setBackground(Color.pink);
Panel panel_down_left = new Panel();
Panel panel_down_mid = new Panel();
panel_down_mid.setLayout(new GridLayout(2,2));
Panel panel_down_right = new Panel();
panel_down.add(panel_down_left);
panel_down.add(panel_down_mid);
panel_down.add(panel_down_right);
panel_down.setBackground(Color.BLACK);
panel_down.setBackground(Color.BLUE);
panel_down.setBackground(Color.white);
//往裡邊增加按鈕
Button down_left = new Button("down_left");
Button down_rigth = new Button("down_rigth");
Button button1 = new Button("1");
Button button2 = new Button("2");
Button button3 = new Button("3");
Button button4 = new Button("4");
panel_down_left.add(down_left);
panel_down_right.add(down_rigth);
panel_down_mid.add(button1);
panel_down_mid.add(button2);
panel_down_mid.add(button3);
panel_down_mid.add(button4);
myframe.pack();
}
}
事件監聽
當事件發生的時候我們幹什麼。
先宣告一個監聽類,裡面說明 該事件執行以後做什麼
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
MyActionListener myActionListener = new MyActionListener();
元件物件.addActionListener(myActionListener);
多個按鈕使用一個監聽事件類
需要使用兩個命令
button.setActionCommand("1"); //設定活動標記,在元件裡邊設定
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
} //在事件類裡邊設定,傳一個可以區別判別的引數。
輸入框
TextField textField=new TextField();
frame.add(textField); //基本顯示程式碼
MyTextactionListnner myTextactionListnner = new MyTextactionListnner();
//按下回車就是
//誰知替換編碼
textField.setEchoChar('*');//設定一個輸入的時候不顯示文字的內容
textField.addActionListener(myTextactionListnner);//事件
事件類:實現在文字框輸入文字以後按下回車,文字消失
class MyTextactionListnner implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField textField= (TextField) e.getSource();//獲得一些資源,返回一個物件
System.out.println(textField.getText());//獲得輸入框中的文字
textField.setText("");
}
}
標籤 label
Label label=new Label("+");
不使用組合的計算器,面向過程
package com.xiucai;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextCalc {
public static void main(String[] args) {
new Calculator(200,200,200,200,Color.white);
}
}
class Calculator extends Myframe {
public Calculator(int x, int y, int w, int h, Color color) throws HeadlessException {
super(x, y, w, h, color);
//定義元件 三個文字框 一個標籤 一個按鈕
TextField textField = new TextField(10);//最大能寫幾個字元數
TextField textFiel2 = new TextField(10);//最大能寫幾個字元數
TextField textFiel3 = new TextField(10);//最大能寫幾個字元數
Button button = new Button("=");
Label label=new Label("+");
//佈局
this.setLayout(new FlowLayout());
add(textField);
add(label);
add(textFiel2);
add(button);
add(textFiel3);
pack();
//設定監聽
MyListenerAction myListenerAction = new MyListenerAction(textField, textFiel2, textFiel3);
button.addActionListener(myListenerAction);
};
}
class MyListenerAction implements ActionListener {
// private TextField t1,t2, t3;
//把上邊的程式碼替換掉,用組合的方式進行計算器物件的宣告
Calculator calculator = null;
//建構函式實現,不用進行傳入的物件甄別,直接呼叫物件即可
public MyListenerAction(TextField t1, TextField t2, TextField t3) {
this.t1 = t1;
this.t2 = t2;
this.t3 = t3;
}
@Override
public void actionPerformed(ActionEvent e) {
Integer num1;
Integer num2;
num1 = Integer.parseInt(t1.getText());
num2= Integer.parseInt(t2.getText());
t3.setText(String.valueOf(num1+num2));
t1.setText("");
t2.setText("");
}
}
使用組合的計算器
package com.xiucai;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextCalc {
public static void main(String[] args) {
new Calculator(200,200,200,200,Color.white).loadFrame();
}
}
class Calculator extends Myframe {
//屬性
TextField textField,textFiel2, textFiel3;
//方法
public void loadFrame() {
textField = new TextField(10);//最大能寫幾個字元數
textFiel2 = new TextField(10);//最大能寫幾個字元數
textFiel3 = new TextField(10);//最大能寫幾個字元數
Button button = new Button("=");
Label label=new Label("+");
//佈局
this.setLayout(new FlowLayout());
add(textField);
add(label);
add(textFiel2);
add(button);
add(textFiel3);
pack();
//設定監聽
MyListenerAction myListenerAction = new MyListenerAction(this);
button.addActionListener(myListenerAction);
}
public Calculator(int x, int y, int w, int h, Color color) throws HeadlessException {
super(x, y, w, h, color);
//定義元件 三個文字框 一個標籤 一個按鈕
};
}
class MyListenerAction implements ActionListener {
// private TextField t1,t2, t3;
//把上邊的程式碼替換掉,用組合的方式進行計算器物件的宣告
Calculator calculator = null;
//建構函式實現,不用進行傳入的物件甄別,直接呼叫物件即可
public MyListenerAction(Calculator calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
int num1, num2, num3;
num1 = Integer.parseInt(calculator.textField.getText());
num2 = Integer.parseInt(calculator.textFiel2.getText());
num3 = num1 + num2;
calculator.textFiel3.setText(String.valueOf(num3));
calculator.textField.setText("");
calculator.textFiel2.setText("");
}
}
組合的思想就是傳入物件。不再是傳入引數
內部類的計算器
內部類最大的好處,就是可以暢通無阻的訪問外部類的屬性跟方法。
package com.xiucai;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextCalc {
public static void maAin(String[] args) {
new Calculator(200,200,200,200,Color.white).loadFrame();
}
}
class Calculator extends Myframe {
//屬性
TextField textField,textFiel2, textFiel3;
//方法
public void loadFrame() {
textField = new TextField(10);//最大能寫幾個字元數
textFiel2 = new TextField(10);//最大能寫幾個字元數
textFiel3 = new TextField(10);//最大能寫幾個字元數
Button button = new Button("=");
Label label=new Label("+");
//佈局
this.setLayout(new FlowLayout());
add(textField);
add(label);
add(textFiel2);
add(button);
add(textFiel3);
pack();
//設定監聽
MyListenerAction myListenerAction = new MyListenerAction();
button.addActionListener(myListenerAction);
}
public Calculator(int x, int y, int w, int h, Color color) throws HeadlessException {
super(x, y, w, h, color);
//定義元件 三個文字框 一個標籤 一個按鈕
};
class MyListenerAction implements ActionListener {
// private TextField t1,t2, t3;
//把上邊的程式碼替換掉,用組合的方式進行計算器物件的宣告
//建構函式實現,不用進行傳入的物件甄別,直接呼叫物件即可
@Override
public void actionPerformed(ActionEvent e) {
int num1, num2, num3;
num1 = Integer.parseInt(textField.getText());
num2 = Integer.parseInt(textFiel2.getText());
num3 = num1 + num2;
textFiel3.setText(String.valueOf(num3));
textField.setText("");
textFiel2.setText("");
}
}
}