Java学习之GUI编程
- 这是什么?
- 它怎么玩?
- 我们该如何去在平时运用?
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具
1、简介
Gui的核心技术:Swing、AWT
1、因为界面不美观
2、需要jre环境!
为什么我们需要学习?
1、可以写出自己心中想要的一些小工具
2、工作时候,也可能需要维护到swing界面,概率极小
3、了解MVC框架,了解监听
2、AWT
2.1、介绍
1、包含了很多类和接口!GUI:图形用户界面编程!
2、元素:窗口,按钮,文本框
3、Java.awt
2.2、组件和容器
1、Frame
package com.wyy.lesson01;
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame,JDK, 看源码!
Frame frame = new Frame("我的第一个Java图像界面窗口");
//需要设置可见性 w h
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色 color
frame.setBackground(new Color(26, 232, 225));
//弹出的初始位置
frame.setLocation(883,412);
//设置大小固定
frame.setResizable(false);
}
}
问题:发现窗口关闭不掉,停止Java程序
尝试回顾封装
package com.wyy.lesson01;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口 new
new MyFrame(100,100,200,200,Color.blue);
new MyFrame(300,100,200,200,Color.yellow);
new MyFrame(100,300,200,200,Color.red);
new MyFrame(300,300,200,200,Color.white);
}
}
class MyFrame extends Frame{
static int id = 0;//可能存在多个窗口,我们需要一个计数器
public MyFrame(int x,int y,int w ,int h,Color color){
//窗口的标题
super("MyFrame+"+(++id));
//需要设置可见性 w h
setVisible(true);
//设置弹出的初始位,窗口大小
setBounds(x,y,w,h);
//设置背景颜色 color
setBackground(color);
}
}
2、面板panel
解决了关闭事件!!!
package com.wyy.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(200, 200, 500, 500);
//颜色
frame.setBackground(new Color(83, 236, 231));
//panel设置坐标,相对于frame
panel.setBounds(50, 50, 400, 400);
//颜色
panel.setBackground(new Color(231, 239, 7));
//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、布局管理器
-
流式布局
package com.wyy.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(200, 200); //把按钮添加上去 frame.add(button1); frame.add(button2); frame.add(button3); //可见 frame.setVisible(true); } }
-
东西南北中
package com.wyy.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.wyy.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); //Java函数! 自动选择最优布局 frame.pack(); //可见 frame.setVisible(true); } }
小任务:
实现这个图
package com.wyy.lesson01;
import java.awt.*;
//练习的demo
public class TestDemo01 {
public static void main(String[] args) {
//总Frame框框
Frame frame = new Frame("练习");
//大小
frame.setSize(400, 300);
//位置
frame.setLocation(300, 300);
//颜色
frame.setBackground(Color.black);
//可见性
frame.setVisible(true);
frame.setLayout(new GridLayout(2, 1));
//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("panel2-btn-1"));
panel2.add(new Button("panel2-btn-1"));
//第二个面板嵌入到第一个里面
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);
}
});
}
}
总结:
1、Frame是一个顶级窗口
2、panel无法单独显示,必须添加到某个容器中
3、布局管理器
1.流式
2.东西南北中
3.表格
4、大小,定位,背景颜色,可见性,监听
2. 4、事件监听
事件监听:当某个事件发生的时候,干什么?
package com.wyy.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("button1");
//因为,addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
//文本框
frame.add(button, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
windoClose(frame);//关闭窗口
}
//关闭窗体事件
private static void windoClose(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.wyy.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) {
//
System.out.println("按钮被点击了:msg=>" + e.getActionCommand());
if (e.getActionCommand().equals("start")){
}
}
}
2.5、输入框TextFiled监听
package com.wyy.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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();
//按enter 就会触发这个输入框的事件
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("");//null
}
}
2.6简易计算器,组合+内部类回顾复习
oop原则:组合,大于继承
package com.wyy.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() {
//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.wyy.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();
setVisible(true);
}
public Calculator() {
}
}
//监听器类
class MyCalculatorListener implements ActionListener {
//获取计算器这个对象,在一个类中组合另外一个类:
Calculator calculator = null;
// private TextField num1, num2, num3;
public MyCalculatorListener(Calculator calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
//2.将这个值 + 法运算后,放到第三个框
//3.清除前两个框
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
calculator.num3.setText(""+(n1+n2));
calculator.num1.setText("");
calculator.num2.setText("");
}
}
内部类:
- 更好的包装
package com.wyy.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();
setVisible(true);
}
public Calculator() {
}
//监听器类
//内部类最大的好处,就是可以畅通无阻的访问外部的属性和方法!
private class MyCalculatorListener 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.wyy.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,200,100,100);
// g.setColor(Color.green);
// 画一个实心的矩形
g.fillRect(200,100,150,150);
//养成一个习惯,画笔用完,将它还原到最初的颜色
}
}
2.8、鼠标监听
目的:实现鼠标画画!
思路图
package com.wyy.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, 500, 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 myframe = (MyFrame) e.getSource();
//这里我们点击的时候,就会在界面上,产生一个点!画
//这个点就是鼠标的点:
myframe.addPaint(new Point(e.getX(), e.getY()));
//每次点击鼠标都需要重新画一遍
myframe.repaint();//刷新 30帧 60帧
}
}
}
2.9、窗口监听
package com.wyy.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 MyWindowListener());
this.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");
}
}
);
}
// class MyWindowListener extends WindowAdapter {
// @Override
// public void windowClosing(WindowEvent e) {
// setVisible(false);//隐藏窗口,通过按钮,隐藏当前窗口
// System.exit(0);//正常退出
// }
// }
}
2.10、键盘监听
package com.wyy.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("你按下了上键");
}
//根据按下的不同操作,产生不同的结果
}
});
}
}
3、Swing
3.1、窗口、面板
package com.wyy.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,500,400);
jframe.setBackground(Color.cyan);
//设置文字 JLabel
JLabel jlabl = new JLabel("欢迎来到十九婊哥学习Java的代码世界");
jframe.add(jlabl);
//容器实例化
// jframe.getContentPane();
//关闭事件
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
标签居中
package com.wyy;
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(100,100,400,400);
this.setVisible(true);
//设置文字 JLabel
JLabel jLabel = new JLabel("欢迎来到十九婊哥学习Java的代码世界"/**这个也是居中 ,SwingConstants.CENTER*/);
this.add(jLabel);
//让文本标签居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.yellow);
}
}
3.2、弹窗
JDialog,用来被弹出,默认就有关闭事件!
package com.wyy.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.setBounds(100,100,400,300);
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 ActionListener() {//监听器
@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,400,300);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
// contentPane.setLayout(null);
contentPane.add(new JLabel("十九婊哥的Java弹窗",SwingConstants.CENTER));
}
}
3.3、标签
label
new JLabel("xxx")
图标ICON
package com.wyy.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 jLabel = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@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;
}
}
图片ICON
package com.wyy.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo() {
//获取图片的地址
JLabel jLabel = new JLabel("ImageIcon");
//读取当前目录下的资源
URL url = ImageIconDemo.class.getResource("tx.jpg");
ImageIcon imageIcon = new ImageIcon(url);//命名不要重复了
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
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.wyy.lesson05;
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 jPanel1 = new JPanel(new GridLayout(1,3));
JPanel jPanel2 = new JPanel(new GridLayout(1,2));
JPanel jPanel3 = new JPanel(new GridLayout(2,1));
JPanel jPanel4 = new JPanel(new GridLayout(3,2));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel2.add(new JButton("2"));
jPanel2.add(new JButton("2"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("3"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
contentPane.add(jPanel1);
contentPane.add(jPanel2);
contentPane.add(jPanel3);
contentPane.add(jPanel4);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JScrollPanel
package com.wyy.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollPanelDemo extends JFrame {
public JScrollPanelDemo() {
Container contentPane = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("欢迎学习Java");
//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 JScrollPanelDemo();
}
}
3.5、按钮
图片按钮
package com.wyy.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01(){
Container container = this.getContentPane();
//将一个图片变为一个图标
// URL resource = JButtonDemo01.class.getResource("tx.jpg");
URL resource = JButtonDemo01.class.getResource("tx.jpg");
// Icon icon = new ImageIcon(resource);
Icon icon = new ImageIcon(resource);
//把这个图片放到按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
//add
container.add(button);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
单选按钮
package com.wyy.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame {
public JButtonDemo02() {
Container container = this.getContentPane();
//将一个图片变为一个图标
URL resource = JButtonDemo01.class.getResource("tx.jpg");
Icon icon = new ImageIcon(resource);
//单选框
JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3");
//由于单选框只能选择一个,分组,一个组中只能选择一个
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
container.add(jRadioButton1, BorderLayout.CENTER);
container.add(jRadioButton2, BorderLayout.NORTH);
container.add(jRadioButton3, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
复选按钮
package com.wyy.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo03 extends JFrame {
public JButtonDemo03() {
Container container = this.getContentPane();
//将一个图片变为一个图标
URL resource = JButtonDemo01.class.getResource("tx.jpg");
Icon icon = new ImageIcon(resource);
//多选框
JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
container.add(jCheckBox1,BorderLayout.NORTH);
container.add(jCheckBox2,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
3.6、列表
下拉框
package com.wyy.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo01 extends JFrame {
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.setBounds(100,100,400,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
列表框
package com.wyy.lesson06;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02() {
Container container = this.getContentPane();
//生成列表的内容 稀疏数组 压缩用的
// String[] contents = {"1", "2", "3"};
Vector contents = new Vector();
//列表中需要放内容
JList jList = new JList(contents);
contents.add("张三");
contents.add("李四");
contents.add("王五");
container.add(jList);
this.setVisible(true);
this.setBounds(100, 100, 400, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
应用场景
- 选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容!
3.7、文本框
-
文本框
package com.wyy.lesson06; import javax.swing.*; import java.awt.*; public class TextTxtDemo01 extends JFrame { public TextTxtDemo01() { Container container = this.getContentPane(); JTextField textField = new JTextField("hello"); JTextField textField2 = new JTextField("world",20); container.add(textField,BorderLayout.NORTH); container.add(textField2,BorderLayout.SOUTH); this.setVisible(true); this.setBounds(100, 100, 400, 300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TextTxtDemo01(); } }
-
密码框
package com.wyy.lesson06; import javax.swing.*; import java.awt.*; public class TextTxtDemo02 extends JFrame { public TextTxtDemo02() { Container container = this.getContentPane(); //面板去做 //密码框 JPasswordField passwordField = new JPasswordField();//**** passwordField.setEchoChar('*'); container.add(passwordField); this.setVisible(true); this.setBounds(100, 100, 400, 300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TextTxtDemo02(); } }
-
文本域
package com.wyy.lesson05; import javax.swing.*; import java.awt.*; public class JScrollPanelDemo extends JFrame { public JScrollPanelDemo() { Container contentPane = this.getContentPane(); //文本域 JTextArea jTextArea = new JTextArea(20, 50); jTextArea.setText("欢迎学习Java"); //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 JScrollPanelDemo(); } }