当前位置:网站首页>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
边栏推荐
- 基于opencv的实时睡意检测系统
- Kotlin学习笔记
- 超人飞来!Flutter 实现满屏的力量感动画!
- Redis布隆过滤器
- IJCAI 2022 | Can Graph Neural Networks Detect Anomalies?
- Mei cole studio OpenHarmony equipment development training notes - the first learn notes
- 方舟单机/管理员生物指令代码大全
- VSCode 必知必会的 20 个快捷键
- 1100 三角形面积
- Questions about Mac terminal custom commands and Mysql command
猜你喜欢
What are the latest developments in the handling of false information?KDD2022 "Fighting Misinformation and Responding to Media Bias" tutorial, 161 pages ppt
tar zcf是单线程瓶颈
OneNote 教程,如何在 OneNote 中检查拼写?
如何用WebSocket打造Web端IM即时通讯聊天
IJCAI 2022 | Can Graph Neural Networks Detect Anomalies?
学习笔记:线性表的顺序表示和实现(二级指针实现)
阿里云OSS文件下载到本地指定文件有坑
有幸与美团大佬共同探讨单节点连接数超1.5W的问题
Ansible自动化运维工具(一)安装及模块
自定义MVC
随机推荐
Kotlin之JSON格式解析
rk3588使用npu进行模型转换和推理,加速AI应用落地
MySQL权限管理
基于opencv的实时睡意检测系统
Kotlin注解
Kotlin Notes - Difference Between ForEach and ForEachIndexed
正则表达式与文本处理器
深度学习初步认知
Kotlin解析String路径小知识
如何用精益敏捷组合管理,提升研发效能?软件研发团队必看!
测试计划
劳务派遣业务流程图
1088 N的阶乘
Mysql管理指令
Kotlin reflection
CVPR 2022 ActivityNet竞赛冠军:中科院深圳先进院提出高低分双模态行为识别框架
方舟单机/管理员生物指令代码大全
随手记:laravel、updateOrCreate 和 updateOrInsert 的区别
PHPUnit 单元测试
1100 三角形面积