当前位置:网站首页>Simple Swing interface notes
Simple Swing interface notes
2022-08-08 20:34:00 【Don't forget the original intention of z】
文章目录
very simple toSwing界面
第一种方式
//图形用户界面
fun main(args: Array) {
//创建窗口对象
val frame = JFrame(“MyFrame”)
// frame.title = “This is a header file name”
//创建Label
val label = JLabel(“Hello Swing”)
//获取窗口内容面板
val pane = frame.contentPane
//添加Labelto the content panel
pane.add(label)
//设置窗口大小
frame.setSize(500,500)
//设置窗口可见
frame.isVisible = true
}
第二种方式
package SwingDemo
import javax.swing.JFrame
import javax.swing.JLabel
//Panels are created in a class-to-class manner
class MyFrame(title:String) : JFrame(title) {
//初始化
init {
val label =JLabel(“Hello Swing”)
// Get the window to the content panel
val pane = contentPane
//添加labelto the content panel
pane.add(label)
setSize(500,500)
isVisible = true
}
}
fun main(args: Array) {
MyFrame(“MyFrame”)
}
If you use a class, it is better to use it in other classes.
ButtonSet up and add listeners
1,创建button按键,并且设置 事件监听器
init {
val label =JLabel(“Hello Swing”)
val button1 = JButton(“按键名”)
Get the window to the content panel
val pane = contentPane
//添加labelto the content panel,and add the button to it
pane.add(label,BorderLayout.TOP) 添加标签,and displayed at the top
pane.add(button,BorderLayout.CENTER) Add buttons and set center display
setSize(500,500)
isVisible = true
//设置监听器
button1.addactionListener {
通过lambdaIt is easy to achieve
label.text = “按键1Triggered successfully“
}
}
But there is a problem in the above code,Just close the window and the program is still running,So create a window to listen to events
addWindowListener(object : WindowAdapert{ objectThe role is an anonymous class,这个是kotlinBasic to key,Because it needs to be used in many placesobject关键字
override fun windowClosing(e: WindowEvent?) {
super.windowClosing(e)
System.exit(0) You can write an integer from the parameter inside
}
})
Swingto the layout manager
流式布局管理器: FlowLayout
具体方法
FlawLayout(align: Int) 创建一个flow对象,指定对齐方式,The default to horizontal and vertical gaps is5个单位
FlawLayout.CENTER Each row of components should be centered
FlawLayout.LEADING Each row of components should be aligned in the container direction to the start edge
FlawLayout.LEFT Each row of components should be left-aligned
FlawLayout.RIGHT Each row of components should be right-aligned
FlawLayout.TRAILING Each row of components should be aligned in the direction of the container to the end edge
Swing的默认布局管理器 BorderLayout
构造
BorderLayout(hgap : Int , vgap:Int) Specifies the gap between components
BorderLayout() Not writing by default means that there is no gap between components
具体方法:
Five common methods,Specifies the direction in the component's East-West, North-South
GridLayout 格子布局
This layout is suitable for layouts such as the left nine-square grid
构造
GridLayout(3,3) 3行3列的布局 It is Jiugongge
This layout will automatically adapt to the best fit,However, it is recommended to determine the layout rows and columns before use
绝对布局管理器
An absolute layout manager is one that does not use a layout manager for settings,Instead, use the following methods:
setLocation(X , Y) 设置组件的位置
setSize (width,height) 设置组件的大小
setBounds(X,Y,W,H)Set the size and position of the component
isResizable = Boolean Sets whether the window size is variable true可变
注意:If using a layout manager this3method no longer works.
Swing组件
所有的SwingComponents are inherited fromJComponent
标签 和 按钮
JLabel() 可以显示文本,Icons can also be displayed
构造函数比较多,Compiler hints can be viewed
Mainly to set text or pictures and the like
按钮 JButton
可以设置点击事件,Icon buttons can be set
The main constructor is 4个,See the compiler hints for details
图片 ImageIcon 继承 icon
How to use eg: JButton(Imageicon(图片路径)) Set up a picture button
文本输入组件
文本框(JTextField)
JTextField() 默认构造
JTextField(width:Int)Specifies the text width:主要用于FlowLayout布局
密码框(JPasswordField)
文本区(JTextArea)
复选框和单选按钮
复选框(JCheckBox)
单选按钮(JRadioButton)
构造函数:
JCheckBox():建立一个新的JChcekBox.
JCheckBox(Icon icon):Build one with an image but no textJCheckBox.
JCheckBox(Icon icon,boolean selected):Build one with an image but no textJCheckBox,and set its initial state(Is it selected).
JCheckBox(String text):Build one with textJCheckBox.
JCheckBox(String text,boolean selected):Build one with textJCheckBox,and set its initial state(Is it selected).
JCheckBox(String text,Icon icon):Create one with text and an imageJCheckBox,The initial state is None selected.
JCheckBox(String text,Icon icon,boolean selected):Create one with text and an imageJCheckBox,and set its initial state(Is it selected )
Checkbox and radio button constructors are similar,就不再重复了
下拉列表
JComboBox() 创建一个下拉列表
JComboBox(items:Array)创建一个下拉列表,itemsSet the options for the drop-down list,The content can be arbitrary,而不局限于String的
列表:
JList() Create a list for exclusive use
JList(listData) Specify the array as a list
分割面板
JSplitPane
JSplitPane(new Orientation:Int)创建分割面板,The parameter is the layout direction
JSplitPane(new Orientation:Int,newLeftComponent:Component,newRightComponent:Compoent)创建分割面板,指定布局方向,Specifies the left panel component,Specifies the right panel component
使用表格
JTable
JTable(dm:TableModel)Create tables from models,dm是模型对象,It contains the data to be displayed by the table
JTable(rowData:Array< Array > , ColuName:Array) Specify column names via a two-dimensional array,创建一个表格对象,rowData是表格中的数据,coluName是列名
JTable(numRows:int , numColumns:Int) Creates an empty table object specifying the number of rows and columns
table.font 设置字体
table.tableHeader.font Sets the table column header font
table.rowHeight Refers to the table row height
table.setselecttionMode //Set single selection or multiple selection
val rowSM = table.selectionModel 有返回值,Returns the current state model
rowSM.addListselectListener 设置监听器
Other methods will not be described one by one,具体百度
滚动面板 JScrollpane When the screen is not enough for display,A scrollable panel can be set
边栏推荐
- Notes: The difference between laravel, updateOrCreate and updateOrInsert
- From interview to autism, five rounds of interviews for byte software testing post, four hours of soul torture...
- leveldb-impl:level0
- Kotlin-学习的第五天之Handler
- 一文教你普罗米修斯Prometheus的基础应用
- 1259 Alice and Bob
- 简单Swing界面笔记
- LeetCode_2_两数相加
- PHP传递任意数量的函数参数
- 分门别类输入输出,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang基本数据类型和输入输出EP03
猜你喜欢
Maykel Studio OpenHarmony Device Development Training Notes - Chapter 6 Study Notes
2022-08-08 第六小组 瞒春 学习笔记
关于Mac终端自定义命令和Mysql命令问题
学习与尝试 --> 事件风暴
莅临GOPS大会龙智展位,获取Forrester最新报告:《Forrester Wave:2021年第四季度企业服务管理报告》
rk3588使用npu进行模型转换和推理,加速AI应用落地
监控工具普罗米修斯(Prometheus)的介绍与安装
What are the role of document management system for companies?
How can recommender systems be trusted?A review of the latest "Trusted Recommender System" from Rutgers University, a 43-page pdf explaining the composition and technology of trusted RS
fillder4 keeps prompting the system proxy was changed, watch me solve it
随机推荐
WPF主窗体调用 User32的SetWindowPos 设置窗体置顶会导致与其他窗体抢夺焦点的问题
超人飞来!Flutter 实现满屏的力量感动画!
源码分析MyCat专栏
技术分享活动
接口测试经典面试题:Session、cookie、token有什么区别?
文件上传接入阿里云OSS
Kotlin annotations
Maykel Studio OpenHarmony Device Development Training Notes - Chapter 6 Study Notes
Little knowledge about KotlinAndroid encounters
自然堂品牌焕新升级,携手代言人王一博彰显美妆年轻新态度
CSP-J2021 题解
实践篇1:深度学习之----LetNet之tensorflow2的实现
新库上线 | CnOpenData信息传输、软件和信息技术服务业工商注册企业基本信息数据
差点被ECCV错过的论文:视频理解新框架,仅用微调的「成本」,达到预训练的「全能」...
正则表达式的限定符、或运算符、字符类、元字符、贪婪/懒惰匹配
Kotlin-学习的第五天之Handler
方舟基础物品指令代码大全
一文教你普罗米修斯Prometheus的基础应用
Kotlin - learn the fifth day of the Handler
投资基金定投安全吗