GUI基础02

news/2025/2/26 16:53:53/文章来源:https://www.cnblogs.com/LyqStudyJava/p/18738952

Swing

1、JFrame:中文Lable乱码

package com.lesson04;import javax.swing.*;
import java.awt.*;public class JFrameDemo01 {//init 初始化 initializepublic void init() {JFrame jFrame = new JFrame();jFrame.setBounds(200,200,400,400);jFrame.setBackground(Color.YELLOW);jFrame.setVisible(true);Label label = new Label("小刘学Java");jFrame.add(label);//关闭事件jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JFrameDemo01().init();}
}

1.1、JFrame: 中文Lable不乱码

package com.lesson04;import javax.swing.*;
import java.awt.*;public class JFrameDemo {public static void main(String[] args) {new MyJFrame().init();}
}class MyJFrame extends JFrame {public void init() {this.setBounds(200,200,600,800);this.setVisible(true);JLabel jLabel = new JLabel("小刘学Java");this.add(jLabel);jLabel.setHorizontalAlignment(0);Container container = this.getContentPane();container.setBackground(Color.YELLOW);}
}

2、 JDialog弹窗

package com.lesson04;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;//主窗口
public class JDialogDemo extends JFrame {public JDialogDemo() {this.setBounds(100,100,400,400);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//JFrame放东西需要容器Container container = this.getContentPane();//绝对布局container.setLayout(null);//按钮JButton jButton = new JButton("点击弹出一个对话框");jButton.setBounds(30,30,200,30);//按钮事件监听jButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) { //监听器new MyJDialogDemo();}});container.add(jButton);}public static void main(String[] args) {new JDialogDemo();}
}class MyJDialogDemo extends JDialog {public MyJDialogDemo() {this.setBounds(100,100,200,200);this.setVisible(true);Container container = this.getContentPane();container.setLayout(null);container.add(new Label("小刘学Java"));}
}

3、 标签

1. label

new JLabel("XXX");

2. 图标ICON

package com.L.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(20,20);//图标可以放在标签上,也可以放在按钮上JLabel jLabel = new JLabel("iconText", iconDemo, SwingConstants.CENTER);Container container = getContentPane();container.add(jLabel);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {IconDemo iconDemo = new IconDemo();iconDemo.init();}@Overridepublic void paintIcon(Component c, Graphics g, int x, int y) {g.fillOval(x,y,width,height);}@Overridepublic int getIconWidth() {return this.width;}@Overridepublic int getIconHeight() {return this.height;}
}

3. 图片Icon

package com.L.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("tp01.jpg");ImageIcon imageIcon = new ImageIcon(url); //命名不要冲突了jLabel.setIcon(imageIcon);jLabel.setHorizontalAlignment(SwingConstants.CENTER);Container container = getContentPane();container.add(jLabel);setVisible(true);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);setBounds(100,100,200,200);}public static void main(String[] args) {new ImageIconDemo();}
}

4、面板

1. 普通面板

package com.L.lesson05;import javax.swing.*;
import java.awt.*;public class JPanelDemo extends JFrame {public JPanelDemo(){Container container = this.getContentPane();container.setLayout(new GridLayout(2,1,10,10));JPanel jPanel01 = new JPanel(new GridLayout(1, 3));JPanel jPanel02 = new JPanel(new GridLayout(1, 2));JPanel jPanel03 = new JPanel(new GridLayout(2, 1));JPanel jPanel04 = new JPanel(new GridLayout(3, 1));jPanel01.add(new JButton("1"));jPanel01.add(new JButton("1"));jPanel01.add(new JButton("1"));jPanel02.add(new JButton("2"));jPanel02.add(new JButton("2"));jPanel03.add(new JButton("3"));jPanel03.add(new JButton("3"));jPanel04.add(new JButton("4"));jPanel04.add(new JButton("4"));jPanel04.add(new JButton("4"));container.add(jPanel01);container.add(jPanel02);container.add(jPanel03);container.add(jPanel04);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setVisible(true);this.setBounds(100,100,200,200);}public static void main(String[] args) {new JPanelDemo();}
}

2. JScrollPanel

package com.L.lesson05;import javax.swing.*;
import java.awt.*;public class JScrollDemo extends JFrame {public JScrollDemo(){Container container = this.getContentPane();//文本域JTextArea jTextArea = new JTextArea(20,50);jTextArea.setText("小刘学Java");//Scroll面板JScrollPane jScrollPane = new JScrollPane(jTextArea);container.add(jScrollPane);this.setVisible(true);this.setBounds(100,100,200,350);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JScrollDemo();}
}

5、按钮

1. 图片按钮

package com.L.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("xx.jpg");ImageIcon imageIcon = new ImageIcon(resource);//把这个图标放在按钮上JButton jButton = new JButton();jButton.setIcon(imageIcon);jButton.setText("联想宇航员");jButton.setToolTipText("这是一个图片按钮");//addcontainer.add(jButton);this.setBounds(100,100,200,400);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setVisible(true);}public static void main(String[] args) {new JButtonDemo01();}
}

1. 单选按钮

package com.L.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("xx.jpg");ImageIcon imageIcon = new ImageIcon(resource);//单选框JRadioButton radioButton01 = new JRadioButton("radioButton01");JRadioButton radioButton02 = new JRadioButton("radioButton02");JRadioButton radioButton03 = new JRadioButton("radioButton03");//由于单选框只能选择一个,所以要分组,一个组中只能选择一个ButtonGroup buttonGroup = new ButtonGroup();buttonGroup.add(radioButton01);buttonGroup.add(radioButton02);buttonGroup.add(radioButton03);container.add(radioButton01,BorderLayout.NORTH);container.add(radioButton02,BorderLayout.CENTER);container.add(radioButton03,BorderLayout.SOUTH);this.setBounds(100,100,200,400);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setVisible(true);}public static void main(String[] args) {new JButtonDemo02();}}
  • 2. 复选按钮

package com.L.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("xx.jpg");ImageIcon imageIcon = new ImageIcon(resource);//多选框JCheckBox checkBox01 = new JCheckBox("checkBox-01");JCheckBox checkBox02 = new JCheckBox("checkBox-02");JCheckBox checkBox03 = new JCheckBox("checkBox-03");container.add(checkBox01,BorderLayout.SOUTH);container.add(checkBox02,BorderLayout.CENTER);container.add(checkBox03,BorderLayout.NORTH);this.setBounds(100,100,200,400);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setVisible(true);}public static void main(String[] args) {new JButtonDemo03();}
}

6、列表

1. 下拉框

package com.L.lesson06;import javax.swing.*;
import java.awt.*;public class TextComboBoxDemo01 extends JFrame {public TextComboBoxDemo01(){Container container = this.getContentPane();JComboBox status = new JComboBox();JPanel panel = new JPanel();status.addItem("null");status.addItem("正在热映");status.addItem("已下架");status.addItem("即将上架");panel.add(status);container.add(panel);this.setBounds(100,100,200,200);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setVisible(true);}public static void main(String[] args) {new TextComboBoxDemo01();}
}

2. 列表框

package com.L.lesson06;import javax.swing.*;
import java.awt.*;
import java.util.Vector;public class TextComboBoxDemo02 extends JFrame {public TextComboBoxDemo02(){Container container = this.getContentPane();//生成列表的内容
//        String[] contents = {"frank","zhang","Li"};Vector contents = new Vector();//把输入的内容放入列表中JList list = new JList(contents);contents.add("zhangSan");contents.add("LiSi");contents.add("XiaoLiu");container.add(list);this.setBounds(100,100,200,200);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setVisible(true);}public static void main(String[] args) {new TextComboBoxDemo02();}
}
  • 应用场景
    • 选择地区,或者一些单个选项。一般contents超过两个用下拉框,两个一下用单选框,节省内存
    • 列表,用来展示信息,一般是动态扩容的

7、文本框

  • 1. 本框

    package com.L.lesson06;import javax.swing.*;
    import java.awt.*;public class TextTextDemo01 extends JFrame{public TextTextDemo01(){Container container = this.getContentPane();JTextField textField01 = new JTextField("小刘学Java");JTextField textField02 = new JTextField("Java很难学",20);container.add(textField01,BorderLayout.NORTH);container.add(textField02,BorderLayout.SOUTH);this.setBounds(100,100,200,200);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setVisible(true);}public static void main(String[] args) {new TextTextDemo01();}
    }
    
  • 2. 密码框

    package com.L.lesson06;import javax.swing.*;
    import java.awt.*;public class TextTextDemo02 extends JFrame{public TextTextDemo02(){Container container = this.getContentPane();JPasswordField passwordField = new JPasswordField();    //******passwordField.setEchoChar('*');container.add(passwordField);this.setBounds(100,100,200,200);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setVisible(true);}public static void main(String[] args) {new TextTextDemo02();}
    }
    
  • 3. 文本域

package com.L.lesson05;import javax.swing.*;
import java.awt.*;public class JScrollDemo extends JFrame {public JScrollDemo(){Container container = this.getContentPane();//文本域JTextArea jTextArea = new JTextArea(20,50);jTextArea.setText("小刘学Java");//Scroll面板JScrollPane jScrollPane = new JScrollPane(jTextArea);container.add(jScrollPane);this.setVisible(true);this.setBounds(100,100,200,350);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JScrollDemo();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/890175.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

以下是使用:empty 搭配before实现表格中数据为空时的默认展示

以下是使用:empty 搭配before实现表格中数据为空时的默认展示// 在文件.vue中的table<template><!-- 省略其他代码 --><el-table-column prop="title" label="标题"></el-table-column><el-table-column prop="desc"…

07 常用优化器简介

模型能否准确地预测数据,是通过损失函数来衡量的。如何调整权重和偏差参数,从而最小化神经网络的损失函数,这是一类特定的优化算法。我们称它们为优化器(optimizer)。 为什么需要优化器? 因为损失函数参数众多且结构复杂,其导数置零的方程无法得到解析解或计算非常复杂。…

github-emoji-list

Github-emoji-list转载自https://github.com/caiyongji/emoji-list。emoji-list具体效果如下所示。人物:bowtie: :bowtie: 😄 :smile: 😆 :laughing:😊 :blush: 😃 :smiley: ☺️ :relaxed:😏 :smirk: 😍 :heart_eyes: 😘 :kissing_heart:😚 :kissing_closed_e…

2024年我读过的10本技术书籍(Java、架构、SRE运维)

什么是一本好书? 首先,我们需要定义什么是一本有价值的书籍。个人认为,一本好书应该具备以下特点:它能让我理解其中的一半+内容,引发共鸣,而剩下的部分内容则需要我反复阅读才能逐渐领会。如果我读完后发现完全理解了,那么这本书对我来说意义不大,因为它没有挑战我的思…

如何在WEB前端页面嵌入桌面程序EXE运行窗口?

在数字化转型浪潮中,企业常面临WEB与本地桌面应用割裂的痛点:‌复杂文档编辑需频繁切换系统,专属行业软件无法融入B/S架构‌……如何在WEB页面中直接调用并嵌入EXE程序窗口,实现真正的“云端+本地”一体化体验?猿大师中间件自从2019年发布以来,已经发布了三个重要产品:猿…

探索2025年客户管理新方向,看这家企业如何成功落地

某企业近年来销售业绩一直停滞不前,尽管产品质量过硬,企业的销售业绩却始终未能实现预期的突破。企业内部对销售策略进行了深入分析,发现销售业绩不佳的主要原因在于重复购买率低。 在管理层进一步分析后,发现是企业的销售和市场推广手段过于单一,缺乏有效的客户留存策略。…

使用Git拉取项目并对idea,vs-code配置(图解)

方法一:在线拉取(直接拉取到本地文档,后面还有一个IDEA直接在线拉取)在gitee中找到自己要拉取的项目,点击克隆/下载 对下图标红的命令复制打开你的文档,这个文件夹(名称不为中文)自己选择(想把项目保存到哪里看自己),我这里随便新建了一个文件夹。 注意:在这…

REACT: SYNERGIZING REASONING AND ACTING IN LANGUAGE MODELS

ReAct:结合推理(COT)和行动(动作生成)。 ReAct主要内容将COT方法与ACT方法结合起来,引入了 ReAct,这是一种新的基于提示的范式,旨在将推理和行动在语言模型中协同起来,以解决通用任务。 展示了在少样本学习设置中,ReAct 相较于仅进行推理或动作生成的先前方法的优势。…

数字孪生城市:北斗网格码破解地下管网难题!

城市地下管网如同人体的“血管”和“神经”,承载着供水、供电、燃气、通信等核心功能。然而,传统管理模式长期面临“看不见、摸不着、管不好”的困境:施工误挖、管线老化、信息孤岛等问题频发,每年因管网事故造成的经济损失高达数百亿元。如何破解这一难题?北斗网格码技术…

供应链管理系统--升鲜宝收银系统功能解析,登录、主界面UI 设计图(一)

供应链管理系统--升鲜宝收银系统功能解析,登录、主界面UI 设计图(一)登录界面

振动电阻式传感器测量模块 差动电阻式 驱动与测量RM502

振动电阻式传感器测量模块 差动电阻式 驱动与测量RM502RM502模块是一款高精度、多功能的传感器驱动与测量解决方案。本产品采用先进的模拟信号处理技术,专为差动电阻式传感器及其他高精度电阻测量应用而设计。通过恒流驱动与4路实时电流测量技术,配合高精度采样电阻,有效消除…

No.13 CSS--简介

一、CSS 1.1概念CSS(Cascading Style Sheets)层叠样式表,又叫级联样式表,简称样式表。 CSS文件后缀名为 .css。 CSS用于HTML文档中元素样式的定义。1.2 作用使用css的目的就是让网页具有美观一致的页面。1.3 语法CSS 规则由两个主要的部分构成 : 选择器,以及一条或多条声明(…