Java GUI 学习
GUI编程
组件
窗口
弹窗
面板
文本框
列表框
按钮
图片
监听事件
鼠标
键盘事件
破解工具
1、 简介
Gui的核心技术: Swing AWT
不流行的原因:
- 界面不美观。
- 需要jre环境!
为什么我们要学习?
- 可以写出自己心中的一些小工具
- 工作时也可能需要维护Swing界面
- 了解MVC架构,了解监听!
2、AWT
2.1、 AWT介绍
- 包含了很多的类和接口! GUI!
- 元素:窗口,按钮,文本框
![]()
2.2、组件和容器
1、Frame
package com.yang.lesson01;
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(85,150,1));
//弹出的初始位置
frame.setLocation(300,300);
//设置大小固定
frame.setResizable(false);
}
}
问题:发现窗口关闭不掉,停止Java程序

尝试回顾封装:
package com.yang.lesson01;
import java.awt.*;
public class TestFrame2 extends TestFrame{
public static void main(String[] args) {
//展示多个窗口 new
MyFrame myFrame = new MyFrame(100, 100, 200, 200, Color.blue);
MyFrame myFrame1 = new MyFrame(300, 100, 200, 200, Color.yellow);
MyFrame myFrame2 = new MyFrame(100, 300, 200, 200, Color.gray);
MyFrame myFrame3 = 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、面板Panel
1、解决了关闭事件!
package com.yang.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
//Panel 是一个空间,但是不能单独存在(frame上)
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念Flowlayout
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(99, 154, 215));
//Panel 设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(105, 184, 27));
//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);
}
});
}
}

3、布局管理器
- 流式布局
package com.yang.lesson01;
import java.awt.*;
public class TesFlowLayout {
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.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setSize(200,200);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
- 东西南北中
package com.yang.lesson01;
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.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.setVisible(true);
}
}
- 表格布局 Grid
package com.yang.lesson01;
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.setVisible(true);
}
}
练习

代码实现:
package com.yang.lesson01;
import java.awt.*;
public class Quiz {
public static void main(String[] args) {
Frame frame = new Frame("Quiz");
Panel p1 = new Panel(new GridLayout(1,3));
Panel p2 = new Panel (new GridLayout(1,3));
frame.setLayout(new GridLayout(2,1));
Button btn1 = new Button("btn1");
p1.add(btn1);
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Panel p3 = new Panel(new GridLayout(2, 1));
p3.add(btn2);
p3.add(btn3);
p1.add(p3);
Button btn4 = new Button("btn4");
p1.add(btn4);
Button btn5 = new Button("btn5");
p2.add(btn5);
Button btn6 = new Button("btn6");
Button btn7 = new Button("btn7");
Button btn8 = new Button("btn8");
Button btn9 = new Button("btn9");
Panel p4 = new Panel(new GridLayout(2, 2));
p4.add(btn6);
p4.add(btn7);
p4.add(btn8);
p4.add(btn9);
p2.add(p4);
Button btn10 = new Button("btn10");
p2.add(btn10);
frame.add(p1);
frame.add(p2);
frame.pack();
frame.setSize(500,500);
frame.setVisible(true);
}
}
总结:
- Frame是一个顶级窗口
- Panel无法单独显示,必须添加到某个容器中。
- 布局管理器
- 流式
- 东西南北中
- biaoge
- 大小、定位、背景颜色、可见性、监听!
4、事件监听
事件监听:当某个事情发生的时候,干什么?
package com.yang.lesson02;
import javax.swing.*;
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) {
//按下按钮,触发一些事件
Button button = new Button("bbb");
Frame frame = new Frame();
//因为,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");
}
}
多个按钮,共享一个事件
package com.yang.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionTwo {
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) {
//e.getActionCommand() 获得按钮的信息
System.out.println("按钮被点击了:mes=>"+e.getActionCommand());
if(e.getActionCommand().equals("start"))
}
}
5、输入框TextField监听
2.6、简易计算器,组合+内部
类回顾复习
oop原则:组合,大于继承
class A extends B{
}
class A{
public B b;
}
目前代码
package com.yang.lesson02;
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() {
//三个文本框
TextField num1 = new TextField(10);//字符数
TextField num2 = new TextField(10);//字符数
TextField num3 = new TextField(10);//字符数
//一个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1,num2,num3));
//一个标签 +
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.valueOf(num1.getText());
int n2 = Integer.valueOf(num2.getText());
//2.将这个值加法运算后,放到第三个框
num3.setText(""+(n1+n2));
//3.清除前两个框
num1.setText("");
num2.setText("");
}
}
完全改造为面向对象
package com.yang.lesson02;
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(){
//三个文本框
num1 = new TextField(10);//字符数
num2 = new TextField(10);//字符数
num3 = new TextField(10);//字符数
//一个按钮
Button button = new Button("=");
//一个标签 +
Label label = new Label("+");
// 监听器
button.addActionListener(new MyCalculatorListener(this));
//布局
setLayout(new FlowLayout());
add(this.num1);
add(label);
add(this.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("");
}
}
内部类:
- 更好的包装
package com.yang.lesson02;
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(){
//三个文本框
num1 = new TextField(10);//字符数
num2 = new TextField(10);//字符数
num3 = new TextField(10);//字符数
//一个按钮
Button button = new Button("=");
//一个标签 +
Label label = new Label("+");
// 监听器
button.addActionListener(new MyCalculatorListener());
//布局
setLayout(new FlowLayout());
add(this.num1);
add(label);
add(this.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.7、画笔
package com.yang.Lesson03;
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,400);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
//super.paint(g);
//画笔,需要有颜色,画笔可以画画
g.setColor(Color.GREEN);
g.drawOval(100,100,100,100);
//实心的圆
g.fillOval(200,200,100,100);
g.setColor(Color.BLUE);
g.fillRect(300,300,100,100);
//养成习惯,画笔用完,将它还原到最初的颜色
}
}
2.8、鼠标监听
目标:实习鼠标画画!
package com.yang.Lesson03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
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 tile) {
super(tile);
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 point = (Point) iterator.next();
g.setColor(Color.blue);
g.fillOval(point.x,point.y,10,10);
}
}
//添加一个点到界面上
public void addPaint(Point point){
points.add(point);
}
//适配器模式
private class MyMouseListener extends MouseAdapter{
//鼠标 按下,弹起,按住不放
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame = (MyFrame) e.getSource();
//这里我们点击的时候就会在界面上产生一个点!画
//这个点就是鼠标的点
new Point(e.getX(),e.getY());
frame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一遍
frame.repaint();//刷新
}
}
}
2.9、窗口监听
package com.yang.GUI.Lesson03;
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.GREEN);
setBounds(200,100,200,100);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
// 激活窗口
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
setTitle("去哪了,快回来!");
}
// 关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(1);
}
});
}
}
package com.yang.GUI.Lesson03;
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.GREEN);
setBounds(200,100,200,100);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
// 激活窗口
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
setTitle("去哪了,快回来!");
}
// 关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(1);
}
});
}
}
2.10、键盘监听
package com.yang.GUI.Lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestkeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBackground(Color.WHITE);
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter(){
// 键盘按下
@Override
public void keyPressed(KeyEvent e) {
// 直接使用静态属性
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
// 根据不同操作,产生不同结果
}
});
}
}
package com.yang.GUI.Lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestkeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBackground(Color.WHITE);
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter(){
// 键盘按下
@Override
public void keyPressed(KeyEvent e) {
// 直接使用静态属性
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
// 根据不同操作,产生不同结果
}
});
}
}
3、Swing
3.1窗口和面板
package com.yang.GUI.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo{
// 初始化
public void init(){
JFrame jf = new JFrame("这是一个Jframe窗口");
jf.setVisible(true);
jf.setBounds(100,100,200,200);
//设置文字 JLabel
JLabel jLabel = new JLabel("lalalala!");
// 容器实例化
Container contentPane = jf.getContentPane();
contentPane.setBackground(Color.YELLOW);
jf.add(jLabel);
// 让文本标签居中 设置水平对齐
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
// 关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JFrameDemo().init();
}
}
3.2弹窗
JDialog,用来被弹窗,默认就有关闭事件
package com.yang.GUI.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
// 主窗口
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// JFrame 放东西,容器
Container contentPane = this.getContentPane();
// 绝对布局
contentPane.setLayout(null);
// 按钮
JButton jButton = new JButton("点击弹出对话框");
jButton.setBounds(30,30,200,50);
// 点击按钮时,弹出一个弹窗,需要监听事件
jButton.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// 弹窗
new MyDialogDemo();
}
});
contentPane.add(jButton);
}
public static void main(String[] args) {
new DialogDemo();
}
}
// 弹窗
class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(100,100,500,500);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
Label label = new Label("lalalalala!");
label.setVisible(true);
label.setBounds(20,20,100,20);
contentPane.add(label);
}
}
3.3、标签
label
new JLable("xxx");
图标 Icon
package com.yang.GUI.lesson04;
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 icontest = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(icontest);
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
package com.yang.GUI.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
//获取图片的地址
public ImageIconDemo(){
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("xgg.jpg");
ImageIcon imageIcon = new ImageIcon(url);//不要命名冲突
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
3.4、面板
JPanel
package com.yang.GUI.lesson05;
import com.yang.GUI.lesson04.JFrameDemo;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridLayout(2,1,10,10)); //后面参数的意思,间距!
JPanel jPanel = new JPanel(new GridLayout(1,3));
JPanel jPanel1 = new JPanel(new GridLayout(1, 2));
JPanel jPanel2 = new JPanel(new GridLayout(2, 2));
JPanel jPanel3 = new JPanel(new GridLayout(3, 2));
jPanel.add(new Button("1"));
jPanel.add(new Button("2"));
jPanel.add(new Button("3"));
jPanel1.add(new Button("3"));
jPanel1.add(new Button("3"));
jPanel2.add(new Button("3"));
jPanel2.add(new Button("3"));
jPanel2.add(new Button("3"));
jPanel2.add(new Button("3"));
jPanel3.add(new Button("3"));
jPanel3.add(new Button("3"));
jPanel3.add(new Button("3"));
jPanel3.add(new Button("3"));
jPanel3.add(new Button("3"));
jPanel3.add(new Button("3"));
contentPane.add(jPanel);
contentPane.add(jPanel1);
contentPane.add(jPanel2);
contentPane.add(jPanel3);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JScrollPanel
package com.yang.GUI.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container contentPane = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("啦啦啦啦!");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
this.setVisible(true);
this.setBounds(100,100,300,150);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
3.5、按钮
- 图片按钮
package com.yang.GUI.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01(){
Container contentPane = this.getContentPane();
//将一个图片变为图标
URL resource = JButtonDemo01.class.getResource("xgg.jpg");
Icon icon = new ImageIcon(resource);
//把图标放在按钮上
JButton jButton = new JButton();
jButton.setIcon(icon);
jButton.setToolTipText("图片按钮");
//将按钮添加到容器
contentPane.add(jButton);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
- 单选按钮
package com.yang.GUI.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame {
public JButtonDemo02(){
Container contentPane = this.getContentPane();
//将一个图片变为图标
URL resource = JButtonDemo01.class.getResource("xgg.jpg");
Icon icon = new ImageIcon(resource);
//单选框
JRadioButton jRadioButton01 = new JRadioButton("JRadioButton01");
JRadioButton jRadioButton02 = new JRadioButton("JRadioButton02");
JRadioButton jRadioButton03 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个,分组
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton01);
buttonGroup.add(jRadioButton02);
buttonGroup.add(jRadioButton03);
contentPane.add(jRadioButton01,BorderLayout.NORTH);
contentPane.add(jRadioButton02,BorderLayout.CENTER);
contentPane.add(jRadioButton03,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
- 复选按钮 JCheckBox
package com.yang.GUI.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo03 extends JFrame {
public JButtonDemo03(){
Container contentPane = this.getContentPane();
//将一个图片变为图标
URL resource = JButtonDemo01.class.getResource("xgg.jpg");
Icon icon = new ImageIcon(resource);
//多选框
JCheckBox checkBox01 = new JCheckBox("checkBox01");
JCheckBox checkBox02 = new JCheckBox("checkBox02");
contentPane.add(checkBox01,BorderLayout.NORTH);
contentPane.add(checkBox02,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
3.6、列表
- 下拉框
package com.yang.GUI.lesson06;
import com.yang.GUI.lesson05.JButtonDemo01;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01(){
Container contentPane = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在上映");
status.addItem("即将上映");
status.addItem("已下架");
contentPane.add(status);
this.setVisible(true);
this.setBounds(100,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
- 列表框
package com.yang.GUI.lesson06;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02(){
Container contentPane = this.getContentPane();
//生成列表内容
// String[] contents ={"1","2","3"};
Vector contents = new Vector();
//列表需要中放入内容
JList jList = new JList(contents);
contents.add("zhangsan");
contents.add("lisi");
contents.add("wangwu");
contentPane.add(jList);
this.setVisible(true);
this.setBounds(100,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
- 应用场景
-
- 选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容!
3.7、文本框
- 文本框
package com.yang.GUI.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01(){
Container contentPane = this.getContentPane();
JTextField textField = new JTextField("hello");
JTextField textField2 = new JTextField("world",20);
contentPane.add(textField,BorderLayout.NORTH);
contentPane.add(textField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
- 密码框
package com.yang.GUI.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo02 extends JFrame {
public TestTextDemo02(){
Container contentPane = this.getContentPane();
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setEchoChar('*');
contentPane.add(jPasswordField);
this.setVisible(true);
this.setBounds(100,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo02();
}
}
- 文本域
package com.yang.GUI.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container contentPane = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("啦啦啦啦!");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
this.setVisible(true);
this.setBounds(100,100,300,150);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
笔记记录于【狂神说Java】GUI编程入门到游戏实战
秦疆老师yyds
视频地址


浙公网安备 33010602011771号