JavaGUI笔记(狂神说Java)
作者:互联网
GUI
MySQL学习的视频在这里:点 !这!里!
狂神老师的学习平台:www.kuangStudy.com
这个笔记并不包括后面贪吃蛇项目
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听时间
- 鼠标
- 键盘事件
- 外挂
- 破解工具
1、简介
Gui的核心技术: Swing AWT
不流行原因:
- 因为界面不美观
- 需要jre环境
为什么我们要学习?
- 可以写出自己心中想要的小工具
- 工作时候,也可能需要维护到swing界面,概率极小!
- 了解MVC架构,了解监听!
2、 AWT介绍
- 包含了很多类和接口! GUI:图形用户界面编程
- 元素:窗口,按钮,文本框
- 包都在java.awt
组件和容器
1、Frame框架
//Gui 的第一个界面
public static void main(String[] args){
//Frame,JDK,看源码!(JDK帮助文档)
Frame frame = new Frame("我的第一个Java图像界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色,可以直接查源代码设置已有的颜色frame.setBackground(Color.black);
//也可以直接输入new color(r.g.b)三基色
frame.setBackground(new Color(37, 80, 167));
//弹出的初始位置
frame.setLocation(200,200);
//已经大概有雏形,但是没有设置关闭窗口功能,最小化、最大化、窗口尺寸已经默认存在。
//设置大小固定
frame.setResizable(false);
}
问题:发现窗口关闭不掉,只能停止java程序运行。
回顾封装:
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(150, 150, 200, 200, Color.yellow);
MyFrame myFrame3 = new MyFrame(200, 200, 200, 200, Color.red);
MyFrame myFrame4 = new MyFrame(250, 250, 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));//继承
setBounds(x, y, w, h);//坐标
setBackground(color);//颜色
setVisible(true);//可见性
}
}
2、面板Panel
import java.awt.*;
//panel 可以看成是一个空间,但是不能单独存在
public class TestPanel {
public static void main(String[] args){
Frame frame = new Frame(); //new 窗口
//布局的概念
Panel panel = new Panel(); //new 面板
Panel panel1 = new Panel();
//设置布局,不设置面板会置顶
frame.setLayout(null);
//窗口坐标和颜色
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(140, 208, 212));
//panel 设置坐标,相对于frame
panel.setBounds(50,50,400,100);
panel.setBackground(new Color(181, 186, 54));
panel1.setBounds(50,200,400,250);
panel1.setBackground(new Color(165, 34, 101));
//将panel添加进frame
frame.add(panel1);
frame.add(panel);
frame.setVisible(true);
}
}
// 解决窗口关闭的问题
//监听事件,监听窗口关闭事件 System.exit(0)强制结束
/*frame.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
}
});......
直接new WindowListener()子类太多,
可以使用适配器模式,new 其中一项需要的子类*/
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
布局管理器
- 流式布局
- 东西南北中
- 表格布局
frame.setLayout
流式布局
frame.setLayout(new FlowLayout(0));
import java.awt.*;
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(0)); 0为左,1为中...
frame.setLayout(new FlowLayout(FlowLayout.LEFT));//两种方式
frame.setSize(200,200);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
东西南北中
frame.add(按键名字,BorderLayout.EAST方位);
import java.awt.*;
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.setSize(400,400);
//不同布局的方位
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.setVisible(true);
}
}
表格布局
TestGridLayout
import java.awt.*;
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.setSize(400,400);
frame.setVisible(true);
}
}
总结:
- Frame是一个顶级窗口
- Panel无法单独显示,必须添加到某个容器中。
- 布局管理器
- 流式
- 东西南北中
- 表格
- 大小,定位,背景颜色,可见性,监听!
事件监听
事件监听:当某个事情发生的时候,干什么?
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);
}
// 关闭窗体的事件
public 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");
}
}
多个按钮,共享一个事件
public class testActionEvent02 {
public static void main(String[] args) {
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("stop");
// 可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值!
// 可以多个按钮只写一个监听类
button2.setActionCommand("button2-stop");
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// 获得按钮的信息
System.out.println("按钮被点击了:msg"+e.getActionCommand());
}
}
输入框 TextField 监听
public class testTextFieldEvent {
public static void main(String[] args) {
// 启动
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
// 监听这个文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
// 按下enter 就会触发这个输入框的事件
textField.addActionListener(myActionListener2);
// 设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource(); // 获得一些资源,返回一个对象
System.out.println(field.getText()); // 获得输入框的文本
field.setText("");
}
}
简易计算器,组合+内部类回顾复习!
oop原则:组合,大于继承!
class A extends B{
}
class A{
public
}
初始代码
public class testCalc {
public static void main(String[] args) {
new Calculator();
}
}
// 计算器类
class Calculator extends Frame{
public Calculator(){
// 三个文本框
TextField textField1 = new TextField(10);// 字符数
TextField textField2 = new TextField(10);// 字符数
TextField textField3 = new TextField(10);// 字符数
// 一个按钮
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());
int n3 = n1 + n2;
//将这个值加法运算后放到第三个框
textField3.setText(""+n3);
//清除前两个框
textField1.setText("");
textField2.setText("");
}
}
完全改造为面向对象写法
public class testCalc {
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());
int n3 = n1 + n2;
//将这个值加法运算后放到第三个框
calculator.textField3.setText(""+n3);
//清除前两个框
calculator.textField1.setText("");
calculator.textField2.setText("");
}
}
内部类
- 更好的包装
public class testCalc {
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);
}
// 监听器类
// 内部类最大的好处,就是可以畅通无阻的访问外部的属性和方法!
private class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
int n1 = Integer.parseInt(textField1.getText());
int n2 = Integer.parseInt(textField2.getText());
int n3 = n1 + n2;
//将这个值加法运算后放到第三个框
textField3.setText(""+n3);
//清除前两个框
textField1.setText("");
textField2.setText("");
}
}
}
画笔
public class testPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,1200,500);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
//画笔需要有颜色才能画画
g.setColor(Color.BLUE);
g.drawOval(100,100,100,100);//空心圆
g.fillOval(200,200,100,100);//实心圆
g.setColor(Color.green);
g.fillRect(300,300,100,100);
//养成习惯,画笔用完,将他还原到最初的颜色(黑色)
}
}
鼠标监听
目的:想要实现鼠标画画!
//鼠标监听事件
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<>();
//鼠标监听器,针对这个窗口
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
@Override
public void paint(Graphics g) {
Iterator iterator = points.iterator();
while(iterator.hasNext()){
Point points =(Point) iterator.next();
g.setColor(Color.blue);
g.fillOval(points.x, points.y,10,10);
}
}
//添加一个点到界面上
public void addPaint(Point point){
points.add(point);
}
private class MyMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame = (MyFrame) e.getSource();
//这个我们点击的时候,就会在界面上产生一个点!画
//这个点就是鼠标的点
addPaint(new Point(e.getX(),e.getY()));
// 每次点击鼠标都需要画一遍
repaint();
}
}
}
窗口监听
public class testWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame() {
setVisible(true);
setBackground(Color.BLUE);
setBounds(100,100,200,200);
addWindowListener(new MyWindowListener());
// this.addWindowListener(
// //匿名内部类
// new WindowAdapter() {
// @Override
// public void windowClosing(WindowEvent e) {
// System.exit(0);
// }
// }
// );
}
class MyWindowListener extends WindowAdapter{
@Override
// 关闭窗口
public void windowClosing(WindowEvent e) {
setVisible(false);// 隐藏窗口,并不是退出
System.exit(0);// 正常退出
}
@Override
//激活窗口
public void windowActivated(WindowEvent e) {
WindowFrame source =(WindowFrame) e.getSource();
source.setTitle("被激活了");
System.out.println("windowactivated");
}
}
}
键盘监听
public class testKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame {
public KeyFrame() {
setBounds(100,100,400,300);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//获得键盘下的键是哪一个,当前的码
int keyCode = e.getKeyCode();//不需要记住值,直接用动态属性 VK_XX
System.out.println(keyCode);
if(keyCode == KeyEvent.VK_UP){
System.out.println("你按了上键");
}
// 根据按下不同的操作,产生不同的结果;
}
});
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
3、Swing
窗口、面板
public class JFrameDemo {
// init();初始化
public void init(){
JFrame jframe = new JFrame("this is a JFrame window!");
jframe.setVisible(true);
jframe.setBounds(100,100,300,400);
//设置文字JLabel
JLabel jLabel = new JLabel("你打开了这个窗口哦!");
jframe.add(jLabel);
//关闭事件
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//新建一个窗口
new JFrameDemo().init();
}
}
标签居中
package GUI.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
// init();初始化
public void init(){
JFrame jframe = new JFrame("this is a JFrame window!");
jframe.setVisible(true);
jframe.setBounds(100,100,300,400);
//设置文字JLabel
JLabel jLabel = new JLabel("你打开了这个窗口哦!");
jframe.add(jLabel);
//关闭事件
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//容器实例化
Container container = jframe.getContentPane();
container.setBackground(Color.blue);
//设置label水平居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
public static void main(String[] args) {
//新建一个窗口
new JFrameDemo().init();
}
}
弹窗
JDialog,用来被弹出,默认就有关闭事件!
//主窗口
public class DiologDemo extends JFrame {
public DiologDemo(){
this.setVisible(true);
this.setBounds(300,300,700,500);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西,容器
Container container = this.getContentPane();
// 绝对定位
container.setLayout(null);
//按钮
JButton jbutton = new JButton("点我");
jbutton.setBounds(30,30,200,50);
//点击这个按钮的时候,弹出一个弹窗
jbutton.addActionListener(new ActionListener() { //监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialog();
}
});
container.add(jbutton);
}
public static void main(String[] args) {
new DiologDemo();
}
}
//弹窗的窗口
class MyDialog extends JDialog{
public MyDialog() {
this.setBounds(100,100,500,500);
this.setVisible(true);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.add(new Label("啦啦啦"));
container.setLayout(null);
}
}
标签
label
new JLabel("xxx");
图标Icon
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 jLabel = new JLabel("Icontest", iconDemo, SwingConstants.CENTER);
Container container = getContentPane();
container.add(jLabel);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@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;
}
public static void main(String[] args) {
new IconDemo().init();
}
}
图片Icon
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
JLabel jLabel = new JLabel("ImageIcon");
//获取图片的地址
URL url = ImageIconDemo.class.getResource("pic.jpg");
ImageIcon imageIcon = new ImageIcon(url);
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(jLabel);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
面板
JPanel
public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//后面的两个参数代表间距
JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
JPanel jPanel2 = new JPanel(new GridLayout(3, 1));
JPanel jPanel3 = new JPanel(new GridLayout(1, 2));
JPanel jPanel4 = new JPanel(new GridLayout(2, 1));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel2.add(new JButton("2"));
jPanel2.add(new JButton("2"));
jPanel2.add(new JButton("2"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("3"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
container.add(jPanel1);
container.add(jPanel2);
container.add(jPanel3);
container.add(jPanel4);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(200,200,500,500);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JSrocllPanel
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("啦啦啦啦啦啦啦啦啦");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(200,200,500,500);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
按钮
图片按钮
public class JButtonDemo01 extends JFrame {
public JButtonDemo01(){
//将一个图片变成一个图标
Container container = this.getContentPane();
URL resource = JButtonDemo01.class.getResource("pic.jpg");
Icon icon = new ImageIcon(resource);
//把这个图标放在按钮上
JButton jButton = new JButton();
jButton.setIcon(icon);
jButton.setToolTipText("图片按钮");
//add
container.add(jButton);
this.setVisible(true);
this.setBounds(100,100,100,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
单选按钮
public class JButtonDemo01 extends JFrame {
public JButtonDemo01(){
//将一个图片变成一个图标
Container container = this.getContentPane();
URL resource = JButtonDemo01.class.getResource("pic.jpg");
Icon icon = new ImageIcon(resource);
//单选框
JRadioButton radioButton01 = new JRadioButton("JRadioButton01");
JRadioButton radioButton02 = new JRadioButton("JRadioButton02");
JRadioButton radioButton03 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个,分组,一个组中只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(radioButton01);
group.add(radioButton02);
group.add(radioButton03);
container.add(radioButton01,BorderLayout.NORTH);
container.add(radioButton02,BorderLayout.CENTER);
container.add(radioButton03,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,100,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
多选按钮
public class JButtonDemo03 extends JFrame {
public JButtonDemo03(){
//将一个图片变成一个图标
Container container = this.getContentPane();
URL resource = JButtonDemo03.class.getResource("pic.jpg");
Icon icon = new ImageIcon(resource);
//多选框
JCheckBox checkBox01 = new JCheckBox("JCheckBox01");
JCheckBox checkBox02 = new JCheckBox("JCheckBox02");
JCheckBox checkBox03 = new JCheckBox("JCheckBox03");
container.add(checkBox01,BorderLayout.NORTH);
container.add(checkBox02,BorderLayout.CENTER);
container.add(checkBox03,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
列表
下拉框
public class testComboboxDemo01 extends JFrame {
public testComboboxDemo01() {
Container container = this.getContentPane();
JComboBox comboBox = new JComboBox();
comboBox.addItem("正在热映");
comboBox.addItem("已下架");
comboBox.addItem("即将上映");
container.add(comboBox);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(200,200,500,500);
}
public static void main(String[] args) {
new testComboboxDemo01();
}
}
列表框
public class testComboboxDemo02 extends JFrame {
public testComboboxDemo02() {
Container container = this.getContentPane();
//生成列表的内容
// String[] contents = {"1","2","3"};
Vector vector = new Vector();
//列表中需要放入的内容
JList jList = new JList(vector);
vector.add("zhangsan");
vector.add("lisi");
vector.add("wangwu");
container.add(jList);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(200,200,500,500);
}
public static void main(String[] args) {
new testComboboxDemo02();
}
}
应用场景
- 选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容!
文本框
文本框
public class testTextDemo01 extends JFrame {
public testTextDemo01() {
Container container = this.getContentPane();
JTextField textField = new JTextField("Hello");
JTextField textField2 = new JTextField("World",20);
container.add(textField,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new testTextDemo01();
}
}
密码框
public class testTextDemo02 extends JFrame {
public testTextDemo02() {
Container container = this.getContentPane();
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setEchoChar('*');
container.add(jPasswordField);
this.setVisible(true);
this.setBounds(100,100,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new testTextDemo02();
}
}
文本域
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("啦啦啦啦啦啦啦啦啦");
补充
定时器
Timer timer = new Timer(delay,listener);
//delay是多少时间执行一次,单位是毫秒,listener是监听的对象
标签:Java,frame,JavaGUI,class,add,void,new,狂神,public 来源: https://blog.csdn.net/m0_52403880/article/details/120700615