1.GUI 程式設計簡介
GUI:圖形使用者介面
GUI 核心技術:Swing、Awt 類
- 介面不美觀
- 需要 jre 環境
為什麼要學習:
- 可以寫出一些小工具
- 工作時候,也可能需要維護到 Swing 介面,機率績效
- 瞭解 MVC 架構,瞭解監聽
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 圖形介面視窗");
//需要設定可見性
frame.setVisible(true);
//設定視窗大小
frame.setSize(400, 400);
//設定背景顏色
frame.setBackground(new Color(126, 193, 129));
//彈出的初始位置
frame.setLocation(200, 200);
//設定大小固定
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;//可能存在多個視窗,我們需要一個計數器
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();
//設定佈局
frame.setLayout(null);
//座標
frame.setBounds(300, 300, 500, 500);
frame.setBackground(new Color(76, 185, 185));
//Panel 設定座標,相對 frame
panel.setBounds(50, 50, 400, 400);
panel.setBackground(new Color(250, 151, 151));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//監聽事件,監聽視窗關閉事件 System.exit(0)
//介面卡模式:
frame.addWindowListener(new WindowAdapter() {
//視窗點選關閉的時候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//結束程式
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");
//設定為流式佈局
//frame.setLayout(new FlowLayout());
//設定靠左
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);
}
}
練習:
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.事件監聽
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) {
//按下按鈕,觸發一些事情
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);
}
//關閉窗體的事件
private static void windowClose(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("aaa");
}
}
多個按鈕共享一個事件:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionDemo2 {
public static void main(String[] args) {
//兩個按鈕,實現同一個監聽
//開始 停止
Frame frame = new Frame("開始-停止");
Button start = new Button("start");
Button stop = new Button("stop");
//可以顯示地定義觸發會返回的命令,如果不顯示定義,則會走預設的值
//可以多個按鈕只寫一個監聽類
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);
//監聽這個文字框輸入的文字
MyActionListener myActionListener = new MyActionListener();
//按下 Enter,就會觸發這個輸入框的事件
textField.addActionListener(myActionListener);
//設定替換編碼
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.簡易計算器,組合+內部類
簡易計算器
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);//字元數
TextField num2 = new TextField(10);//字元數
TextField num3 = new TextField(10);//字元數
//1個按鈕
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1, num2, num3));
//1個標籤
Label label = new Label("+");
//佈局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//監聽器類
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.獲取加數和被加數
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2.加法運算後放到第三個框
num3.setText(""+(n1+n2));
//3.清除前兩個框
num1.setText("");
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);//字元數
num2 = new TextField(10);//字元數
num3 = new TextField(10);//字元數
//1個按鈕
Button button = new Button("=");
//1個標籤
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);
}
}
//監聽器類
class MyCalculatorListener implements ActionListener{
//獲取計算器這個物件,在一個類中組合另外一個類
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator){
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.獲取加數和被加數
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);//字元數
num2 = new TextField(10);//字元數
num3 = new TextField(10);//字元數
//1個按鈕
Button button = new Button("=");
//1個標籤
Label label = new Label("+");
button.addActionListener(new MyCalculatorListener());
//佈局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//監聽器類
//內部類最大的好處,就是可以暢通地訪問外部的屬性和方法
private class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1.獲取加數和被加數
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.滑鼠監聽
目的:實現滑鼠畫畫
滑鼠點選一次畫一個點
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{
//畫畫需要畫筆,需要監聽滑鼠當前的偽造,需要集合來儲存這個點
ArrayList points;
public MyFrame(String title){
super(title);
setBounds(200, 200, 400, 300);
//存滑鼠點選的點
points = new ArrayList<>();
setVisible(true);
//滑鼠監聽器,針對該視窗的
this.addMouseListener(new MyMouseListener());
}
@Override
public void paint(Graphics g) {
//畫畫,監聽滑鼠的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();//指標指向下一個,返回當前的點
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{
//滑鼠 按下、彈起、按住不妨
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame = (MyFrame) e.getSource();
//點選滑鼠的時候,就會在介面上產生一個點
//這個點就是滑鼠的點
frame.addPoint(new Point(e.getX(), e.getY()));
//每次點選滑鼠都需要重新畫一遍
frame.repaint();//重新整理
}
}
}
2.8.視窗監聽
常用兩種視窗監聽
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);
//匿名內部類
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("視窗關閉");
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
//視窗啟用
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被啟用了");
System.out.println("windowActivated");
}
});
}
}
2.9.鍵盤監聽
點選檢視程式碼
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) {
//獲得鍵盤下的鍵是哪一個,當前的碼
int keyCode = e.getKeyCode();//不需要記錄該數值,直接使用靜態屬性:VK_xxx
if (keyCode == KeyEvent.VK_UP){
System.out.println("你按下了上建");
}
//根據按下不同操作,產生不同的結果
}
});
}
}
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);
//設定文字 JLabel
JLabel label = new JLabel("Hello world");
jf.add(label);
//文字居中
label.setHorizontalAlignment(SwingConstants.CENTER);
//獲得容器
Container contentPane = jf.getContentPane();
contentPane.setBackground(Color.BLUE);
//關閉事件
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("點選彈出一個對話方塊");//建立
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);
//預設就有關閉事件
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("彈窗標題"));
}
}
3.3.標籤
label
new JLabel("xxx");
圖示ICON
import javax.swing.*;
import java.awt.*;
//圖示,需要實現類,Frame繼承
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);
//圖示放在標籤,也可以放在按鈕上
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();
//將一個圖片變為圖示
URL resource = JButtonDemo.class.getResource("tx.jpg");
ImageIcon icon = new ImageIcon(resource);
//把這個圖示放在按鈕上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("圖示按鈕");
//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");
//設定只能選擇一個,分組
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.核取按鈕
點選檢視程式碼
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();
//生成列表內容
//String[] contents = {"1","2","3"};//靜態寫死
Vector contents = new Vector();
//列表中需要放入內容
JList jList = new JList(contents);
//動態新增
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.應用場景
下拉框:選擇地區,或一些單個選項
列表:展示資訊,一般是動態擴容
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("這是預設字元");
//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幀,拆開就是靜態的圖片
鍵盤監聽
定時器:Timer
圖片資源及原始碼:貪吃蛇程式碼