GUI编程
1、简介
Gui的核心技术: Swing AWT
-
因为界面不美观。
-
需要jre环境!
为什么我们要学习?
- 可以写出自己心中想要的一些小工具
- 工作时候,也可能需要维护到swing界面,概率极小!3.了解MVC架构,了解监听!
2、AWT
2.1、AWT介绍
- 包含了很多类和接口! GUI!
- 元素:狂口,按钮,文本框
- java.awt
2.2、组件和容器
1、Frame
package com.zjj.lesson01;
import java.awt.*;
/**
* @author zjj
* @create 2023-03-19 16:33
*/
public class TestFrame {
public static void main(String[] args) {
//Frame
Frame frame = new Frame("我的第一个Java图像界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色 Color
frame.setBackground(new Color(91, 91, 225));
//弹出的初始位置
frame.setLocation(200,500);
//设置大小
frame.setResizable(false);
}
}
尝试回顾封装
package com.zjj.lesson01;
import java.awt.*;
/**
* @author zjj
* @create 2023-03-19 16:43
*/
public class TestFrame02 {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame01 = new MyFrame(100,100,200,200,Color.blue);
MyFrame myFrame02 = new MyFrame(300,100,200,200,Color.blue);
MyFrame myFrame03 = new MyFrame(100,300,200,200,Color.blue);
MyFrame myFrame04 = new MyFrame(300,300,200,200,Color.blue);
}
}
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、面板
package com.zjj.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @author zjj
* @create 2023-03-19 17:01
*/
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(37, 118, 161));
//panel设置坐标,相对于frame
panel.setBounds(50,50,300,300);
panel.setBackground(new Color(193,15,60));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(e)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的是后需要做的事情
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
2.3、布局管理器
-
流式布局
package com.zjj.lesson01; import java.awt.*; /** * @author zjj * @create 2023-03-19 18:48 */ 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,220); //把按钮添加上去 frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); } }
-
东西南北中
package com.zjj.lesson01; import java.awt.*; /** * @author zjj * @create 2023-03-19 19:04 */ 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.setVisible(true); frame.setSize(200,200); } }
-
表格布局
package com.zjj.lesson01; import java.awt.*; /** * @author zjj * @create 2023-03-19 19:17 */ public class TestGridLayout { public static void main(String[] args) { Frame frame = new Frame("TestGridLayout"); 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(); frame.setVisible(true); } }
练习
package com.zjj.lesson01;
import java.awt.*;
/**
* @author zjj
* @create 2023-03-20 15:23
*/
public class ExDemo {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setLayout(new GridLayout(2,1));
frame.setVisible(true);
frame.setSize(400,400);
frame.setLayout(new GridLayout(2,1));
frame.setBackground(Color.red);
//4个面板
Panel panel1 = new Panel(new BorderLayout());
Panel panel2 = new Panel(new GridLayout(2,1));
Panel panel3 = new Panel(new BorderLayout());
Panel panel4 = new Panel(new GridLayout(2,2));
//上部分
panel1.add(new Button("East-1"),BorderLayout.EAST);
panel1.add(new Button("West-1"),BorderLayout.WEST);
panel2.add(new Button("p2-btn-1"));
panel2.add(new Button("p2-btn-2"));
panel1.add(panel2,BorderLayout.CENTER);
//下面
panel3.add(new Button("East-2"),BorderLayout.EAST);
panel3.add(new Button("West-2"),BorderLayout.WEST);
for (int i = 0; i < 4; i++) {
panel4.add(new Button("for-"+i));
}
panel3.add(panel4,BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gzldPfuZ-1679386163125)(E:/Typora/image-20230320153857377.png)]
总结:
- Frame是一个顶级窗口
- Panel无法单独显示,必须添加到某个容器中
- 布局管理器
- 流式布局
- 东西南北中
- 表格布局
- 大小、定位、背景颜色、可见性、监听
2.4、事件监听
事件监听:当某个事情发生的时候,该干什么?
package com.zjj.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @author zjj
* @create 2023-03-20 15:46
*/
public class TestActionEvent {
public static void main(String[] args) {
//按下按钮触发事件
Button button = new Button();
Frame frame = new Frame();
//因为addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.setVisible(true);
frame.pack();
frame.add(button,BorderLayout.CENTER);
windowClose(frame);//关闭窗口
}
//关闭窗体的事件
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("张");
}
}
2.5、输入框事件监听
package com.zjj.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author zjj
* @create 2023-03-20 16:58
*/
public class TestText01 {
public static void main(String[] args) {
//启动
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
textField.addActionListener(myActionListener2);//按下回车就会触发这个输入框的事件
//设置替换编码
textField.setEchoChar('*');
pack();
setVisible(true);
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField)e.getSource();//获得资源,返回一个对象
System.out.println(field.getText());//活的输入框中的文本
field.setText("");
}
}
2.6、简易计算机器,组合+内部类
oop原则:组合>继承!
-
目前代码(面向过程)
package com.zjj.lesson02; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * @author zjj * @create 2023-03-20 17:24 */ //简易计算器 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(30);//字符数 //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.zjj.lesson02; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * @author zjj * @create 2023-03-20 17:24 */ //简易计算器 public class TestCalc_1 { public static void main(String[] args) { new Calculator1(); } } //计算器类 class Calculator1 extends Frame { //属性 TextField num1, num2, num3; //方法 public void loadFrame() { num1 = new TextField(10);//字符数 num2 = new TextField(10);//字符数 num3 = new TextField(20);//字符数 //1个按钮 Button button = new Button("="); button.addActionListener(new MyCalculatorListener1(this)); //1个标签 Label label = new Label("+"); setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } } //监听器类 class MyCalculatorListener1 implements ActionListener{ // 获取三个变量 Calculator1 calculator1 = null; public MyCalculatorListener1(Calculator1 calculator1){ this.calculator1 = calculator1; } /*private TextField num1,num2,num3; public MyCalculatorListener1(TextField num1,TextField num2,TextField num3){ this.num1 = num1; this.num2 = num2; this.num3 = num3; }*/ @Override public void actionPerformed(ActionEvent e) { //1. 获得加数和被加数 //2.将这个值加法运算后,放到第三个框 //3.清除前两个框的值 int n1 = Integer.parseInt(calculator1.num1.getText()); int n2 = Integer.parseInt(calculator1.num2.getText()); calculator1.num3.setText(""+(n1+n2)); calculator1.num1.setText(""); calculator1.num2.setText(""); } }
-
内部类 ——(更好的包装)
package com.zjj.lesson02; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * @author zjj * @create 2023-03-20 17:24 */ //简易计算器 public class TestCalc_3 { public static void main(String[] args) { new Calculator2().loadFrame(); } } //计算器类 class Calculator2 extends Frame { //属性 TextField num1, num2, num3; //方法 public void loadFrame() { num1 = new TextField(10);//字符数 num2 = new TextField(10);//字符数 num3 = new TextField(20);//字符数 //1个按钮 Button button = new Button("="); button.addActionListener(new MyCalculatorListener2()); //1个标签 Label label = new Label("+"); setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } private class MyCalculatorListener2 implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { //1. 获得加数和被加数 //2.将这个值加法运算后,放到第三个框 //3.清除前两个框的值 int n1 = Integer.parseInt(num1.getText()); int n2 = Integer.parseInt(num2.getText()); num3.setText(""+(n1+n2)); num1.setText(""); num2.setText(""); } } }
2.7、画笔
package com.zjj.lesson03;
import java.awt.*;
/**
* @author zjj
* @create 2023-03-20 21:07
*/
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.fillOval(100,100,100,100);
g.setColor(Color.GREEN);
g.fillRect(150,200,200,50);
}
}
2.8、鼠标监听
目的:想要实现鼠标画画
package com.zjj.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;
import java.util.logging.FileHandler;
/**
* @author zjj
* @create 2023-03-20 21:21
*/
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画图");
}
}
//自己的类
class MyFrame extends Frame{
//画画需要画笔,需要监听鼠标当前的位置,需要=集合来存储这个点
ArrayList points;
public MyFrame(String title){
super(title);
setBounds(300,300,400,400);
//存放鼠标点击的点
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.MAGENTA);
g.fillOval(point.x,point.y,5,5);
}
}
//添加一个点到界面上
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();//刷新
}
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ruZA1IV4-1679386163125)(E:/Typora/image-20230321110601086.png)]
2.9、窗口监听
package com.zjj.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @author zjj
* @create 2023-03-21 11:17
*/
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setVisible(true);
setBounds(200,200,200,200);
//addWindowListener(new MyWindowListener());
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("你点击了关闭");
}
});
}
class MyWindowListener extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);//隐藏窗口,通过按钮隐藏窗口
System.exit(0);//正常退出
}
}
}
2.10、键盘监听
package com.hong.gUI.awt_.listener_;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
/**
* 键盘监听事件
*/
public class KeyListener_ {
public static void main(String[] args) {
new KeyFrame("KeyListener");
}
}
//键
class KeyFrame extends Frame{
public KeyFrame(String title) {
super(title);
setBounds(10,10,300,500);
setVisible(true);
//在这个窗口监听键盘事件
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {//键盘按下
int keyCode=e.getKeyCode();
//获得键盘下的键是哪一个,当前的码
System.out.println(keyCode+":"+(char)keyCode);
//不需要去记录这个数值,直接使用静态属性 KeyEvent.VK_XXX
if (keyCode==KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
}
}
3、Swing
3.1、JFrame:这是一个顶层容器(窗口)
Swing与AWT组件不同,Swing组件不能直接添加到顶层容器中,它必须添加到一个与Swing顶层容器相关联的内容面板(content pane)上,内容面板是一个顶层容器包含的一个普通容器,它是一个轻量级组件。基本规则:把Swing组件放入一个顶层Swing容器的内容面板上,避免使用非Swing的重量级组件
package com.hong.gUI.swing_;
import javax.swing.*;
import java.awt.*;
/**
* JFrame窗口
* 在顶层容器(窗口)下直接添加非Swing的重组件是没有用的,必须要放到一个容器内,才可添加
* 与AWT组件不同,Swing组件不能直接添加到顶层容器中,它必须添加到一个与Swing顶层容器相关联的内容面板(content pane)上,
* 内容面板是一个顶层容器包含的一个普通容器,它是一个轻量级组件。
* 基本规则:把Swing组件放入一个顶层Swing容器的内容面板上,避免使用非Swing的重量级组件
*/
public class JFrame_1 {
//init() 初始化方法 Applet与这个有关
public void init(){
//顶级窗口
JFrame jFrame = new JFrame("JFrame");
jFrame.setVisible(true);
jFrame.setBounds(100,400,300,300);
jFrame.setBackground(Color.BLUE);//在顶层容器下,需要一个与顶层容器相关的内容面板来设置面板颜色
//设置一个标签 设置文字
JLabel jLabel = new JLabel("欢迎来到java-JFrame窗口",SwingConstants.CENTER);//后面参数是设置为居中显示
jFrame.add(jLabel,BorderLayout.NORTH);
jLabel.setBackground(Color.BLUE);
//设置一个按钮
JButton button = new JButton("button");
button.setBackground(Color.cyan);
jFrame.add(button,BorderLayout.CENTER);
//关闭事件 不设置可以点击关闭按钮,但是程序不结束,窗口只是隐藏了
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//3==JFrame.EXIT_ON_CLOSE==WindowConstants.EXIT_ON_CLOSE
}
public static void main(String[] args) {
//建立一个窗口
new JFrame_1().init();
}
}
用一个内容面板:
package com.hong.gUI.swing_;
import javax.swing.*;
import java.awt.*;
/**
*
*/
public class JFrame_2 {
public static void main(String[] args) {
new MyJFrame1().init();
}
}
class MyJFrame1 extends JFrame{
public void init(){
setBounds(100,100,300,300);
setVisible(true);
//获得一个内容面板
Container container = getContentPane();
//设置面板颜色,也间接设置窗口颜色
container.setBackground(Color.PINK);
//设置一个标签 设置文字
JLabel jLabel = new JLabel("欢迎来到java-JFrame窗口",SwingConstants.CENTER);//后面参数是设置为居中显示
jLabel.setBackground(Color.cyan);
container.add(jLabel,BorderLayout.NORTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
标签居中:
JLabel jLabel = new JLabel("欢迎来到java-JFrame窗口",SwingConstants.CENTER);
//或者是jLabel.setHorizontalAlignment(SwingConstants.CENTER);//也是设置标签居中
3.2 弹窗
**JDialog:**用来被弹出,默认有关闭事件
package com.hong.gUI.swing_;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Swing弹窗 JDialog
*/
//主窗口
public class JDialogSwing_ extends JFrame {
public JDialogSwing_(){
setBounds(100,100,400,800);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JFrame 放东西 容器
Container container = getContentPane();
//设置为绝对布局:组件大小固定,不可改变,组件的大小必须设置
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");//创建
button.setBounds(30,30,100,50);
//点击这个按钮,弹出一个弹窗
button.addActionListener(new ActionListener() {//监听器
@Override
public void actionPerformed(ActionEvent e) {
//创建一个弹窗
new MyDialog();
}
});
container.add(button);
}
public static void main(String[] args) {
new JDialogSwing_();
}
}
//弹窗
class MyDialog extends JDialog{
public MyDialog() {
this.setVisible(true);
this.setBounds(200,500,300,300);
//对于弹窗来说不用再设置退出监听了,默认可以直接退出
//setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container =this.getContentPane();
container.setLayout(null);//设置了绝对布局,组件就必须设置大小
JLabel jLabel1=new JLabel("JDialog弹窗",SwingConstants.CENTER);
jLabel1.setBounds(0,0,400,400);
container.add(jLabel1);
}
}
3.3 标签
JLabel:
JLabel jLabel = new JLabel("欢迎来到java-JFrame窗口",SwingConstants.CENTER);
//或者是jLabel.setHorizontalAlignment(SwingConstants.CENTER);//也是设置标签居中
图标标签ICON:
package com.hong.gUI.swing_;
import javax.swing.*;
import java.awt.*;
/**
* Swing组件:标签(JLabel)设置图标标签
*/
public class JLabelIcon extends JFrame implements Icon{
private int width;
private int height;
//无参构造
public JLabelIcon(){
}
public JLabelIcon(int width, int height) throws HeadlessException {
this.width = width;
this.height = height;
}
public static void main(String[] args) {
new JLabelIcon(15,15).init();
}
public void init(){
JLabelIcon jLabelIcon = new JLabelIcon(10, 10);
JLabel label = new JLabel("标签图标", jLabelIcon, SwingConstants.CENTER);//第二个参数为Icon实现类
Container container = this.getContentPane();
container.add(label);
this.setBounds(10,10,400,800);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.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;
}
}
将图片添加到标签内ImageIcon:
package com.hong.gUI.swing_;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
* 将图片添加到标签内
* 添加一个图片到标签中,需要ImageIcon类
*/
public class ImageIcon_ extends JFrame {
public ImageIcon_(){
//获取图片的地址
JLabel label = new JLabel("ImageIcon",SwingConstants.CENTER);//设置标签的上下左右位置,所以是SwingConstants
URL url = ImageIcon_.class.getResource("Mid-AutumnFestival.jpg");//找到ImageIcon_类的同级目录下的1.jpg文件
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
setVisible(true);
setBounds(100,100,400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = getContentPane();
container.add(label);
}
public static void main(String[] args) {
new ImageIcon_();
}
}
3.4 面板
JPanel
package com.hong.gUI.swing_;
import javax.swing.*;
import java.awt.*;
/**
* JPanel面板
*/
public class JPanel_ extends JFrame {
public JPanel_() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,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"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
container.add(panel1);
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
container.add(panel2);
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
container.add(panel3);
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
container.add(panel4);
this.setVisible(true);
this.setSize(800,800);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanel_();
}
}
滑动面板JScrollPane
package com.hong.gUI.swing_;
import javax.swing.*;
import java.awt.*;
/**
* JScrollPane:滑动面板 有滚动条
*/
public class JScrollPane_ extends JFrame {
public JScrollPane_() throws HeadlessException {
Container container = this.getContentPane();
//设置一个文本域 文本域可以换行,而文本框不行
JTextArea textArea = new JTextArea(20,30);//设置起始行列数,行列可变化
textArea.setText("滑动面板的文本域,有换行功能");
//JScrollPane面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
setVisible(true);
setBounds(100,100,200,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollPane_();
}
}
3.5 按钮
图标按钮:把一个图标变为一个按钮图标
package com.hong.gUI.swing_;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
* 设置一个图标按钮 加了一个按钮提示方法
* 将一个图片变为图标
*/
public class JButtonIcon extends JFrame {
public JButtonIcon() throws HeadlessException {
Container container = getContentPane();
//将一个图片变成图标
URL url = JButtonIcon.class.getResource("Mid-AutumnFestival.jpg");
ImageIcon imageIcon = new ImageIcon(url);
//把这个图标放在按钮上
JButton button = new JButton();
button.setIcon(imageIcon);
button.setToolTipText("图片按钮");//设置提示按钮,鼠标放到按钮上面会弹出一些提示
container.add(button);
setSize(500,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonIcon();
}
}
-
单选按钮
将按钮添加进来,不是将组添加进来!! 要设置按钮的位置,避免出现覆盖现象,没有设置组的话,就是多选
package com.hong.gUI.swing_; import javax.swing.*; import java.awt.*; import java.net.URL; /** * 单选按钮 JRadioButton * 需要用到一个组,ButtonGroup */ public class JButton_1 extends JFrame { public void init(){ Container container = getContentPane(); //单选框 JRadioButton radioButton1 = new JRadioButton("JRdioButton1"); JRadioButton radioButton2 = new JRadioButton("JRdioButton2"); JRadioButton radioButton3 = new JRadioButton("JRdioButton3"); //由于是单选框,需要准备一个组,让三个按钮在这组内实现单选功能 ButtonGroup group = new ButtonGroup(); group.add(radioButton1); group.add(radioButton2); group.add(radioButton3); //将按钮添加进来,不是将组添加进来!! 要设置按钮的位置,避免出现覆盖现象 container.add(radioButton1,BorderLayout.NORTH); container.add(radioButton2,BorderLayout.CENTER); container.add(radioButton3,BorderLayout.SOUTH); setSize(500,300); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButton_1().init(); } }
-
复选按钮
package com.hong.gUI.swing_; import javax.swing.*; import java.awt.*; /** * 多选框 JCheckBox */ public class JButton_2 extends JFrame { public void init(){ Container container = getContentPane(); //多选框 JCheckBox checkBox1 = new JCheckBox("checkBox1"); JCheckBox checkBox2 = new JCheckBox("checkBox2"); JCheckBox checkBox3 = new JCheckBox("checkBox3"); container.add(checkBox1,BorderLayout.NORTH); container.add(checkBox2,BorderLayout.CENTER); container.add(checkBox3,BorderLayout.SOUTH); setSize(500,300); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButton_2().init(); } }
3.6 列表
-
下拉框
package com.hong.gUI.swing_; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * 下拉框 JComboBox类 * System.out.println(comboBox.getSelectedIndex());//返回当前项的位置 * System.out.println(comboBox.getSelectedItem());//返回当前项的内容 */ public class Combobox_1 extends JFrame { public Combobox_1() { Container container = this.getContentPane(); JComboBox comboBox = new JComboBox(); comboBox.addItem("正在热映"); comboBox.addItem(null); comboBox.addItem("即将上映"); comboBox.addItem("下架"); container.add(comboBox,BorderLayout.NORTH); comboBox.addActionListener(new MyActionListener()); this.setVisible(true); this.setBounds(100,100,400,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new Combobox_1(); } } class MyActionListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { JComboBox comboBox=(JComboBox)e.getSource(); System.out.println(comboBox.getSelectedIndex());//返回当前项的位置 System.out.println(comboBox.getSelectedItem());//返回当前项的内容 } }
-
列表框
package com.hong.gUI.swing_; import javax.swing.*; import java.awt.*; import java.util.Vector; /** * 列表框 JList * 每一项都是可点击的 */ public class Combobox_2 extends JFrame { public Combobox_2() { Container container = this.getContentPane(); //生成列表的内容 //String[] contents={"Item1","Item2","Item3"};//静态变量 Vector vector = new Vector();//动态变量 动态集合 JList list = new JList(vector);//列表中需要放入内容 container.add(list); vector.add("正在热映"); vector.add(null);//与下拉框不同,动态集合自动排除掉为空的内容,所以这里显示的就两个项 vector.add("已下架"); this.setVisible(true); this.setBounds(100,100,400,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new Combobox_2(); } }
-
应用场景
- 选择地区,或者一些单个选项 (选项只有两个的话建议使用单选框,三个以上建议使用下拉框)
- 列表框,展示项信息,一般都是动态扩容
3.7 文本框
-
文本框:
package com.hong.gUI.swing_; import javax.swing.*; import java.awt.*; /** * 文本框 JTextField * 不能换行 */ public class JTextField_ extends JFrame { public JTextField_() throws HeadlessException { Container container = this.getContentPane(); //设置文本框 不能换行 JTextField textField1 = new JTextField("hello");//设置初始内容 JTextField textField2 = new JTextField("world",20);//设置起始字符数,行列可变化 container.add(textField1,BorderLayout.NORTH); container.add(textField2,BorderLayout.SOUTH); setVisible(true); setBounds(100,100,200,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new JTextField_(); } }
-
密码框
package com.hong.gUI.swing_; import javax.swing.*; import java.awt.*; /** * 密码框 JPasswordField 在文本框中用一个字符隐藏内容 * 是JTextField的子类 默认是一个小黑点隐藏 */ public class JPasswordField_ extends JFrame { public JPasswordField_() throws HeadlessException { Container container = this.getContentPane(); //设置文本框 不能换行 JPasswordField passwordField = new JPasswordField("hello");//设置初始内容 passwordField.setEchoChar('*');//设置用一个字符代替输入字符,实现隐藏作用 System.out.println(passwordField.getPassword());//得到文本框的内容 container.add(passwordField,BorderLayout.NORTH); setVisible(true); setBounds(100,100,200,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new JPasswordField_(); } }
-
文本域
package com.hong.gUI.swing_; import javax.swing.*; import java.awt.*; /** * 文本域 */ public class JTextArea_ extends JFrame { public JTextArea_() throws HeadlessException { Container container = this.getContentPane(); //设置一个文本域 文本域可以换行,而文本框不行 JTextArea textArea = new JTextArea(20,30);//设置起始行列数,行列可变化 textArea.setText("滑动面板的文本域,有换行功能"); //JScrollPane面板 JScrollPane scrollPane = new JScrollPane(textArea); container.add(scrollPane); setVisible(true); setBounds(100,100,200,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new JTextArea_(); } }
4、贪吃蛇游戏
帧,如果时间片足够小就是动画,一秒30帧 60帧。连起来是动画,拆开就是静态的图片
定时器 Timer
四步:
- 定义数据
- 画
- 监听事件
- 键盘监听事件
- 事件监听