GUI编程
GUI编程:图形用户界面编程
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标事件
- 键盘事件
- 外挂
- 破解工具
1.简介
GUI的核心开发技术:Swing(封装过的,好看一些)、AWT (底层,难看)
GUI不流行的原因:1.界面不美观
2.需要jre环境 、
学习GUI的意义:是MVC的基础,有助于了解MVC,了解监听;可以写一些自己的小工具;工作可能需要维护到swing界面(几率较小)
2.AWT
2.1AWT介绍
- 抽象的窗口工具,包含了很多的类和接口,用于GUI编程
- 元素:窗口,Annie,文本框
- java.awt包
2.2组件和容器
2.2.1 Frame
//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame
Frame frame = new Frame("我的第一个ava图形界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色 需要color类
frame.setBackground(Color.green);
//弹出的初始位置,坐标0,0点在框的左上角
frame.setLocation(600,300);
//设置大小固定,不可手动调节
frame.setResizable(false);
//此时出来的窗口,只有最小化按钮有实际功能
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hYLkNsad-1609102299169)(C:\Xiang\图片\1608988924(1)].jpg)
存在问题:窗口点右上角X关闭不了,只能停止程序运行或者任务管理器才能关闭
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口 new
MyFrame myFrame = new MyFrame(600, 300, 600, 400, Color.yellow);
MyFrame myFrame2 = new MyFrame(700, 350, 600, 400, Color.green);
MyFrame myFrame3 = new MyFrame(800, 400, 600, 400, Color.red);
MyFrame myFrame4 = new MyFrame(900, 450, 600, 400, Color.darkGray);
}
}
class MyFrame extends Frame{
static int id=0; //可能存在多个窗口,需要一个计数器
//构造器
public MyFrame(int x,int y,int w,int h,Color color){
super("Myframe"+(++id));
setVisible(true);
setBackground(color);
setBounds(x,y,w,h);//x,y是初始位置坐标,w,h是窗口宽高
}
}
2.2.2 面板
代码末尾加入监听事件,解决了窗口关闭问题
//Panel 可以看成是一个空间,但不能单独存在
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//设置布局null,关掉默认的布局
frame.setLayout(null);
//坐标
frame.setBounds(600,400,400,300);
frame.setBackground(new Color(179,206,105));
//此处不知道颜色怎么选是,先随便写一组三个数字,然后左边会出来一个颜色框,点进去手动调颜色即可
//panel设置坐标,相对于frame(面板坐标是相对坐标) 在frame的核心窗口里面设置面板区域
panel.setBounds(50,50,300,200);
panel.setBackground(new Color(206,106,122));
//frame.add(panel) 往窗口里面添加面板
frame.add(panel);
frame.setVisible(true); //设置可见性
//监听事件:监听窗口关闭事件 System.exit(0) -----------------------设置关闭功能,点击X可以关掉窗口--------------
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
//点击窗口关闭的时候需要做的事情
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
2.2.3 布局管理器
- 流式布局(从左到右)(默认)
- 东西南北中(上下布局)
- 表格布局
//流式布局
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();
//Panel panel = new Panel();
//组件-按钮组件
Button button1 = new Button("按钮1");
Button button2 = new Button("按钮2");
Button button3 = new Button("按钮3");
//设置为流式布局
frame.setLayout(new FlowLayout());//默认为中
//frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //靠左布置
//frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //靠右布置
frame.setSize(400,200);
//frame.add(panel.add(button1));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
//东西南北中(上下布局)
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,300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
//点击窗口关闭的时候需要做的事情
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
//表格布局
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.setSize(400,300);
frame.setVisible(true); //设置可见
frame.addWindowListener(new WindowAdapter() {
//点击窗口关闭的时候需要做的事情
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
//联系,创建如下窗口
//思路:橙色p1,绿色p2,蓝色p3,红色p4;p1,p2以2行1列表格布局加进窗口(整个框);p1,p3分别以东西南北中布局包含p3,p4在中间(CENTER)位置
//p1,p3再分别东西插俩按钮;p3表格布局2行1列插俩按钮;p4表格布局2行2列插俩按钮
//窗口含p1,p2;p1含p3;p2含p4;各自再含按钮
public class MyTest {
public static void main(String[] args) {
//总窗口frame
Frame frame = new Frame();
frame.setVisible(true); //设置可见
frame.setSize(400,400);
frame.setLocation(600,400);
frame.setBackground(new Color(147,176,86));
frame.setLayout(new GridLayout(2,1));
//setLayout 设置布局,此处设置为表格布局,两行一列; //不指定时默认为流式布局 FlowLayout
//四个面板
Panel p1 = new Panel(new BorderLayout()); //BorderLayout 东西南北中布局
Panel p2 = new Panel(new GridLayout(2,1)); //GridLayout 表格布局
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
p1.add(new Button("East-1"),BorderLayout.EAST);
p1.add(new Button("West-1"),BorderLayout.WEST);
p2.add(new Button("p2-btn-1"));
p2.add(new Button("p2-btn-2"));
p1.add(p2,BorderLayout.CENTER);
p3.add(new Button("East-2"),BorderLayout.EAST);
p3.add(new Button("West-2"),BorderLayout.WEST);
p4.add(new Button("p4-btn-1"));
p4.add(new Button("p4-btn-2"));
p4.add(new Button("p4-btn-3"));
p4.add(new Button("p4-btn-4"));
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.addWindowListener(new WindowAdapter() {
//点击窗口关闭的时候需要做的事情
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
总结:
- Frame是一个顶级窗口
- Panel(面板)无法单独显示,必须添加到某个容器中(frame中或其他panel中)
- 布局管理器
- 流式
- 东西南北中
- 表格
- 大小,定位,背景颜色,可见性,监听
2.2.4 事件监听
事件监听:当某个事情发生的时候,做出什么相应的操作,一般会跟按钮配合使用
//案例,按下一个按钮会自动做出某些操作 事件监听
public class TestActionEvent {
public static void main(String[] args) {
//按下按钮,触发一些事件
Frame frame = new Frame();
Button button = new Button();
Button button2 = new Button();
frame.setLocation(300,500);
frame.setLayout(new GridLayout(2,1));
//因为addActionListener()需要一个ActionListener-事件监听器,所以我们需要构造一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener); //给按钮添加事件
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("这还是一个没卵用的按钮"); //自己创建的事件,输出一段话
}
});
frame.add(button);
frame.add(button2);
frame.setVisible(true);
frame.pack();
windowClose(frame); //关闭窗口
}
//将关闭窗体的事件抽成一个方法
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
//点击窗口关闭的时候需要做的事情
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("你按下了一个没有屁用的按钮"); //自己创建的事件,输出一段话
}
}
多个按钮,共享一个事件
public class TestActionTwo {
public static void main(String[] args) {
//两个按钮实现同一个监听
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("stop");
frame.setLocation(240,600);
MyMonitor myMonitor = new MyMonitor();
//可以显式地定义触发事件会返回的命令,如果不定义,会走默认的值
//可以多个按钮只写一个监听类
button1.setActionCommand("button1-start");
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
windowClose(frame);
frame.pack();
frame.setVisible(true);
}
//将关闭窗体的事件抽成一个方法
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
//点击窗口关闭的时候需要做的事情
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
class MyMonitor implements ActionListener{
public void actionPerformed(ActionEvent e) {
//e.getActionCommand()获得按钮信息
System.out.println("按钮被点击了:+msg---"+e.getActionCommand());
}
}
2.2.5 输入框TextField 监听事件
public class TestText01 {
public static void main(String[] args) {
//在main方法里面不放其他,只放启动程序的功能
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
//按下enter 就会触发这个输入框的时间
textField.addActionListener(myActionListener2);
//设置替换字符编码
textField.setEchoChar('*');//此时就是在文本框输入一些文本,在文本框中会输出*,但打印在控制台的依然是输入的内容
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener2 implements ActionListener{
public void actionPerformed(ActionEvent e) {
//e.getSource(); //获得一些资源,返回的是一个对象Object,可以向下转型
TextField field = (TextField)e.getSource();
System.out.println(field.getText()); //获得输入框中的文本
field.setText(""); //设置回车以后,输入的文本会清空(设置为""空字符串)
}
}
2.2.6 简易计算器(包含组合,内部类相关知识)
oop原则:组合大于继承
内部类:更好的包装;内部类最大的好处,就是可以畅通无阻地访问外部类的属性与方法
//写一个简易计算器 未调优
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator() {
//3个文本框 两个输入值,一个输出值
TextField num1 = new TextField(10);//字符数最多为10
TextField num2 = new TextField(10);
TextField num3 = new TextField(11);
//1个按钮 = 按下去开始计算并输出结果
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1,num2,num3)); //给button加监听事件
//一个标签 + 没有任何操作,仅表示一个符号(相当于做一些文字性的提示)
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
//获取3个变量
private TextField num1,num2,num3;
public MyCalculatorListener(TextField num1,TextField num2,TextField num3) {
this.num1=num1;
this.num2=num2;
this.num3=num3;
}
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().loadFrame();
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
//组件
num1 = new TextField(10);//字符数最多为10
num2 = new TextField(10);
num3 = new TextField(11);
Button button = new Button("=");
Label label = new Label("+");
//监听器
button.addActionListener(new MyCalculatorListener(this)); //给button加监听事件
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
//获取计算器对象,在一个类中组合另外一个类
Calculator calculator=null;
public MyCalculatorListener(Calculator calculator) {
this.calculator=calculator;
}
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("");
}
}
//写一个简易计算器 内部类优化 内部类最大的好处,就是可以畅通无阻地访问外部类的方法与属性
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);//字符数最多为10
num2 = new TextField(10);
num3 = new TextField(11);
Button button = new Button("=");
Label label = new Label("+");
//监听器
button.addActionListener(new MyCalculatorListener()); //给button加监听事件
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//监听器类
private class MyCalculatorListener implements ActionListener {
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.2.7 画笔
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(600,400,600,500);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//画笔
@Override
public void paint(Graphics g) {
//画笔需要颜色,可以画画
//g.setColor(Color.red);
//g.drawOval(200,200,100,200);//画空心圆 前两个数是圆心坐标相对窗口,后两个是横向与纵向轴长
g.fillOval(400,300,100,100);//画实心圆
//g.setColor(Color.green);
g.fillRect(100,100,200,110); //画矩形
//养成习惯:画笔用完,将它还原到最初的颜色
}
}
2.2.8 鼠标监听MouseListener
目的:想要实现鼠标画画
//测试鼠标监听事件
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画图");
}
}
class MyFrame extends Frame{
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
ArrayList points;
public MyFrame(String title){
super(title);
setBounds(600,400,400,300);
//存鼠标点击的点
points =new ArrayList<Object>();
//鼠标监听器,针对这个窗口
this.addMouseListener(new MyMouseListener());
setVisible(true);
addWindowListener(new WindowAdapter() {
//点击窗口关闭的时候需要做的事情
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 addPoint(Point point){
points.add(point);
}
//适配器模式
private class MyMouseListener extends MouseAdapter{
//鼠标 按下,弹起,长按
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame = (MyFrame)e.getSource();
//这里我们点击的时候就会在界面上产生一个点
//这个点就是鼠标的点
frame.addPoint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一遍
frame.repaint();//刷新
}
}
}
2.2.9 窗口监听 WindowListener
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame() {
setBackground(Color.BLUE);
setBounds(600, 400, 200, 200);
setVisible(true);
//addWindowListener(new MyWindowListenter());
this.addWindowListener(new WindowAdapter() {//使用匿名内部类
@Override
//关闭窗口
public void windowClosing(WindowEvent e) {
System.out.println("你点击了关闭");
setVisible(false);//隐藏窗口,可以通过按钮隐藏窗口
System.exit(0);//正常退出 0变成1就是非正常退出 //现在这两句就是先隐藏,然后再关闭
}
@Override
//激活窗口 鼠标点到别的项目离开这个窗口以后,再次点进这个窗口,窗口变高亮时就是激活
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
});
}
}
2.2.10 键盘监听
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(600,400,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override//键盘按下
public void keyPressed(KeyEvent e) {
//获得键盘按下的键是哪一个,当前键盘的码
int keyCode = e.getKeyCode();
System.out.println(keyCode); //数出按到的每个键对应的码(对应的是不同的数字)
//不用记这个值,直接使用其静态属性即可 VK_XXX
if(keyCode==KeyEvent.VK_UP){ //键盘上的 ↑ 按键
System.out.println("你按下了上键");
}
//根据按下不同的操作,产生不同的结果
}
});
}
}
## 3 Swing
3.1 窗口、面板
public class JFrameDemo {
//Init() 初始化
public void init(){
//顶级窗口 JFrame
JFrame jf = new JFrame("这是一个JFrame窗口");
jf.setVisible(true);
jf.setBounds(600,400,200,200);
jf.setBackground(Color.blue);//此处设置蓝色,并不会有任何显示(设置的是窗口颜色,但之后展示的是标签,标签会把窗口填充满,只能看到标签的颜色
//设置文字 JLable
JLabel label = new JLabel("这是一个练手的标签");
jf.add(label);
//关闭事件 JFrame中已经将关闭窗口写成了方法,直接调用即可
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
//让标签居中
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJFrame2().init();
}
}
class MyJFrame2 extends JFrame {
public void init(){ //这个init方法相当于构造器方法
this.setBounds(600,400,200,200);
this.setVisible(true);
JLabel label = new JLabel("这是一个练手的标签");
this.add(label);
//让标签文本居中
label.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器 JFrame窗口放东西,需要容器,使用getContentPane()方法
Container container = this.getContentPane();
container.setBackground(Color.green);
}
}
3.2 弹窗
JDialog 用来被弹出,默认就有关闭事件
//主窗口
public class DialogDemo extends JFrame {
public static void main(String[] args) {
new DialogDemo();
}
public DialogDemo(){ //此处是构造器,功能与前面示例中init()方法一样
this.setVisible(true);
this.setSize(400,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口 //继承JFrame后,此三行代码可以认为是建窗口的默认代码
//JFrame 放东西,需要容器,使用getContentPane()方法
Container container = this.getContentPane();
//绝对布局 使用绝对不觉后,组件输入坐标,都是按顶级窗口的坐标来定位的
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出对话框"); //创建对象
button.setBounds(30,30,200,50);
//点击这个按钮的时候,弹出一个弹窗
//监听事件
button.addActionListener(new ActionListener() {//监听器
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
container.add(button);
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{ //JDialog 弹窗类
public MyDialogDemo() {
this.setVisible(true);
this.setBounds(100,100,300,240);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 弹窗本身已包含关闭事件(弹窗本来就可以关闭),不用再手动添加,添加后会报异常
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("这是一个弹窗"));
}
}
3.3 标签
Jlabel
new Jlabel("xxx");
图标 ICON ,是一个接口,是标签的进阶
//图标是一个接口,需要实现类,实现类还需要继承JFrame
public class IconDemo extends JFrame implements Icon {
public static void main(String[] args) {
new IconDemo().init();
}
private int width;
private int heigth;
public IconDemo(){ } //无参构造
public IconDemo(int width,int heigth){
this.width=width;
this.heigth=heigth;
}
public void init(){
IconDemo iconDemo = new IconDemo(15, 15);
//此处图标放在标签上,也可以放在按钮上
JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
//重写接口中的方法
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,heigth); //重写方法话一个圆,xy圆心,后面宽高
}
public int getIconWidth() {
return this.width;
}
public int getIconHeight() {
return this.heigth;
}
}
public class ImageIconDemo extends JFrame {
public static void main(String[] args) {
new ImageIconDemo();
}
public ImageIconDemo(){
//获取图片的地址
JLabel label = new JLabel("果果");
URL url = ImageIconDemo.class.getResource("guoguo.jpg");
//获取该类统计资源中guoguo.jpg所在的路径,返回一个url路径地址 利用反射
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(600,400,200,200);
}
}
3.4 面板
Jpanel
public class JPanelDemo extends JFrame {
public static void main(String[] args) {
new JPanelDemo();
}
public JPanelDemo(){
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,20));
//GridLayout 表格布局 ,2行1列,后面两个参数10,20意思是间距 途中可以看到面板与面板间,行间距20,列间距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"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
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(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
JScrollPanel 滚动面板
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); //最多20行,每行最多50个字符
//当当前页面总行列不超过设置的20,50时,不会出现滚动条;当前页面较小时才会有滚动条出现
textArea.setText("滚动面板示例");//设置默认文本
//Scroll面板 文本域放在公洞面板中,滚动面板再放进容器里
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(600,400,400,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
3.5 按钮
//图片按钮
public class JButtonDemo01 extends JFrame {
public static void main(String[] args) {
new JButtonDemo01();
}
public JButtonDemo01(){
Container container = this.getContentPane();
URL url = JButtonDemo01.class.getResource("guoguo.jpg");
Icon icon = new ImageIcon(url); //以上这两句代码,将一个图片变成一个图标
//把这个图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮"); //鼠标悬浮上去的时候会有一个图片按钮提示
//把按钮加到容器中
container.add(button);
setVisible(true);
setSize(500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
- 单选按钮
//单选按钮
public class JButtonDemo02 extends JFrame {
public static void main(String[] args) {
new JButtonDemo02();
}
public JButtonDemo02(){
Container container = this.getContentPane();
//单选框
JRadioButton radioButton01 = new JRadioButton("iRadioButton01");
JRadioButton radioButton02 = new JRadioButton("iRadioButton02");
JRadioButton radioButton03 = new JRadioButton("iRadioButton03");
//由于单选框只能选择一个,所以将这3个单选框分成一个组;一个组中只能选择一个
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);
setVisible(true);
setSize(500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
- 复选按钮
//多选按钮
public class JButtonDemo03 extends JFrame {
public static void main(String[] args) {
new JButtonDemo03();
}
public JButtonDemo03(){
Container container = this.getContentPane();
//单选框
JRadioButton radioButton01 = new JRadioButton("iRadioButton01");
JRadioButton radioButton02 = new JRadioButton("iRadioButton02");
JRadioButton radioButton03 = new JRadioButton("iRadioButton03");
//多选框 可以选择多个选项,打钩
JCheckBox checkBox01 = new JCheckBox("checkBox01");
JCheckBox checkBox02 = new JCheckBox("checkBox02");
JCheckBox checkBox03 = new JCheckBox("checkBox03");
container.add(checkBox01,BorderLayout.NORTH);
container.add(checkBox02,BorderLayout.CENTER);
container.add(checkBox03,BorderLayout.SOUTH);
setVisible(true);
setSize(500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
3.6 列表
- 下拉框
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);
setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
- 列表框
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("列表1");
contents.add("列表2");
contents.add("列表3");
container.add(jList);
setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
- 应用场景
- 下拉框:选择地区或一些单个选项
- 列表:展示信息,一般是动态扩容的
3.8 文本框
- 文本框
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",20);
container.add(textField,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
- 密码域
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); //输入的内容在面板里面会自动显示为***
setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
- 文本域
//文本域,一般配合面板使用
public class TestTextDemo03 extends JFrame {
public static void main(String[] args) {
new TestTextDemo03();
}
public TestTextDemo03(){
Container container = this.getContentPane();
//文本域 可以换行
JTextArea textArea = new JTextArea(20,50);
textArea.setText("文本框示例");
//Scroll面板 文本域放在滚动面板中,滚动面板再放进容器里
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}