当前位置:网站首页>Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
2022-07-04 05:53:00 【Gentle ~】
Recommended reading :Java Detailed explanation of graphical interface (AWT、Swing) special column
List of articles
Container Inheritance system
1.Window
It is a top-level window that can exist independently , By default BorderLayout
Manage its internal component layout ;
2.Panel
Can accommodate other components , But it can't exist independently , It must be embedded in other containers for use , By default FlowLayout
Manage its internal component layout ;
3. ScrollPane
yes A container with scroll bars , Nor can it exist independently , By default BorderLayout
Manage its internal component layout ;
Commonly used API
Component
As the base class , The following common methods are provided to set the size of components 、 Location 、 Visibility, etc .
Method signature | Method function |
---|---|
setLocation(int x, int y) | Set the location of the component |
setSize(int width, int height) | Set the size of the component |
setBounds(int x, int y, int width, int height) | At the same time, set the position of the components 、 size |
setVisible(Boolean b) | Set the visibility of the component |
Container
As the root class of the container , The following methods are provided to access the components in the container .
Method signature | Method function |
---|---|
Component add(Component comp) | Add other components to the container ( This component can be either a normal component , It can also be a container ) , And return the added component |
Component getComponentAt(int x, int y) | Returns the component of the specified point |
int getComponentCount() | Returns the number of components in the container |
Component[] getComponents() | Return all components in the container |
Container demonstration
window
import java.awt.*;
public class FrameDemo {
public static void main(String[] args) {
//1. Create window objects
Frame frame = new Frame(" This is the first window container ");
// Set the position and size of the window
frame.setBounds(100,100,500,300);
// Set window visible
frame.setVisible(true);
}
}
Panel
because IDEA By default utf-8 Encoding , But at present, the code we execute is windows On the system , and windows The default encoding for the operating system is gbk, So it's messy , If there's a garbled code , Then just before running the current code , Set up a jvm Parameters -Dfile.encoding=gbk
that will do .
public class PanelDemo {
public static void main(String[] args) {
//1. establish Frame Container object
Frame frame = new Frame(" Here we are testing Panel");
//2. establish Panel Container object
Panel panel = new Panel();
//3. Go to Panel Add components to the container
panel.add(new TextField(" This is a test text "));
panel.add(new Button(" This is a test button "));
//4. hold Panel Add to Frame in
frame.add(panel);
//5. Set up Frame Location and size of
frame.setBounds(30,30,500,300);
//6. Set up Frame so
frame.setVisible(true);
}
}
ScrollPane
The program is clearly directed to ScrollPane Added... To the container A text box and a button , But you can only see A button , But I can't see the text box , Why is that Well ? This is because ScrollPane
Use BorderLayout
Layout manager , and BorderLayout
As a result, only one component in the container is displayed .
import java.awt.*;
public class ScrollPaneDemo {
public static void main(String[] args) {
//1. establish Frame Window object
Frame frame = new Frame(" Test here ScrollPane");
//2. establish ScrollPane object , And specify that there is a scroll bar by default
ScrollPane scrollPane = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
//3. Go to ScrollPane Add components to
scrollPane.add(new TextField(" This is the test text "));
scrollPane.add(new Button(" This is the test button "));
//4. hold ScrollPane Add to Frame in
frame.add(scrollPane);
//5. Set up Frame The location and size of
frame.setBounds(30,30,500,300);
//6. Set up Frame so
frame.setVisible(true);
}
}
Recommended reading :Java Detailed explanation of graphical interface (AWT、Swing) special column
边栏推荐
- FRP intranet penetration, reverse proxy
- Input displays the currently selected picture
- 安装 Pytorch geometric
- 724. 寻找数组的中心下标
- C语言中的函数(详解)
- Tutle clock improved version
- VB. Net GIF (making and disassembling - optimizing code, class library - 5)
- Recommended system 1 --- framework
- Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
- Principle and practice of common defects in RSA encryption application
猜你喜欢
ES6 模块化
js如何将秒转换成时分秒显示
1480. 一维数组的动态和
C语言中的函数(详解)
724. Find the central subscript of the array
Kubernets first meeting
buuctf-pwn write-ups (8)
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
随机推荐
A little understanding of GSLB (global server load balance) technology
[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx
el-select如何实现懒加载(带搜索功能)
复合非线性反馈控制(二)
C # character similarity comparison general class
MySQL information_ Schema database
HMS v1.0 appointment.php editid参数 SQL注入漏洞(CVE-2022-25491)
Penetration tool - sqlmap
LC周赛300
谷歌 Chrome 浏览器将支持选取文字翻译功能
Install pytoch geometric
每周小结(*63):关于正能量
VB. Net simple processing pictures, black and white (class library - 7)
js获取对象中嵌套的属性值
1480. Dynamic sum of one-dimensional array
What are the reasons for the frequent high CPU of ECS?
Introduction To AMBA 简单理解
如何展开Collapse 的所有折叠面板
1480. 一维数组的动态和
测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文