GUI圖形界面編程(Java)


GUI編程

組件

  • 窗口

  • 彈窗

  • 面板

  • 文本框

  • 列表框

  • 按鈕

  • 圖片

  • 監聽事件

  • 鼠標

  • 鍵盤事件

  • 破解工具

1、簡介

Gui的核心技術:Swing、AWT

2、AWT

2.1、AWT介紹

1.包含了很多類和接口! GUI:圖形用戶界面編程

Eeclipse:Java

2.元素:窗口,按鈕,文本框

3.java.awt

4.image-20210625174228935

2.2、組件和容器

2.2.1 Frame

public class TextFrame {
   public static void main(String[] args) {
       //Frame對象
       Frame frame = new Frame("我的第一個Java圖形界面窗口");
       //設置可見性
       frame.setVisible(true);
       //設置窗口大小
       frame.setSize(400,400);
       //窗口背景顏色
       frame.setBackground(new Color(78, 61, 151));
       //彈出的初始位置
       frame.setLocation(200,200);
       //設置大小固定
       frame.setResizable(false);
  }
}

image-20210625181132142

問題:發現窗口關閉不掉,終止Java程序運行

嘗試回顧封裝:

package com.sgd.lesson01;

import java.awt.*;

/**
* @author 邵國棟
* @function:
* @date 2021年07月02日 9:11
*/
public class TestFrame_ {
   public static void main(String[] args) {
       //展示多個窗口

       MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.CYAN);
       MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.GREEN);
       MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.PINK);
  }
}

class MyFrame extends Frame {
   static int id = 0;//肯能存在多個窗口,需要一個計數器

   public MyFrame(int x, int y, int w, int h, Color color) {
       super("MyFrame+" + (id++));
       setBackground(color);
       setBounds(x, y, w, h);
       setVisible(true);
       setResizable(false);
  }
}

image-20210702094233218

2.2.2 面板Panel

解決了關閉事件

package com.sgd.lesson01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
* @author 邵國棟
* @function:面板Panel的練習
* @date 2021年07月02日 9:49
*/
//可看成空間,但不能單獨存在
public class TestPanel {
   public static void main(String[] args) {
       Frame frame = new Frame();
       //布局
       Panel panel = new Panel();
       //設置布局
       frame.setLayout(null);
       //坐標
       frame.setBounds(300, 300, 500, 500);
       frame.setBackground(new Color(65, 191, 18));
       //panel坐標,相對於frame
       panel.setBounds(50, 50, 400, 400);
       panel.setBackground(new Color(10, 101, 10));
       //frame.add(panel);
       frame.add(panel);
       frame.setResizable(false);
       frame.setVisible(true);
       //監聽事件,監聽窗口關閉事件 System.exit(0);
       //適配器模式:
       frame.addWindowListener(new WindowAdapter() {
           //窗口點擊關閉時要做的事情
           @Override
           public void windowClosing(WindowEvent e) {
               //結束程序
               System.exit(0);
          }
      });


  }
}

image-20210702105741678

2.2.3 布局管理器

  • 流式布局

package com.sgd.lesson01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* @author 邵國棟
* @function:流式布局的練習
* @date 2021年07月02日 15:48
*/
public class TestFlowLayout {
   public static void main(String[] args) {
       Frame frame = new Frame();
       frame.setLocation(500,500);
       //按鈕--組件
       Button button1 = new Button("Button1");
       Button button2 = new Button("Button2");
       Button button3 = new Button("Button3");
       //流式布局的定義
       //自定義為中 frame.setLayout(new FlowLayout());
       //自定義為中
       frame.setLayout(new FlowLayout(FlowLayout.LEFT));
       //彈窗尺寸的定義
       frame.setSize(200, 200);
       //彈窗可視性的定義
       frame.setVisible(true);
       //加入按鈕
       frame.add(button1);
       frame.add(button2);
       frame.add(button3);
       //窗口關閉操作
       frame.addWindowListener(new WindowAdapter() {
           @Override
           public void windowClosing(WindowEvent e) {
               System.exit(0);
          }
      });

  }
}

image-20210702164932065

  • 東西南北中

package com.sgd.lesson01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* @author 邵國棟
* @function:東西南北中式布局
* @date 2021年07月02日 16:55
*/
public class TestBorderLayout {
   public static void main(String[] args) {

       Frame frame = new Frame("TestBorderLayout");

       frame.setVisible(true);

       frame.setLocation(200, 200);

       frame.setSize(500, 500);

       frame.setBackground(new Color(104, 16, 16));

       Button east = new Button("East");
       Button west = new Button("West");
       Button south = new Button("South");
       Button north = new Button("North");
       Button center = new Button("Center");

       frame.add(east, BorderLayout.EAST);
       frame.add(west, BorderLayout.WEST);
       frame.add(south, BorderLayout.SOUTH);
       frame.add(north, BorderLayout.NORTH);
       frame.add(center, BorderLayout.CENTER);

       frame.addWindowListener(new WindowAdapter() {
           @Override
           public void windowClosing(WindowEvent e) {
               System.exit(0);
          }
      });

  }
}

image-20210702170857625

 

  • 表格布局 Grid

package com.sgd.lesson01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* @author 邵國棟
* @function:表格布局
* @date 2021年07月02日 17:10
*/
public class TestGridLayout {
   public static void main(String[] args) {
       Frame frame = new Frame("TestGridLayout");
       Button btn1 = new Button("btn1");
       Button btn2 = new Button("btn2");
       Button btn3 = new Button("btn3");
       Button btn4 = new Button("btn4");
       Button btn5 = new Button("btn5");
       Button btn6 = new Button("btn6");

       frame.setLayout(new GridLayout(3,2));

       frame.add(btn1);
       frame.add(btn2);
       frame.add(btn3);
       frame.add(btn4);
       frame.add(btn5);
       frame.add(btn6);

       frame.pack();//Java函數:布局自動優化;
       frame.setVisible(true);

       frame.addWindowListener(new WindowAdapter() {
           @Override
           public void windowClosing(WindowEvent e) {
               System.exit(0);
          }
      });


  }
}

image-20210702172341388

練習:

image-20210703091026560

 

package com.sgd.lesson01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* @author 邵國棟
* @function:
* @date 2021年07月02日 17:25
*/
public class Exercise1 {
   public static void main(String[] args) {
       Frame frame = new Frame("Exercise");
       frame.setVisible(true);
       frame.setLocation(100, 100);
       frame.setSize(300, 400);
       frame.setResizable(false);
       Panel p1 = new Panel();
       Panel p2 = new Panel();
       Panel p3 = new Panel();
       Panel p4 = new Panel();

       frame.setLayout(new GridLayout(2, 1));
       p1.setLayout(new BorderLayout());
       p2.setLayout(new GridLayout(2, 1));
       p3.setLayout(new BorderLayout());
       p4.setLayout(new GridLayout(2, 2));

       p1.add(new Button("but1"), BorderLayout.WEST);
       p1.add(new Button("but2"), BorderLayout.EAST);
       p2.add(new Button("p2-but.1"));
       p2.add(new Button("p2-but.2"));
       p1.add(p2, BorderLayout.CENTER);

       p3.add(new Button("but3"), BorderLayout.WEST);
       p3.add(new Button("but4"), BorderLayout.EAST);
       p4.add(new Button("p4-but.1"));
       p4.add(new Button("p4-but.2"));
       p4.add(new Button("p4-but.3"));
       p4.add(new Button("p4-but.4"));
       p3.add(p4, BorderLayout.CENTER);

       frame.add(p1);
       frame.add(p3);
       frame.addWindowListener(new WindowAdapter() {
           @Override
           public void windowClosing(WindowEvent e) {
               System.exit(0);
          }
      });
     

  }
}

image-20210703091115262

總結:

  1. Frame是頂級窗口

  2. Panel必須添加在某個容器中顯示

2.2.4 事件監聽

事件監聽:當某個事情發生的時候,干什么?

package com.sgd.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* @author 邵國棟
* @function:對監聽的練習
* @date 2021年07月03日 9:50
*/

public class TestActionEvent {
   public static void main(String[] args) {
       //按下按鈕,觸發一些事件
       Button button = new Button("but");
       Frame frame = new Frame("監聽事件");
       //流式分布
       frame.setLayout(new FlowLayout());
       //是否可改變窗口
       frame.setResizable(false);
       //可見性
       frame.setVisible(true);
       //初始位置
       frame.setLocation(100, 100);
       //窗口大小
       frame.setSize(300, 500);
       //添加按鈕去窗口
       frame.add(button);
       //自己調節美觀
       frame.pack();

       MyActionListener myActionListener = new MyActionListener();
       button.addActionListener(myActionListener);
       frameClose(frame);

  }
   //關閉窗口的事件
   private static void frameClose(Frame frame){
       frame.addWindowListener(new WindowAdapter() {
           @Override
           public void windowClosing(WindowEvent e) {
               System.exit(0);
          }
      });
  }
}
//事件監聽
class MyActionListener implements ActionListener {
   @Override
   public void actionPerformed(ActionEvent e) {
       System.out.println("OK!");
  }
}

兩個按鈕實現同一個監聽:

package com.sgd.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* @author 邵國棟
* @function:兩個按鈕實現同一個監聽: 開始、關閉
* @date 2021年07月19日 17:21
*/
public class TextActionTwo {
public static void main(String[] args) {
Frame frame = new Frame("開始 -- 關閉");
frame.setSize(400, 500);
frame.setLocation(200, 200);
frame.setResizable(false);
frame.setVisible(true);
frame.setBackground(new Color(20, 202, 20));
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
Button button1 = new Button("start");
Button button2 = new Button("stop");

frame.add(button1);
frame.add(button2);

MyMonitor myMonitor = new MyMonitor();
//可以顯示的定義觸發會返回的命令,如果不顯示定義,則會走默認的值 (stop) !
//可以多個按鈕只寫一個監聽類
button2.setActionCommand("button2-stop");

button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);

WindowClose(frame);
}

private static void WindowClose(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

class MyMonitor implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand() -> 獲取按鈕上的一些信息
System.out.println("按鈕被點擊了,msg:" + e.getActionCommand());
//可根據按鈕判斷不同信息的輸出
if (e.getActionCommand().equals("start")) {
System.out.println("start");
} else if (e.getActionCommand().equals("stop")) {
System.out.println("stop");
} else if(e.getActionCommand().equals("button2-stop")){
System.out.println("button2-stop");
}
}
}

2.2.5 輸入框 TextField 監聽

package com.sgd.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* @author 邵國棟
* @function:對GUI文本框的練習
* @date 2021年07月20日 15:35
*/
public class TextField01 {
public static void main(String[] args) {
//啟動!
MyFrame myFrame = new MyFrame();

}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);

//設置替換編碼
textField.setEchoChar('*');

setVisible(true);
pack();

//監聽這個文本框輸入的文字
MyActionListener02 myActionListener02 = new MyActionListener02();
//按下Enter就會觸發這個輸入框的事件
textField.addActionListener(myActionListener02);
}
}
class MyActionListener02 implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {

//獲得一些資源,返回的一個對象
TextField field=(TextField) e.getSource();
System.out.println(field.getText());//獲得輸入框中的文本
field.setText("");//null ""
}
}

2.2.6 簡單計算器,組合+內部類回顧復習!

oop原則:組合,大於繼承!


目前代碼:

package com.sgd.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* @author 邵國棟
* @function:簡易計算器的實現
* @date 2021年07月21日 10:03
*/
public class calExercise {
public static void main(String[] args) {
Calculator calculator = new Calculator();
WindowClose(calculator);
}

private static void WindowClose(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

//計算器類
class Calculator extends Frame {
public Calculator() {
//3個文本框
TextField num01 = new TextField(10);//字符數
TextField num02 = new TextField(10);
TextField num03 = new TextField(20);
// 1個按鈕
Button button = new Button("=");

button.addActionListener(new MyActionListener03(num01, num02, num03));

// 1個標簽
Label label = new Label("+");

//流式布局
setLayout(new FlowLayout());
add(num01);
add(label);
add(num02);
add(button);
add(num03);
pack();

setVisible(true);
}
}

//監聽器類
class MyActionListener03 implements ActionListener {
//獲取3個變量
private TextField num01, num02, num03;

public MyActionListener03(TextField num01, TextField num02, TextField num03) {
this.num01 = num01;
this.num02 = num02;
this.num03 = num03;
}

@Override
public void actionPerformed(ActionEvent e) {
//1.獲得兩個加數
int n1 = Integer.parseInt(num01.getText());
int n2 = Integer.parseInt(num02.getText());
//2.運算后填到第3個框
num03.setText("" + (n1 + n2));
//3.清除前兩個框
num01.setText("");
num02.setText("");
}
}

完全改造為面向對象:

package com.sgd.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* @author 邵國棟
* @function:簡易計算器的實現
* @date 2021年07月21日 10:03
*/
public class calExercise {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.loadFrame();
WindowClose(calculator);
}

//窗口監聽
private static void WindowClose(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

//計算器類
class Calculator extends Frame {

TextField num01, num02, num03;

public void loadFrame() {
//3個文本框
num01 = new TextField(10);//字符數
num02 = new TextField(10);
num03 = new TextField(20);
// 1個按鈕
Button button = new Button("=");

button.addActionListener(new MyActionListener03(this));

// 1個標簽
Label label = new Label("+");

//流式布局
setLayout(new FlowLayout());
add(num01);
add(label);
add(num02);
add(button);
add(num03);
pack();

setVisible(true);
}
}

//監聽器類
class MyActionListener03 implements ActionListener {
//獲取計算器這個對象
Calculator calculator = null;
//private TextField num01, num02, num03;

public MyActionListener03(Calculator calculator) {
this.calculator = calculator;
}

@Override
public void actionPerformed(ActionEvent e) {
//1.獲得兩個加數
int n1 = Integer.parseInt(calculator.num01.getText());
int n2 = Integer.parseInt(calculator.num02.getText());
//2.運算后填到第3個框
calculator.num03.setText("" + (n1 + n2));
//3.清除前兩個框
calculator.num01.setText("");
calculator.num02.setText("");
}
}

內部類:

  • 更好的包裝

package com.sgd.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* @author 邵國棟
* @function:簡易計算器的實現
* @date 2021年07月21日 10:03
*/
public class calExercise {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.loadFrame();
WindowClose(calculator);
}

//窗口監聽
private static void WindowClose(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

//計算器類
class Calculator extends Frame {

TextField num01, num02, num03;

public void loadFrame() {
//3個文本框
num01 = new TextField(10);//字符數
num02 = new TextField(10);
num03 = new TextField(20);
// 1個按鈕
Button button = new Button("=");

button.addActionListener(new MyActionListener03());

// 1個標簽
Label label = new Label("+");

//流式布局
setLayout(new FlowLayout());
add(num01);
add(label);
add(num02);
add(button);
add(num03);
pack();

setVisible(true);
}
private class MyActionListener03 implements ActionListener {
/*
//獲取計算器這個對象
Calculator calculator = null;
//private TextField num01, num02, num03;

public MyActionListener03(Calculator calculator) {
this.calculator = calculator;
}
*/

@Override
public void actionPerformed(ActionEvent e) {
//1.獲得兩個加數
int n1 = Integer.parseInt(num01.getText());
int n2 = Integer.parseInt(num02.getText());
//2.運算后填到第3個框
num03.setText("" + (n1 + n2));
//3.清除前兩個框
num01.setText("");
num02.setText("");
}
}
}

//監聽器類
class MyActionListener03 implements ActionListener {
/*
//獲取計算器這個對象
Calculator calculator = null;
//private TextField num01, num02, num03;

public MyActionListener03(Calculator calculator) {
this.calculator = calculator;
}
*/

@Override
public void actionPerformed(ActionEvent e) {
//1.獲得兩個加數
int n1 = Integer.parseInt(num01.getText());
int n2 = Integer.parseInt(num02.getText());
//2.運算后填到第3個框
num03.setText("" + (n1 + n2));
//3.清除前兩個框
num01.setText("");
num02.setText("");
}
}

2.2.7 畫筆

package com.sgd.lesson03;

import java.awt.*;

/**
* @author 邵國棟
* @function:畫筆練習
* @date 2021年07月26日 15:13
*/
public class TextPaint {
public static void main(String[] args) {
MyPaint myPaint = new MyPaint();
myPaint.loadFrame();
}
}

class MyPaint extends Frame {
public void loadFrame() {
setBounds(200,200,600,500);
setVisible(true);
}


@Override
public void paint(Graphics g) {

//super.paint(g);
//畫筆需要有顏色,可以畫畫
g.setColor(Color.red);
g.drawOval(100,100,100,100);
g.fillOval(200,200,100,100);//實心的圓
g.setColor(Color.green);
g.fillRect(150,200,200,200);
//養成習慣,將畫筆還原成最初的顏色

}
}

2.2.8 鼠標監聽

目的:想要實現鼠標畫畫

 

2.2.9 窗口監聽

2.2.10 鍵盤監聽

 

 

 

3、Swing

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM