GUI的核心技術:Swing AWT(使用的較少)
原因
-
界面不美觀
-
需要JRE環境
學習原因
-
可以寫出自己想要的一些工具
-
工作的時候,可能需要維護到Swing界面,小概率事件
-
了解MVC架構,了解監聽
AWT
AWT介紹
-
包含了很多類和接口 GUI:圖形用戶界面
Eeclipes :java寫的(很老了)
-
元素:窗口、按鈕、文本框
-
java.awt包
窗口
import java.awt.*;
public class Frame01 {
public static void main(String[] args) {
//Frame
// public Frame() throws HeadlessException{//源碼
// public Frame(String title) throws HeadlessException {
// init(title, null);
// }
Frame frame=new Frame("阿嬈");
//需要設置可見性操作
frame.setVisible(true);
//設置窗口大小
frame.setSize(400,440);
//設置顏色
frame.setBackground(new Color(218, 102, 250));
//彈出的初始位置
frame.setLocation(200,020);
//設置大小固定
frame.setResizable(false);//true可以改變窗口大小
}
}
不能點擊×關閉,可以用停止運行關閉窗口
可以自己做一個Frame減少代碼量,通過調用來建立窗口 class繼承
import java.awt.*;
public class Frame02 {
public static void main(String[] args) {
KimiFrame kimiFrame1=new KimiFrame(100,100,400,400,Color.blue);
KimiFrame kimiFrame2=new KimiFrame(400,400,400,400,Color.red);
KimiFrame kimiFrame3=new KimiFrame(100,400,400,400,Color.black);
KimiFrame kimiFrame4=new KimiFrame(400,400,400,400,Color.orange);
}
}
class KimiFrame extends Frame{
static int id=0;
public KimiFrame(int x,int y,int w,int h,Color color){
super("KimiFrame"+(++id));
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
}
面板
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//布局設置
frame.setLayout(null);
//坐標
frame.setBounds(300,300,330,330);
frame.setBackground(new Color(0xF306F3));
//panel設置坐標和顏色,相對於frame
panel.setBounds(50,50,200,200);
panel.setBackground(new Color(0x0C0CEE));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);//可見
//監聽實踐,監聽創就關閉事件 System.exit(0);
//適配器模式,可以解決關閉問題
frame.addWindowListener(new WindowAdapter() {
//窗口關閉是需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//結束程序
super.windowClosing(e);
System.exit(0);
}
});
}
布局管理
-
流式布局
Frame frame = new Frame();
//組件——按鈕
Button button01 = new Button("button01");
Button button02 = new Button("button02");
Button button03 = new Button("button03");
Button button04 = new Button("button04");
//流式布局
//frame.setLayout(new FlowLayout());
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.setSize(200,300);
//添加按鈕
frame.add(button01);
frame.add(button02);
frame.add(button03);
frame.add(button04);
frame.setVisible(true); -
東西南北中
Frame frame = new Frame("東西南北中");
//組件——按鈕中是Center
Button east = new Button("east");
Button west = new Button("west");
Button south = new Button("south");
Button north = new Button("north");
//東西南北中
//添加按鈕
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.setVisible(true); -
表格式布局
Frame frame = new Frame("GridLayout");
//組件——按鈕
Button button01 = new Button("button01");
Button button02 = new Button("button02");
Button button03 = new Button("button03");
Button button04 = new Button("button04");
Button button05 = new Button("button05");
Button button06 = new Button("button06");
frame.setLayout(new GridLayout(3,2));
frame.add(button01);
frame.add(button02);
frame.add(button03);
frame.add(button04);
frame.add(button05);
frame.add(button06);
frame.pack();//java的,自動選擇一個合適的位置
frame.setVisible(true);
練習
-
分析過程
-
代碼實現
import java.awt.*;
public class TEXT {
public static void main(String[] args) {
//總Frame
Frame frame = new Frame();
frame.setSize( 400, 300);frame. setLocation( 300, 400);
frame.setBackground(Color.BLACK);frame.setVisible(true ) ;
frame. setLayout(new GridLayout( 2, 1));
//4個面板
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout( 2, 1)) ;Panel p3 = new Panel(new BorderLayout( ));
Panel p4 = new Panel(new GridLayout( 2, 2) );
p1.add( new Button( "East-1"),BorderLayout.EAST);
p1.add(new Button( "west-1"),BorderLayout.WEST);
p2.add ( new Button( "p2-btn-1"));
p2.add ( new Button( "p2-btn-2"));
p1.add(p2,BorderLayout.CENTER);
//下面
p3.add(new Button( "East-2"),BorderLayout.EAST);
p3.add(new Button( "west-2" ),BorderLayout.WEST);
//中間4個
for (int i = 0; i < 4; i++) {
p4.add ( new Button( "for-"+i));
}
p3.add(p4, BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.setVisible(true);
}
}
總結:
-
Frame是一個頂級窗口
-
Panel無法單獨顯示,必須添加到某個容器中。
-
布局管理器
-
流式
-
東西南北中
-
表格
-
大小,定位,背景顏色,可見性,監聽!
-
事件監聽
-
事件的監聽、處理流程:(參考了某位大大的csdn,我覺得他說的很好)
-
事件源、事件、監聽器之間的關系:
-
事件源通過事件監聽器注冊方法,為自身添加事件監聽器
-
事件監聽器中,實現了事件的handler方法:根據傳入的事件或事件的事件源,執行具體操作
-
事件源的某個動作,將(主動)觸發事件監聽器的handler方法處理事件
-
公用的可以不需要再寫直接調用
-
一個按鈕寫一個監聽
//關閉窗口事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Actional01 {
public static void main(String[] args) {
//按下按鈕,觸發一些事件
Frame frame= new Frame();
Button button=new Button();
//因為.addActionListener()需要一個ActionListener所以我們需要構造一個ActionListener
MyListen myListen=new MyListen();
button.addActionListener(myListen);
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
windowClose(frame);
}
//關閉窗口事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
點擊面板會打印ar
一個按鈕寫多個按鈕
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Action02 {
public static void main(String[] args) {
//兩個按鈕實現同一個監聽,開始結束
Frame frame = new Frame("開始結束");
Button button = new Button("start");
Button button1 = new Button("stop");
button1.setActionCommand("stop");
MyListener myListener = new MyListener();
button.addActionListener(myListener);
button1.addActionListener(myListener);
frame.add(button,BorderLayout.NORTH);
frame.add(button1,BorderLayout.CENTER);
frame.setVisible(true);
windowClose(frame);
}
//關閉窗口事件
private static void windowClose(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("點擊"+e.getActionCommand());
e.getActionCommand();
}
}
點擊start 打印 點擊start
點擊stop 打印 點擊stop
制作計算器
-
基礎
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Cal {
public static void main(String[] args) {
//啟動main里面正常只有一行啟動代碼
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField =new TextField();
add(textField);
//監聽這個文本框上輸入的內容
MyActionListener01 myActionListener01 = new MyActionListener01();
//按enter后會將輸入框中的內容打印輸出
textField.addActionListener(myActionListener01);
//設置替換編碼
textField.setEchoChar('*');//前端獲取的是*號,后端獲取的是輸入的內容密碼將常會用哦
setVisible(true);
pack();
// windowClose(0);
}
//關閉窗口事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener01 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField file=(TextField) e.getSource();//獲取一些資源,強制轉換成TextFild類型
System.out.println(file.getText());//獲得文本框中的內容
file.setText(" ");//null回車后清除
}
}
-
計算器編寫
oop原則:組合大於繼承(狂神yyds)
狂神yyds
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Cal02 {
public static void main(String[] args) {
new Calculator();
}
}
//計算器類
class Calculator extends Frame{
public Calculator(){
//分析結構
//3個文本框輸入兩個數,輸出兩個數
TextField textField1 = new TextField(10);
TextField textField2 = new TextField(10);
TextField textField3 = new TextField(20);
//一個按鈕等號,給button添加監聽時間能夠計算
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(textField1,textField2,textField3));//傳參
//一個標簽+號
Label label = new Label("+");
//布局流式布局
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button) ;
add(textField3);
pack();
setVisible(true);
}
}
//主要需要做的事,給等號一個監聽時間保證能等於數
class MyCalculatorListener implements ActionListener{
private TextField textField1,textField2,textField3;
public MyCalculatorListener(TextField textField1,TextField textField2,TextField textField3){
this.textField1=textField1;
this.textField2=textField2;
this.textField3=textField3;
}
@Override
public void actionPerformed(ActionEvent e) {
//獲取變量,構造里獲得加數
int n1 = Integer.parseInt(textField1.getText());
int n2 = Integer.parseInt(textField2.getText());
//加法運算后返回到第三個
textField3.setText(""+(n1+n2));
//清楚第三個
textField1.setText("");
textField2.setText("");
}
}
點擊等號前倆加數會清除,等號后計算出結果
面向對象寫法
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Cal02 {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//計算器類
class Calculator extends Frame {
//屬性
TextField textField1, textField2, textField3;
public void loadFrame() {
//組件加布局
textField1 = new TextField(10);
textField2 = new TextField(10);
textField3 = new TextField(20);
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(this));//把自己丟給自己
Label label = new Label("+");
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
}
}
//主要需要做的事,給等號一個監聽時間保證能等於數
class MyCalculatorListener implements ActionListener{
//在一個類中用另一個類
Calculator calculator=null;
public MyCalculatorListener( Calculator calculator){
this.calculator=calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//獲取變量,構造
int n1 = Integer.parseInt(calculator.textField1.getText());
int n2 = Integer.parseInt(calculator.textField2.getText());
calculator.textField3.setText(""+(n1+n2));
calculator.textField1.setText("");
calculator.textField2.setText("");
}
}
內部類(更好的包裝)可以更好的訪問外部類
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Cal02 {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//計算器類
class Calculator extends Frame {
//屬性
TextField textField1, textField2, textField3;
public void loadFrame() {
//組件加布局
textField1 = new TextField(10);
textField2 = new TextField(10);
textField3 = new TextField(20);
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener());//把自己丟給自己
Label label = new Label("+");
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
}
class MyCalculatorListener implements ActionListener{
//在一個類中用另一個類
畫筆
package com.ar.chapter02;
import java.awt.*;
public class Paint01{
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame {
public void loadFrame() {
setBounds(100, 100, 400, 300);
setVisible(true);
}
//畫筆
鼠標監聽
點擊
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class MouseListener {
public static void main(String[] args) {
new MyFrame("畫圖");
}
}
//類
class MyFrame extends Frame{
//畫畫需要畫筆,需要監聽鼠標所在位置,需要集合來存儲點
ArrayList points;
public MyFrame(String title){
super(title);
setBounds(200,200,400,300);
//存鼠標電機的點
points= new ArrayList<>();
setVisible(true);
//鼠標監聽器
this.addMouseListener(new MyMouseListener());
}
窗口監聽
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Window011 {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.BLUE);
setBounds(100,100,800,400);
setVisible(true);
//addWindowListener(new MyWindowListener());
// 匿名內部類
this.addWindowListener(new WindowAdapter() {
鍵盤監聽
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Key01 {
public static void main(String[] args) {
new Key();
}
}
class Key extends Frame {
public Key() {
setBounds(100,100,300,300);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//獲得鍵盤按下的鍵是哪一個
int keyCode = e.getKeyCode();
if(keyCode==KeyEvent.VK_2){
System.out.println("2");
}
}
});
}
}
按2的時候輸出System.out.println("2");
2