GUI编程
GUI编程
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具
一、 简介
Gui的核心技术: Swing AWT
1.因为界面不美观。
2.需要jre环境!
为什么我们要学习?
- 可以写出自己心中想要的一些小工具
- 工作时候,也可能需要维护到swing界面,概率极小!
- 了解MVC架构,了解监听!
二、AWT
1、Awt介绍
- 包含了很多类和接口! Gui:图形用户界面
- 元素:窗口,按钮,文本框
- java.awt包
2、组件和容器
2.1Frame
package com.mike.lesson01;
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame,JDk,看源码
Frame frame = new Frame("我的第一个Java图形界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(85,150,68));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
发现一个问题:窗口无法关闭,只能停止java程序运行
尝试回顾封装
package com.mike.lesson01;
import java.awt.*;
public class TestFrame02 {
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.MAGENTA);
}
}
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面板Panel
同时学习并解决上面提到的不能关闭窗口的问题
package com.mike.lesson01;
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(40,161,35));
//panel坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(193,15,60));
//将面板panel放进窗口frame
//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.3布局管理器
- FlowLayout:流式布局管理器
- BorderLayout:边界布局管理器
- GridLayout:网格布局管理器
流式布局
package com.mike.lesson01;
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("TestFlowLayout");
//组件-按钮
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);
//监听事件,监听窗口关闭事件 System. exit(0)
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
边界布局
package com.mike.lesson01;
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.setBounds(666,333,500,500);
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);
//监听事件,监听窗口关闭事件 System. exit(0)
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
网格布局
package com.mike.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
frame.setBounds(666,333,500,500);
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
frame.setLayout(new GridLayout(3,2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack();//Java函数
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System. exit(0)
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
JavaGui总共有八大布局:
- FlowLayout:流式布局管理器
- BorderLayout:边界布局管理器
- GridLayout:网格布局管理器
- GridBagLayout:网格组布局管理器
- GardLayout:卡片布局管理器
- BoxLayout:箱式布局管理器
- SpringLayout:弹簧布局管理器
练习ExDemo
package com.mike.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ExDemo {
public static void main(String[] args) {
//总Frame
Frame frame = new Frame("ExDemo");
frame.setSize(400,400);
frame.setLocation(300,400);
frame.setVisible(true);
frame.setBackground(Color.PINK);
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("p1-West-1"),BorderLayout.WEST);
p1.add(new Button("p1-East-2"),BorderLayout.EAST);
p2.add(new Button("p1-p2-btn-1"));
p2.add(new Button("p1-p2-btn-2"));
p1.add(p2,BorderLayout.CENTER);
//下面
p3.add(new Button("p3-West-2"),BorderLayout.WEST);
p3.add(new Button("p3-East-2"),BorderLayout.EAST);
//下面中间四个
for (int i = 1; i <= 4; i++) {
p4.add(new Button("p3-p4-btn-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
//监听事件,监听窗口关闭事件 System. exit(0)
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
运行截图
总结:
-
Frame是一个顶级窗口
-
Panel无法单独显示,必须添加到某个容器中。
-
布局管理器
- 1.流式
2.东西南北中
3.表格
- 1.流式
-
大小,定位,背景颜色,可见性,监听!
2.4事件监听
事件监听:当某个事情发生的时候,应该干什么
按钮监听事件
package com.mike.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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) {
// e.getActionCommand() 获得按钮的信息
System.out.println("按钮被点击了:msg==>"+e.getActionCommand());
if (e.getActionCommand().equals("start")){
System.out.println(666666);
}
}
}
窗口关闭监听事件
package com.mike.lesson02;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindowClosing {
public static void main(String[] args) {
Frame frame = new Frame("TestWindowClosing");
frame.setBounds(600,600,400,400);
frame.setBackground(Color.BLUE);
frame.setVisible(true);
windowClose(frame);
//窗口关闭监听事件的写法
====================================================================
/*
//匿名内部的类写法
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
*/
===================================================================
//继承WindowAdapter重写里面的方法去关闭窗口
// MyWindowClosing myWindowClosing = new MyWindowClosing();
// frame.addWindowListener(myWindowClosing);
====================================================================
}
//自己写一个关闭窗口的方法,然后可以直接调用(可以把它放进工具类中,反复使用)
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//也可以继承WindowAdapter重写里面的方法去关闭窗口
class MyWindowClosing extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
输入框TextField监听事件
package com.mike.lesson02;
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 TestText01 {
public static void main(String[] args) {
//启动
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
super("TestText01");
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
//按Enter就会触发这个输入框的事件
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
setBounds(600,300,500,200);
setVisible(true);
// pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();//获得一些资源
System.out.println(field.getText());//获得输入框中的文本
field.setText("");
}
//关闭窗口事件的方法,然后可以直接调用
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
练习:制作一个简易计算器,组合+内部类回顾复习!
OOP原则: 组合,大于继承!
目前代码(未优化)
package com.mike.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() {
super("简易计算器");
//3个文本框
TextField num1 = new TextField(10);//字符数
TextField num2 = new TextField(10);//字符数
TextField num3 = new TextField(20);//字符数
//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("");
}
}
完全改造为面向对象的写法
package com.mike.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(){
//3个文本框
num1 = new TextField(10);//字符数
num2 = new TextField(10);//字符数
num3 = new TextField(20);//字符数
//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();
setTitle("简易计算器");
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.mike.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(){ //3个文本框 num1 = new TextField(10);//字符数 num2 = new TextField(10);//字符数 num3 = new TextField(20);//字符数 //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(); setTitle("简易计算器"); 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(""); } } //=========================================================================== }
画笔
package com.mike.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,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.GREEN);
g.fillRect(150,200,200,200);
//养成习惯,画笔用完,将他还原到最初的颜色
}
}
鼠标监听
目的:想要实现鼠标画画!
package com.mike.lesson03;
import java.awt.*;
import java.awt.event.*;
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());
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@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();
//这个我们点击的时候,就会在界面上产生一个点!画
//这个点就是鼠标的点;
frame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一遍
frame. repaint();//刷新
}
}
}
窗口监听
package com.mike.lesson03;
import jdk.nashorn.internal.runtime.Source;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame("TestWindow");
// new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame(String litter) {
super(litter);
setBackground(Color.BLUE);
setBounds(600, 300, 400, 400);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
//窗口被激活
@Override
public void windowActivated(WindowEvent e) {
// WindowFrame source = (WindowFrame) e.getSource();
// source.setTitle("windowActivated被激活了");
System.out.println("windowActivated");
}
});
}
}
键盘监听
package com.mike.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() {
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得键盘下的键是哪一个,获得当前的码
int keyCode = e.getKeyCode();//不需需要去记录这个数值,直接使用静态属性VK_ XXX
System.out.println(keyCode);
if (keyCode==KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
//根据按下不同操作,产生不同结果;
}
});
}
}
三、Swing
1、窗口、面板
初始化init()
package com.mike.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init();初始化
public void init(){
//顶级窗口
JFrame jFrame = new JFrame("这是一个JFrame窗口");
jFrame.setVisible(true);
jFrame.setBounds(100,100,200,200);
jFrame.setBackground(Color.PINK);
//设置文字
JLabel label = new JLabel("你好!世界!");
jFrame.add(label);
//关闭窗口事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JFrameDemo().init();
}
}
2、标签居中
package com.mike.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo02 {
public static void main(String[] args) {
new MyFrame2().init();
}
}
class MyFrame2 extends JFrame{
public void init(){
this.setBounds(100,100,200,200);
this.setVisible(true);
//设置文字
JLabel label = new JLabel("你好!世界!");
add(label);
//让文本标签居中 设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container container = this.getContentPane();
container.setBackground(Color.RED);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
3、设置弹窗JDialog
JDialog,用来被弹出,默认就有关闭事件!
package com.mike.lesson04;
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 jButton = new JButton("点击弹出一个对话框");//创建
jButton.setBounds(30,30,200,50);
//点击这个按钮的时候,弹出一个弹窗
jButton.addActionListener(new ActionListener() {//监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
container.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);
//弹窗默认就有关闭窗口,不需要再设置,这是多余的操作
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
JLabel jLabel = new JLabel("你好!地球!");
jLabel.setBounds(200,200,200,200);
container.add(jLabel);
// container.add(new JLabel("你好!地球!"));
}
}
4、标签
1、label
new JLabel("xxx");
2、图标ICON
package com.mike.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImagIconDemo extends JFrame {
public ImagIconDemo(){
//获取图片的地址
JLabel label = new JLabel("ImagIcon");
URL url = ImagIconDemo.class.getResource("01.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(label);
container.setBackground(Color.PINK);
this.setTitle("ImagIconDemo");
this.setBounds(500,500,500,500);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImagIconDemo();
}
}
5、面板
1.1、JPanel
package com.mike.lesson05;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo(String litter) {
super(litter);
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,2,10,10));//后面的参数,间距的意思
JPanel panel1 = new JPanel(new GridLayout(1,3));
JPanel panel2 = new JPanel(new GridLayout(1,2));
JPanel panel3 = new JPanel(new GridLayout(2,1));
JPanel panel4 = new JPanel(new GridLayout(3,2));
panel1.add(new JButton("1-1"));
panel1.add(new JButton("1-2"));
panel1.add(new JButton("1-3"));
panel2.add(new JButton("2-1"));
panel2.add(new JButton("2-2"));
panel3.add(new JButton("3-1"));
panel3.add(new JButton("3-2"));
panel4.add(new JButton("4-1"));
panel4.add(new JButton("4-2"));
panel4.add(new JButton("4-3"));
panel4.add(new JButton("4-4"));
panel4.add(new JButton("4-5"));
panel4.add(new JButton("4-6"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
container.setBackground(Color.PINK);
this.setVisible(true);
// this.pack();
this.setBounds(500,500,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo("JPanelDemo");
}
}
1.2、JScroll
package com.mike.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(String title) {
super(title);
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,50);
textArea.setText("你好!宇宙!");
//Scroll面板(如果超出窗口大小就会出现滚动条)
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo("JScrollDemo");
}
}
6、按钮
- 图片按钮
- 单选按钮
- 多选按钮
1、图片按钮
package com.mike.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01(String title) {
super(title);
Container container = this.getContentPane();
//将一个图片变为一个图标
URL url = JButtonDemo01.class.getResource("01.jpg");
Icon icon = new ImageIcon(url);
//把这个图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
//add
container.add(button);
this.setVisible(true);
this.setBounds(500,500,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01("JButtonDemo01");
}
}
2、单选按钮
package com.mike.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame {
public JButtonDemo02(String title) {
super(title);
Container container = this.getContentPane();
//将一个图片变为一个图标
URL url = JButtonDemo01.class.getResource("01.jpg");
Icon icon = new ImageIcon(url);
//单选框
JRadioButton radioButton1 = new JRadioButton("JRadioButton01");
JRadioButton radioButton2 = new JRadioButton("JRadioButton02");
JRadioButton radioButton3 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个, 分组,一个组只能选择一个
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.setBounds(500,500,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02("JButtonDemo02");
}
}
3、多选按钮
package com.mike.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo03 extends JFrame {
public JButtonDemo03(String title) {
super(title);
Container container = this.getContentPane();
//将一个图片变为一个图标
URL url = JButtonDemo01.class.getResource("01.jpg");
Icon icon = new ImageIcon(url);
//多选框
JCheckBox checkBox01 = new JCheckBox("checkBox01");
JCheckBox checkBox02 = new JCheckBox("checkBox02");
container.add(checkBox01,BorderLayout.NORTH);
container.add(checkBox02,BorderLayout.SOUTH);
this.setVisible(true);
// this.setSize(500,300);
this.setBounds(500,500,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03("JButtonDemo03");
}
}
7、列表
-
下拉框
-
列表框
1、下拉框
package com.mike.lesson06;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestComboBoxDemo01 extends JFrame {
public TestComboBoxDemo01(String title) {
super(title);
//先拿容器container
Container container = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("即将上映");
status.addItem("已下架");
//监听下拉框的内容
status.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JComboBox status = (JComboBox) e.getSource();//获得一些资源
System.out.println(status.getSelectedIndex());//返回项数
System.out.println(status.getSelectedItem());//获得输入框中的文本
}
});
container.add(status);
this.setVisible(true);
this.setBounds(200,200,500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBoxDemo01("TestComboBoxDemo01");
}
}
2、列表框
package com.mike.lesson06;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class TestComboBoxDemo02 extends JFrame {
public TestComboBoxDemo02(String title) {
super(title);
//先拿容器container
Container container = this.getContentPane();
//生成列表的内容
//这是静态的设置变量值
// String[] contents ={"1","2","3"};
//vector是一个集合,可以动态的设置变量值
Vector contents = new Vector<>();
//列表中需要放入内容
JList jList = new JList(contents);
contents.add("ZhangSan");
contents.add("LiSi");
contents.add("WangWu");
container.add(jList);
this.setVisible(true);
this.setBounds(200,200,500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBoxDemo02("TestComboBoxDemo02");
}
}
- 下拉框应用场景
- 级联选项
- 选择地区,或者一些单个选项
- 列表框应用场景
- 列表,展示信息,一般是动态扩容
8、文本框
- 文本框
- 密码框
- 文本域
1、文本框
package com.mike.lesson06;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01(String title) {
super(title);
//先拿容器container
Container container = this.getContentPane();
JTextField textField1 = new JTextField("Hello");
JTextField textField2 = new JTextField("Word",20);
container.add(textField1,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(200,200,500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01("文本框");
}
}
2、密码框
package com.mike.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo02 extends JFrame {
public TestTextDemo02(String title) {
super(title);
//先拿容器container
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();//默认为*
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setBounds(200,200,500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo02("密码框");
}
}
3、文本域
package com.mike.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(String title) {
super(title);
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,50);
textArea.setText("你好!宇宙!");
//Scroll面板(如果超出窗口大小就会出现滚动条)
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo("JScrollDemo");
}
}