GUI编程


告诉大家该怎么学?

  • 这是什么?
  • 它怎么玩?
  • 该如何去我们平时运用?

    组件
  • 窗口
  • 弹窗
  • 面板
  • 文本框
  • 列表框
  • 按钮
  • 图片
  • 监听事件
  • 鼠标
  • 键盘事件
  • 破解工具

1、简介

GUi的核心技术:Swing AWT

  1. 因为界面不美观
  2. 需要jre环境

为什么我们要学习?

  1. 可以写出自己心中想要的一些小工具
  2. 工作时候,也可能需要维护到Swing界面,概率极小
  3. 了解MVC架构,了解监听!

2、AWT

2.1、Awt介绍

  1. 包含了很多类和接口
  2. 元素:窗口,按钮,文本框
  3. java.awt

2.2 组件和容器

1、Frame

package com.love.lesson01;

import java.awt.*;

public class TestFrame {
public static void main(String[] args) {
//Frame的意思是框架
Frame frame = new Frame("11111"); //设置可见性
frame.setVisible(true); //设置窗口大小
frame.setSize(400,400); //设置背景颜色
frame.setBackground(new Color(203, 10, 10, 255)); //弹出的初始位置
frame.setLocation(200,200); //设置大小固定
frame.setResizable(false);
}
}



问题:发现窗口关闭不掉,停止java程序!

尝试回顾封装

package com.love.lesson01;

import java.awt.*;

public class TestFrame02 {
public static void main(String[] args) {
new MyFrame(100,100,200,200,Color.yellow);
new MyFrame(300,100,200,200,Color.blue);
new MyFrame(100,300,200,200,Color.green);
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);
setResizable(false);
}
}

2、面板Panel

解决了关闭事件!

package com.love.lesson01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; //可以看成一个空间,但是不能单独存在
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(106, 232, 30)); //panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(233, 10, 10)); frame.add(panel); frame.setVisible(true); //frame.setResizable(false); //默认是true,可变大小的 //监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

2.3、布局管理器

流式布局

package com.love.lesson01;

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());
//frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); frame.setSize(400,400); //把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3); frame.setVisible(true); }
}

东西南北中

package com.love.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(400,400);
frame.setVisible(true);
}
}

表格布局

package com.love.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.love.homework;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; public class Test01 {
public static void main(String[] args) {
//框架设置
Frame frame = new Frame("Test01");
frame.setSize(400,300);
frame.setLocation(300,400);
frame.setBackground(Color.BLACK);
frame.setVisible(true);
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));
//Button button = new Button("00");
//button.setBackground(Color.BLUE); p1.add(new Button("East-1"),BorderLayout.EAST);
p1.add(new Button("West-1"),BorderLayout.WEST);
//p2放两个按钮
p2.add(new Button("p2-btn-1"));
//p2.add(button);
p2.add(new Button("p2-btn -2"));
//再把p2面板放p1面板中间
p1.add(p2,BorderLayout.CENTER); //下面
p3.add(new Button("East-2"),BorderLayout.EAST);
p3.add(new Button("West-2"),BorderLayout.WEST);
//p4放4个按钮
for (int i = 0; i < 4; i++) {
p4.add(new Button("for-"+i));
}
//再把p4面板放p3面板中间
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);
}
});
}
}

总结:

  1. Frame是一个顶级窗口
  2. Panel无法单独显示,必须添加到某个容器中。
  3. 布局管理器
    1. 流式
    2. 东西南北中
    3. 表格

4.大小,定位,背景颜色,可见性,监听!

2.4、事件监听

事件监听:当某个事情发生的时候,干什么?

package com.love.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 TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button("button");
//因为,addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
};
button.addActionListener(actionListener);
frame.add(button,BorderLayout.CENTER);
frame.setBounds(400,400,300,300);
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);
}
});
}
}

多个按钮共享一个事件

package com.love.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 TestActionTwo {
public static void main(String[] args) {
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("stop"); button1.setActionCommand("button-1-");
MyMonitor myMonitor = new MyMonitor(); button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor); frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();//自适应大小按钮
windowClose(frame); //关闭窗口
frame.setVisible(true);//可见性
}
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式:
public static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyMonitor implements ActionListener{ @Override
public void actionPerformed(ActionEvent e) {
//获得按钮点击信息
String message = e.getActionCommand();
System.out.println("按钮被点击了:"+e.getActionCommand());
}
}

2.5、输入框TextField监听

package com.love.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 TextText01 {
public static void main(String[] args) {
//启动
new MyFrame();
}
} //我的框架对象
class MyFrame extends Frame{
public MyFrame(){ TextField textField = new TextField();//一行文本框
//因为是继承frame,所以不用加Frame. 直接用Frame里面的方法
add(textField); MyActionListener2 myActionListener2 = new MyActionListener2();
textField.addActionListener(myActionListener2); //设置替换编码
textField.setEchoChar('*'); setLocation(500,500);
windowClose();
pack();
setVisible(true);
} //关闭窗口
public void windowClose(){
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("");//回车一次清空一次文本框
}
}

2.6、简易计算器,组合+内部类回顾复习

oop原则:组合,大于继承!

package com.love.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(20);
//一个按钮
Button button = new Button("="); //增加按钮监听事件
button.addActionListener(new MyCalculatorListener(num1,num2,num3)); //一个标签
Label label = new Label("+"); add(num1);
add(label);
add(num2);
add(button);
add(num3); //布局
setLayout(new FlowLayout());
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("");
}
}*/ //-------------------------------------------------------------------- /*
//简易计算器_完全改造为面向对象写法
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
} //计算器类
class Calculator extends Frame{ //属性
TextField num1,num2,num3; public Calculator(){
//三个文本框
num1 = new TextField(10);//最多字符数
num2 = new TextField(10);
num3 = new TextField(20);
//一个按钮
Button button = new Button("="); //增加按钮监听事件
button.addActionListener(new MyCalculatorListener(this)); //一个标签
Label label = new Label("+"); add(num1);
add(label);
add(num2);
add(button);
add(num3); //布局
setLayout(new FlowLayout());
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("");
}
}
*/ //--------------------------------------------------------------------
//简易计算器_内部类
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(20);
//一个按钮
Button button = new Button("="); //增加按钮监听事件
button.addActionListener(new MyCalculatorListener()); //一个标签
Label label = new Label("+"); add(num1);
add(label);
add(num2);
add(button);
add(num3); //布局
setLayout(new FlowLayout());
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.love.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.fillOval(100,100,100,100); g.setColor(Color.GREEN);
g.fillRect(150,200,200,200); //养成习惯,画笔用完将他还原到最初的颜色
g.setColor(Color.BLACK);
}
}

2.8、鼠标监听

目的:想要实现鼠标画画!

package com.love.lesson03;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
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,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.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();//刷新
}
}
}

2.9、窗口监听

package com.love.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.BLUE);
setBounds(100,100,200,200);
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("被激活了");
System.out.println("windowActivated"); }
});
}
}

2.10、键盘监听

package com.love.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();
System.out.println(keyCode);
if(keyCode == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
}
}

3、Swing

3.1、窗口、面板

package com.love.lesson04;

import javax.swing.*;
import java.awt.*; public class JframeDemo02 {
public static void main(String[] args) {
new MyJframe2().init();
}
}
class MyJframe2 extends JFrame {
public void init(){
this.setBounds(10,10,400,600);
this.setVisible(true); JLabel label = new JLabel("欢迎");
this.add(label); //让文本标签居中,设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER); //获得一个容器
Container container = this.getContentPane();
container.setBackground(Color.YELLOW);
}
}

标签居中

package com.love.lesson04;

import javax.swing.*;
import java.awt.*; public class JFrameDemo{
public static void main(String[] args) {
new JFrameDemo().init();
}
//init(); 初始化
public void init(){
JFrame jf = new JFrame("这是一个JFrame窗口");
jf.setVisible(true);
jf.setBounds(100,100,200,200);
//jf.setBackground(Color.CYAN); JLabel label = new JLabel("这是一个JLabel");
jf.add(label); label.setHorizontalAlignment(SwingConstants.CENTER);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container container = jf.getContentPane();
container.setBackground(Color.YELLOW);
} }

3.2、弹窗

package com.love.lesson04;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; //主窗口
public class DialogDemo extends JFrame {
public static void main(String[] args) {
new DialogDemo();
} public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setLocation(100,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //JFrame 放东西 容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null); //按钮
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50); //点击按钮时弹出一个对话框
button.addActionListener(new ActionListener() {
//监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
}); container.add(button);
}
}
//弹窗
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); container.add(new Label("学Java"));
} }

3.3、标签

label

new JLabel("xxx");

图标ICON

package com.love.lesson04;

import javax.swing.*;
import java.awt.*; public class IconDemo extends JFrame implements Icon { public static void main(String[] args) {
new IconDemo().init();
} 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(30, 30);
//图标放在标签上
JLabel label = new JLabel("iconTest", iconDemo, SwingConstants.CENTER); Container container = getContentPane();
container.add(label); 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 width;
} @Override
public int getIconHeight() {
return height;
}
}

图片Icon

package com.love.lesson04;

import javax.swing.*;
import java.awt.*;
import java.net.URL; public class ImageIconDemo extends JFrame {
public static void main(String[] args) {
new ImageIconDemo();
}
public ImageIconDemo(){
JLabel label = new JLabel("ImageIcon");
//获取图片地址 https://i.loli.net/2020/10/23/zcRDbvyjxgYa7km.png
URL url = ImageIconDemo.class.getResource("123tp.jpg");
//为啥不显示图片,我不清楚,应该是在Mac电脑的原因,下次回Windows系统试一试
//ok了,我换成jpg格式的行了
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER); Container container = getContentPane();
container.add(label); setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
}
}

3.4、面板

JPanel

package com.love.lesson05;

import javax.swing.*;
import java.awt.*; public class JPanelDemo extends JFrame {
public static void main(String[] args) {
new JPanelDemo();
}
public JPanelDemo(){
//Container:容器
Container container = this.getContentPane(); //container.setLayout(new GridLayout(2,1));
//后面参数意思是间距
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 Button("1"));
panel1.add(new Button("1"));
panel1.add(new Button("1"));
panel2.add(new Button("2"));
panel2.add(new Button("2"));
panel3.add(new Button("3"));
panel3.add(new Button("3"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
panel4.add(new Button("4")); container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4); this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} }

JScrollPanel

package com.love.lesson05;

import javax.swing.*;
import java.awt.*; public class JScrollDemo extends JFrame {
public static void main(String[] args) {
new JScrollDemo();
}
public JScrollDemo(){
//获取内容面板
Container container = this.getContentPane(); //文本域
JTextArea textArea = new JTextArea(20,50);
textArea.setText(" Who am i"); //Scroll(滚动)面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane); this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

3.5、按钮

图片按钮

package com.love.lesson05;

import com.love.lesson04.ImageIconDemo;

import javax.swing.*;
import java.awt.*;
import java.net.URL; public class JButtonDemo01 extends JFrame {
public static void main(String[] args) {
new JButtonDemo01();
}
public JButtonDemo01(){
Container container = this.getContentPane();
//将一个图片变为图标
URL url = JButtonDemo01.class.getResource("123tp.jpg");
//icon:图标
ImageIcon icon = new ImageIcon(url); //这个图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
//Tool Tip Text 工具提示文本
button.setToolTipText("图片按钮"); container.add(button);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); }
}

单选按钮

package com.love.lesson05;

import com.love.lesson04.ImageIconDemo;

import javax.swing.*;
import java.awt.*;
import java.net.URL; public class JButtonDemo02 extends JFrame {
public static void main(String[] args) {
new JButtonDemo02();
}
public JButtonDemo02(){
Container container = this.getContentPane();
//将一个图片变为图标
URL url = JButtonDemo01.class.getResource("123tp.jpg");
//icon:图标
ImageIcon icon = new ImageIcon(url); //单选框
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.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); }
}

复选按钮

package com.love.lesson05;

import com.love.lesson04.ImageIconDemo;

import javax.swing.*;
import java.awt.*;
import java.net.URL; public class JButtonDemo03 extends JFrame {
public static void main(String[] args) {
new JButtonDemo03();
}
public JButtonDemo03(){
Container container = this.getContentPane();
//将一个图片变为图标
URL url = JButtonDemo01.class.getResource("123tp.jpg");
//icon:图标
ImageIcon icon = new ImageIcon(url); //复选框
Checkbox checkbox01 = new Checkbox("checkbox01");
Checkbox checkbox02 = new Checkbox("checkbox02");
//复选框可以选多个,不用进行分组, //加到容器中
container.add(checkbox01,BorderLayout.NORTH);
container.add(checkbox02,BorderLayout.SOUTH); this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); }
}

3.6、列表

下拉框

package com.love.lesson06;

import javax.swing.*;
import java.awt.*; /**
* 下拉框
*/
public class TestComboboxDemo01 extends JFrame {
public static void main(String[] args) {
new TestComboboxDemo01();
}
public TestComboboxDemo01(){ Container container = this.getContentPane(); JComboBox status = new JComboBox(); status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映"); container.add(status); this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

列表框

package com.love.lesson06;

import javax.swing.*;
import java.awt.*;
import java.util.Vector; /**
* 列表框
*/
public class TestComboboxDemo02 extends JFrame {
public static void main(String[] args) {
new TestComboboxDemo02();
}
public TestComboboxDemo02(){ Container container = 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"); container.add(jList); this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

应用场景

- 选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容!

3.7、文本框

文本框

package com.love.lesson06;

import javax.swing.*;
import java.awt.*; /**
*文本框
*/
public class TestTextDemo01 extends JFrame {
public static void main(String[] args) {
new TestTextDemo01();
}
public TestTextDemo01(){ Container container = this.getContentPane(); JTextField textField = new JTextField("hello");
JTextField textField2 = new JTextField("world"); container.add(textField,BorderLayout.SOUTH);
container.add(textField2,BorderLayout.NORTH); this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

密码框

package com.love.lesson06;

import javax.swing.*;
import java.awt.*; /**
*密码框
*/
public class TestTextDemo02 extends JFrame {
public static void main(String[] args) {
new TestTextDemo02();
}
public TestTextDemo02(){ Container container = this.getContentPane(); JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*'); container.add(passwordField); this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

文本域

package com.love.lesson05;

import javax.swing.*;
import java.awt.*; public class JScrollDemo extends JFrame {
public static void main(String[] args) {
new JScrollDemo();
}
public JScrollDemo(){
//获取内容面板
Container container = this.getContentPane(); //文本域
JTextArea textArea = new JTextArea(20,50);
textArea.setText(" Who am i"); //Scroll(滚动)面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane); this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

最新文章

  1. node中子进程同步输出
  2. 初识UML类图--类之间关系
  3. Ubuntu常用命令大全(转)
  4. 图片上传前的预览(PHP)
  5. Spring事务Transaction配置的五种注入方式详解
  6. 静态编译Qt5.4.1和Qt WebKit
  7. hbase性能调优之压缩测试
  8. Python操作Access数据库
  9. 【ActiveMQ】持久化消息队列的三种方式
  10. 使用JDBC技术连接数据库(附源码)--JAVA的简单应用
  11. javascript设计模式——单例模式
  12. 【POJ3662】Telephone Lines dij + 二分答案
  13. 【Mysql sql inject】POST方法BASE64编码注入write-up
  14. java实现消息队列的两种方式
  15. 实现简单的shell sed替换功能
  16. bootstrap2.1
  17. php设计模式总结-单件模式
  18. PHP中redis的使用
  19. IntelliJ IDEA 2017版 spring-boot使用JdbcTemplate实例
  20. 解析html文档的java库及范例

热门文章

  1. noip31
  2. 伪静态是什么?伪静态与普通html静态网页区别?
  3. vue+cesium初探(一) 加载cesium
  4. 10.SpringMVC之格式化、校验
  5. python画循环圆
  6. mzy git学习,删除文件(三)
  7. SSM:Mybatis中引入通用mapper
  8. 阿里云(CentOS)搭建MediaWiki
  9. 老司机带你体验SYS库多种新玩法
  10. MySQL 实例空间使用率过高的原因和解决方法