Java学习之路--GUI编程06

发布时间 2023-09-20 18:35:23作者: 寂灭无言
package com.gui.lesson06;

import javax.swing.*;
import java.awt.*;

//2023.3.25/3.26 GUI编程--下拉框学习(Combobox)
//这个程序最终运行结果不美观,正常情况下下拉框我们放在一个面板里面再添加到容器中。这里就只是演示下拉框是什么样子
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01() {

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);//JFrame默认的关闭窗口的事件方法
}

public static void main(String[] args) {
//启动程序
new TestComboboxDemo01();
}
}

//
package com.gui.lesson06;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

//2023.3.25/3.26 GUI编程-列表框(List)学习 JFrame 里列表为 JList
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02() {

Container container = this.getContentPane();//建立容器,并初始化容器

//生成列表的内容
// String[]contents = {"1","2","3"};//创建一个String(字符串)类型的数组,采用静态初始化数组,数组里包含的元素为 1,2,3
//1.注释掉这行代码是因为下面写了Vector 动态数组,将动态数组注释掉就可以取消这行代码的注释了

Vector contents = new Vector();
/*
Vector 类实现了一个动态数组。和 ArrayList 很相似,但是两者是不同的:
Vector 是同步访问的。
Vector 包含了许多传统的方法,这些方法不属于集合框架。
Vector 主要用在事先不知道数组的大小,或者只是需要一个可以改变大小的数组的情况。
Vector 类支持 4 种构造方法。
第一种构造方法创建一个默认的向量,默认大小为 10:
Vector()
第二种构造方法创建指定大小的向量。
Vector(int size)
第三种构造方法创建指定大小的向量,并且增量用 incr 指定。增量表示向量每次增加的元素数目。
Vector(int size,int incr)
第四种构造方法创建一个包含集合 c 元素的向量:
Vector(Collection c)
*/

//列表中需要输入内容
JList list = new JList(contents);//创建列表对象,将数组contents 添加到列表中

contents.add("zhangsan");//1 这3行代码是Vector的
contents.add("lisi");//2
contents.add("wangwu");//3

container.add(list);//将列表添加到容器中

this.setVisible(true);//设置可见性
this.setSize(300,200);//设置大小
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//JFrame 默认关闭窗口的事件方法
}

public static void main(String[] args) {
//启动程序
new TestComboboxDemo02();
}
}

//
package com.gui.lesson06;

import javax.swing.*;
import java.awt.*;

//2023.3.26 GUI编程--文本框学习
public class TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container container = this.getContentPane();//建立容器,初始化容器

JTextField textField1 = new JTextField("hello");//创建文本对象1 初始化内容为‘hello’
JTextField textField2 = new JTextField("world", 20);//创建文本对象2,并初始化内容为‘world’ 文本长度为20

container.add(textField1,BorderLayout.NORTH);//将文本添加到容器中,采用东西南北中布局放在北边
container.add(textField2,BorderLayout.SOUTH);//将文本添加到容器中,采用东西南北中布局放在南边

this.setVisible(true);//设置可见性
this.setSize(300,350);//设置大小
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//JFrame 默认关闭窗口的事件方法
}

public static void main(String[] args) {
//启动程序
new TestTextDemo01();
}
}

//
package com.gui.lesson06;

import javax.swing.*;
import java.awt.*;

//2023.3.27 GUI编程--密码框学习(passwordField)
public class TestTextDemo02 extends JFrame {
public TestTextDemo02() {

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);//JFrame 默认关闭窗口的事件方法
}

public static void main(String[] args) {
//启动程序
new TestTextDemo02();
}
}

//
package com.gui.lesson06;

import javax.swing.*;
import java.awt.*;

//2023.3.27 GUI编程--文本域学习(TextArea)
public class TestTextDemo03 extends JFrame {
public TestTextDemo03() {

Container container = this.getContentPane();//创建容器,初始化容器

JTextArea textArea = new JTextArea(10,20);//创建文本域对象,文本域大小为10行,每行的可输入字符最大数为20
// JTextArea textArea = new JTextArea("加油!打工人!");//创建文本域对象,文本域初始内容为()’加油!打工人!‘
textArea.setText("好好干,日子会越来越甜!");//设置文本域内内容为 ’好好干,日子会越来越甜!‘

container.add(textArea);//将文本域添加到容器中

this.setVisible(true);//设置可见性
this.setSize(500,400);//设置窗体的大小
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//JFrame 默认关闭窗口的事件方法
}

public static void main(String[] args) {
//启动程序
new TestTextDemo03();
}
}