14.GUI 編程
1.GUI 編程簡介
GUI:圖形用戶界面
GUI 核心技術(shù):Swing、Awt 類
- 界面不美觀
- 需要 jre 環(huán)境
為什么要學(xué)習(xí):
- 可以寫出一些小工具
- 工作時候,也可能需要維護(hù)到 Swing 界面,概率績效
- 了解 MVC 架構(gòu),了解監(jiān)聽
2.AWT 類
2.1.Awt 介紹
包含了很多類和接口
元素:窗口、按鈕、文本框
java.awt 包

2.2.組件和容器
2.2.1.彈框
Frame:彈出一個框
import java.awt.*;
//GUI 的第一個界面
public class TestFrame {
public static void main(String[] args) {
//Frame
Frame frame = new Frame("我的第一個 Java 圖形界面窗口");
//需要設(shè)置可見性
frame.setVisible(true);
//設(shè)置窗口大小
frame.setSize(400, 400);
//設(shè)置背景顏色
frame.setBackground(new Color(126, 193, 129));
//彈出的初始位置
frame.setLocation(200, 200);
//設(shè)置大小固定
frame.setResizable(false);
}
}
彈出多個框:
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多個窗口
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.blue);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.yellow);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.red);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.green);
}
}
class MyFrame extends Frame{
static int id = 0;//可能存在多個窗口,我們需要一個計數(shù)器
public MyFrame(int x, int y, int w, int h,Color color){
super("MyFrame" + (++id));
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
}
2.2.2.面板-Panel
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Panel 面板,可以看成是一個空間,但是不能單獨存在
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//設(shè)置布局
frame.setLayout(null);
//坐標(biāo)
frame.setBounds(300, 300, 500, 500);
frame.setBackground(new Color(76, 185, 185));
//Panel 設(shè)置坐標(biāo),相對 frame
panel.setBounds(50, 50, 400, 400);
panel.setBackground(new Color(250, 151, 151));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//監(jiān)聽事件,監(jiān)聽窗口關(guān)閉事件 System.exit(0)
//適配器模式:
frame.addWindowListener(new WindowAdapter() {
//窗口點擊關(guān)閉的時候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//結(jié)束程序
System.exit(0);
}
});
}
}
2.2.3.布局管理器
流式布局-FlowLayout
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();
//組件-按鈕
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//設(shè)置為流式布局
//frame.setLayout(new FlowLayout());
//設(shè)置靠左
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(200, 200);
//把按鈕添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
}
}
東西南北中-BorderLayout
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
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.setSize(200, 200);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
}
}
表格布局-GridLayout
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
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));//3行2列
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.pack();//將布局自動選擇合適的布局
frame.setVisible(true);
}
}
練習(xí):

import java.awt.*;
public class ExDemo {
public static void main(String[] args) {
//總 frame
Frame frame = new Frame();
frame.setSize(400, 300);
frame.setLocation(300, 400);
// frame.setBackground(Color.blue);
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+1)));
}
p3.add(p4, BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
}
}
2.3.事件監(jiān)聽
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 TestActionEvent {
public static void main(String[] args) {
//按下按鈕,觸發(fā)一些事情
Frame frame = new Frame();
Button button = new Button();
//因為 addActionListener() 需要一個 ActionListener,所以需要一個 ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
windowClose(frame);
frame.setVisible(true);
}
//關(guān)閉窗體的事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件監(jiān)聽
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
多個按鈕共享一個事件:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionDemo2 {
public static void main(String[] args) {
//兩個按鈕,實現(xiàn)同一個監(jiān)聽
//開始 停止
Frame frame = new Frame("開始-停止");
Button start = new Button("start");
Button stop = new Button("stop");
//可以顯示地定義觸發(fā)會返回的命令,如果不顯示定義,則會走默認(rèn)的值
//可以多個按鈕只寫一個監(jiān)聽類
stop.setActionCommand("btn2-stop");
MyListener myListener = new MyListener();
start.addActionListener(myListener);
stop.addActionListener(myListener);
frame.add(start, BorderLayout.NORTH);
frame.add(stop,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
class MyListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand():獲得按鈕的信息
System.out.println("按鈕被點擊了:msg=>"+e.getActionCommand());
}
}
2.4.輸入框 TextField
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText01 {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//監(jiān)聽這個文本框輸入的文字
MyActionListener myActionListener = new MyActionListener();
//按下 Enter,就會觸發(fā)這個輸入框的事件
textField.addActionListener(myActionListener);
//設(shè)置替換編碼
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();//獲得一些資源,返回的一個對象
System.out.println(field.getText());//獲得輸入框的文本
field.setText("");//回車置空
}
}
2.5.簡易計算器,組合+內(nèi)部類
簡易計算器
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
//計算器類
class Calculator extends Frame{
public Calculator(){
//3個文本框
TextField num1 = new TextField(10);//字符數(shù)
TextField num2 = new TextField(10);//字符數(shù)
TextField num3 = new TextField(10);//字符數(shù)
//1個按鈕
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1, num2, num3));
//1個標(biāo)簽
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//監(jiān)聽器類
class MyCalculatorListener implements ActionListener{
//獲取三個變量
private TextField num1,num2,num3;
public MyCalculatorListener(TextField num1,TextField num2,TextField num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.獲取加數(shù)和被加數(shù)
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2.加法運算后放到第三個框
num3.setText(""+(n1+n2));
//3.清除前兩個框
num1.setText("");
num2.setText("");
}
}
完全面向?qū)ο蟾脑?/summary>
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//計算器類
class Calculator extends Frame{
//屬性
TextField num1, num2, num3;
//方法
public void loadFrame(){
//3個文本框
num1 = new TextField(10);//字符數(shù)
num2 = new TextField(10);//字符數(shù)
num3 = new TextField(10);//字符數(shù)
//1個按鈕
Button button = new Button("=");
//1個標(biāo)簽
Label label = new Label("+");
button.addActionListener(new MyCalculatorListener(this));
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//監(jiān)聽器類
class MyCalculatorListener implements ActionListener{
//獲取計算器這個對象,在一個類中組合另外一個類
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator){
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.獲取加數(shù)和被加數(shù)
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
//2.加法運算后放到第三個框
calculator.num3.setText(""+(n1+n2));
//3.清除前兩個框
calculator.num1.setText("");
calculator.num2.setText("");
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//計算器類
class Calculator extends Frame{
//屬性
TextField num1, num2, num3;
//方法
public void loadFrame(){
//3個文本框
num1 = new TextField(10);//字符數(shù)
num2 = new TextField(10);//字符數(shù)
num3 = new TextField(10);//字符數(shù)
//1個按鈕
Button button = new Button("=");
//1個標(biāo)簽
Label label = new Label("+");
button.addActionListener(new MyCalculatorListener(this));
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//監(jiān)聽器類
class MyCalculatorListener implements ActionListener{
//獲取計算器這個對象,在一個類中組合另外一個類
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator){
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.獲取加數(shù)和被加數(shù)
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
//2.加法運算后放到第三個框
calculator.num3.setText(""+(n1+n2));
//3.清除前兩個框
calculator.num1.setText("");
calculator.num2.setText("");
}
}
改造為內(nèi)部類
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//計算器類
class Calculator extends Frame{
//屬性
TextField num1, num2, num3;
//方法
public void loadFrame(){
//3個文本框
num1 = new TextField(10);//字符數(shù)
num2 = new TextField(10);//字符數(shù)
num3 = new TextField(10);//字符數(shù)
//1個按鈕
Button button = new Button("=");
//1個標(biāo)簽
Label label = new Label("+");
button.addActionListener(new MyCalculatorListener());
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//監(jiān)聽器類
//內(nèi)部類最大的好處,就是可以暢通地訪問外部的屬性和方法
private class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1.獲取加數(shù)和被加數(shù)
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2.加法運算后放到第三個框
num3.setText(""+(n1+n2));
//3.清除前兩個框
num1.setText("");
num2.setText("");
}
}
}
2.6.畫筆
畫圓
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200, 200, 600, 500);
setVisible(true);
}
//畫筆
@Override
public void paint(Graphics g) {
//畫筆,需要有顏色,可以畫畫
g.setColor(Color.red);
//g.drawOval(100,100,100,100);//畫空心圓
g.fillOval(100, 100, 100, 100);//實心圓
//畫筆用完,將其還原到最初的顏色
g.setColor(Color.black);//黑色
}
}
2.7.鼠標(biāo)監(jiān)聽
目的:實現(xiàn)鼠標(biāo)畫畫

鼠標(biāo)點擊一次畫一個點
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("畫圖");
}
}
//自己的類
class MyFrame extends Frame{
//畫畫需要畫筆,需要監(jiān)聽鼠標(biāo)當(dāng)前的偽造,需要集合來存儲這個點
ArrayList points;
public MyFrame(String title){
super(title);
setBounds(200, 200, 400, 300);
//存鼠標(biāo)點擊的點
points = new ArrayList<>();
setVisible(true);
//鼠標(biāo)監(jiān)聽器,針對該窗口的
this.addMouseListener(new MyMouseListener());
}
@Override
public void paint(Graphics g) {
//畫畫,監(jiān)聽鼠標(biāo)的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();//指針指向下一個,返回當(dāng)前的點
g.setColor(Color.blue);
g.fillOval(point.x, point.y, 10, 10);
}
}
//添加一個點到界面上
public void addPoint(Point point){
points.add(point);
}
//適配器模式
private class MyMouseListener extends MouseAdapter{
//鼠標(biāo) 按下、彈起、按住不妨
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame = (MyFrame) e.getSource();
//點擊鼠標(biāo)的時候,就會在界面上產(chǎn)生一個點
//這個點就是鼠標(biāo)的點
frame.addPoint(new Point(e.getX(), e.getY()));
//每次點擊鼠標(biāo)都需要重新畫一遍
frame.repaint();//刷新
}
}
}
2.8.窗口監(jiān)聽
常用兩種窗口監(jiān)聽
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.blue);
setBounds(100, 100, 200, 200);
setVisible(true);
//匿名內(nèi)部類
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("窗口關(guān)閉");
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
//窗口激活
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
System.out.println("windowActivated");
}
});
}
}
2.9.鍵盤監(jiān)聽
點擊查看代碼
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(1, 2, 300, 400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//鍵盤按下
@Override
public void keyPressed(KeyEvent e) {
//獲得鍵盤下的鍵是哪一個,當(dāng)前的碼
int keyCode = e.getKeyCode();//不需要記錄該數(shù)值,直接使用靜態(tài)屬性:VK_xxx
if (keyCode == KeyEvent.VK_UP){
System.out.println("你按下了上建");
}
//根據(jù)按下不同操作,產(chǎn)生不同的結(jié)果
}
});
}
}
3.Swing 類
3.1.窗口
窗口
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init(); 初始化
public void init(){
//頂級窗口
JFrame jf = new JFrame("這是一個JFrame窗口");
jf.setVisible(true);
jf.setBounds(100, 100, 200, 200);
//設(shè)置文字 JLabel
JLabel label = new JLabel("Hello world");
jf.add(label);
//文本居中
label.setHorizontalAlignment(SwingConstants.CENTER);
//獲得容器
Container contentPane = jf.getContentPane();
contentPane.setBackground(Color.BLUE);
//關(guān)閉事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一個窗口
new JFrameDemo().init();
}
}
3.2.彈窗
彈窗代碼
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogDemo extends JFrame {
public DialogDemo() {
this.setVisible(true);
this.setSize(700, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放東西,容器
Container container = this.getContentPane();
//絕對布局
container.setLayout(null);
//按鈕
JButton button = new JButton("點擊彈出一個對話框");//創(chuàng)建
button.setBounds(30, 30, 200, 50);
//點擊這個按鈕的時候,彈窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//彈窗
new MyDialogDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//彈窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo() {
this.setVisible(true);
this.setBounds(100, 100, 500, 500);
//默認(rèn)就有關(guān)閉事件
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("彈窗標(biāo)題"));
}
}
3.3.標(biāo)簽
label
new JLabel("xxx");
圖標(biāo)ICON
import javax.swing.*;
import java.awt.*;
//圖標(biāo),需要實現(xiàn)類,F(xiàn)rame繼承
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo() {}
public IconDemo(int width, int height) {
this.width = width;
this.height = height;
}
public void init(){
IconDemo iconDemo = new IconDemo(15, 15);
//圖標(biāo)放在標(biāo)簽,也可以放在按鈕上
JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x, y, width, height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
圖片ICON
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public static void main(String[] args) {
new ImageIconDemo();
}
public ImageIconDemo(){
//獲取圖片的地址
JLabel label = new JLabel("ImageIconDemo");
URL url = ImageIconDemo.class.getResource("text.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setBounds(100, 100, 200, 200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
3.4.面板
JPanel
面板操作
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container container = this.getContentPane();
container.setLayout(new GridLayout(2, 1, 10, 10));//各個面板之間的間距:10
JPanel panel1 = new JPanel(new GridLayout(1, 3));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
container.add(panel1);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JScrollPane
滾動條面板
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container container = this.getContentPane();
container.setLayout(new GridLayout(2, 1, 10, 10));//各個面板之間的間距:10
JPanel panel1 = new JPanel(new GridLayout(1, 3));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
container.add(panel1);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
3.5.按鈕
3.5.1.圖片按鈕
點擊查看代碼
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo extends JFrame {
public JButtonDemo(){
Container container = this.getContentPane();
//將一個圖片變?yōu)閳D標(biāo)
URL resource = JButtonDemo.class.getResource("tx.jpg");
ImageIcon icon = new ImageIcon(resource);
//把這個圖標(biāo)放在按鈕上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("圖標(biāo)按鈕");
//add
container.add(button);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
3.5.2.單選按鈕
點擊查看代碼
import javax.swing.*;
import java.awt.*;
public class JButtonDemo2 extends JFrame {
public JButtonDemo2(){
Container container = this.getContentPane();
//單選框
JRadioButton radioButton1 = new JRadioButton("JRadioButton1");
JRadioButton radioButton2 = new JRadioButton("JRadioButton2");
JRadioButton radioButton3 = new JRadioButton("JRadioButton3");
//設(shè)置只能選擇一個,分組
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
container.add(radioButton1,BorderLayout.CENTER);
container.add(radioButton2,BorderLayout.NORTH);
container.add(radioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo2();
}
}
3.5.3.復(fù)選按鈕
點擊查看代碼
import javax.swing.*;
import java.awt.*;
public class JButtonDemo3 extends JFrame {
public JButtonDemo3(){
Container container = this.getContentPane();
//多選框
JCheckBox checkBox1 = new JCheckBox("checkBox1");
JCheckBox checkBox2 = new JCheckBox("checkBox2");
container.add(checkBox1,BorderLayout.NORTH);
container.add(checkBox2,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo3();
}
}
3.6.列表
3.6.1.下拉框
點擊查看代碼
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo extends JFrame {
public TestComboboxDemo(){
Container container = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在上映");
status.addItem("已下架");
status.addItem("即將上映");
container.add(status);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo();
}
}
3.6.2.列表框
點擊查看代碼
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboboxDemo2 extends JFrame {
public TestComboboxDemo2(){
Container container = this.getContentPane();
//生成列表內(nèi)容
//String[] contents = {"1","2","3"};//靜態(tài)寫死
Vector contents = new Vector();
//列表中需要放入內(nèi)容
JList jList = new JList(contents);
//動態(tài)添加
contents.add("zhangsan");
contents.add("wangwu");
contents.add("lisi");
container.add(jList);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo2();
}
}
3.6.3.應(yīng)用場景
下拉框:選擇地區(qū),或一些單個選項
列表:展示信息,一般是動態(tài)擴(kuò)容
3.7.文本框
3.7.1.文本框
點擊查看代碼
import javax.swing.*;
import java.awt.*;
public class TestTextDemo1 extends JFrame {
public TestTextDemo1(){
Container container = this.getContentPane();
JTextField textField1 = new JTextField("hello");
JTextField textField2 = new JTextField("world", 20);
container.add(textField1,BorderLayout.SOUTH);
container.add(textField2,BorderLayout.NORTH);
this.setVisible(true);
this.setSize(500, 350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo1();
}
}
3.7.2.密碼框
點擊查看代碼
import javax.swing.*;
import java.awt.*;
public class TestTextDemo2 extends JFrame {
public TestTextDemo2(){
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setSize(500, 350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo2();
}
}
3.7.3.文本域
點擊查看代碼
import javax.swing.*;
import java.awt.*;
public class TestTextDemo3 extends JFrame {
public TestTextDemo3(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 30);
textArea.setText("這是默認(rèn)字符");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setSize(500, 350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo3();
}
}
4.貪吃蛇小游戲
幀:如果時間片足夠小,就是動畫,一秒30、60幀,拆開就是靜態(tài)的圖片
鍵盤監(jiān)聽
定時器:Timer
圖片資源及源代碼:貪吃蛇代碼

浙公網(wǎng)安備 33010602011771號