当前位置:网站首页>Component、Container容器常用API详解:Frame、Panel、ScrollPane
Component、Container容器常用API详解:Frame、Panel、ScrollPane
2022-07-04 05:35:00 【斯文~】
Container继承体系

1.Window是可以独立存在的顶级窗口,默认使用BorderLayout管理其内部组件布局;
2.Panel可以容纳其他组件,但不能独立存在,它必须内嵌其他容器中使用,默认使用FlowLayout管理其内部组件布局;
3. ScrollPane是 一个带滚动条的容器,它也不能独立存在,默认使用 BorderLayout 管理其内部组件布局;
常用API
Component作为基类,提供了如下常用的方法来设置组件的大小、位置、可见性等。
| 方法签名 | 方法功能 |
|---|---|
| setLocation(int x, int y) | 设置组件的位置 |
| setSize(int width, int height) | 设置组件的大小 |
| setBounds(int x, int y, int width, int height) | 同时设置组件的位置、大小 |
| setVisible(Boolean b) | 设置该组件的可见性 |
Container作为容器根类,提供了如下方法来访问容器中的组件。
| 方法签名 | 方法功能 |
|---|---|
| Component add(Component comp) | 向容器中添加其他组件 (该组件既可以是普通组件,也可以是容器) , 并返回被添加的组件 |
| Component getComponentAt(int x, int y) | 返回指定点的组件 |
| int getComponentCount() | 返回该容器内组件的数量 |
| Component[] getComponents() | 返回该容器内的所有组件 |
容器演示
window

import java.awt.*;
public class FrameDemo {
public static void main(String[] args) {
//1.创建窗口对象
Frame frame = new Frame("这是第一个窗口容器");
//设置窗口的位置和大小
frame.setBounds(100,100,500,300);
//设置窗口可见
frame.setVisible(true);
}
}
Panel
由于IDEA默认使用utf-8进行编码,但是当前我们执行代码是是在windows系统上,而windows操作系统的默认编码是gbk,所以会乱码,如果出现了乱码,那么只需要在运行当前代码前,设置一个jvm参数 -Dfile.encoding=gbk 即可。
public class PanelDemo {
public static void main(String[] args) {
//1.创建Frame容器对象
Frame frame = new Frame("这里在测试Panel");
//2.创建Panel容器对象
Panel panel = new Panel();
//3.往Panel容器中添加组件
panel.add(new TextField("这是一个测试文本"));
panel.add(new Button("这是一个测试按钮"));
//4.把Panel添加到Frame中
frame.add(panel);
//5.设置Frame的位置和大小
frame.setBounds(30,30,500,300);
//6.设置Frame可见
frame.setVisible(true);
}
}
ScrollPane
程序明明向 ScrollPane 容器中添加了 一个文本框和一个按钮,但只能看到 一个按钮,却看不到文本框 ,这是为什么 呢?这是因为ScrollPane使用 BorderLayout 布局管理器的缘故,而 BorderLayout 导致了该容器中只有一个组件被显示出来。
import java.awt.*;
public class ScrollPaneDemo {
public static void main(String[] args) {
//1.创建Frame窗口对象
Frame frame = new Frame("这里测试ScrollPane");
//2.创建ScrollPane对象,并且指定默认有滚动条
ScrollPane scrollPane = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
//3.往ScrollPane中添加组件
scrollPane.add(new TextField("这是测试文本"));
scrollPane.add(new Button("这是测试按钮"));
//4.把ScrollPane添加到Frame中
frame.add(scrollPane);
//5.设置Frame的位置及大小
frame.setBounds(30,30,500,300);
//6.设置Frame可见
frame.setVisible(true);
}
}
边栏推荐
- Build an Internet of things infrared temperature measuring punch in machine with esp32 / rush to work after the Spring Festival? Baa, no matter how hard you work, you must take your temperature first
- 我的NVIDIA开发者之旅——优化显卡性能
- js如何将秒转换成时分秒显示
- Integer type of C language
- Halcon图片标定,使得后续图片处理过后变成与模板图片一样
- left_and_right_net正常版本
- ANSYS command
- How to clone objects
- The end of the Internet is rural revitalization
- 补某视频网站的js,进行视频解密
猜你喜欢

配置交叉编译工具链和环境变量

HMS v1.0 appointment.php editid参数 SQL注入漏洞(CVE-2022-25491)

Penetration tool - sqlmap

Principle and practice of common defects in RSA encryption application

What is MQ?

Online shrimp music will be closed in January next year. Netizens call No

(4) Canal multi instance use

The data mark is a piece of fat meat, and it is not only China Manfu technology that focuses on this meat

Steady! Huawei micro certification Huawei cloud computing service practice is stable!

724. 寻找数组的中心下标
随机推荐
BUU-Crypto-[HDCTF2019]basic rsa
The data mark is a piece of fat meat, and it is not only China Manfu technology that focuses on this meat
[MySQL practice of massive data with high concurrency, high performance and high availability -8] - transaction isolation mechanism of InnoDB
Ping port artifact psping
如何获取el-tree中所有节点的父节点
fastjson
卸载Google Drive 硬盘-必须退出程序才能卸载
[Excel] 数据透视图
Zzulioj:1201: mode problem
Leetcode 184 Employees with the highest wages in the Department (July 3, 2022)
How to determine whether an array contains an element
Wechat applet +php realizes authorized login
力扣(LeetCode)184. 部门工资最高的员工(2022.07.03)
Accidentally deleted the data file of Clickhouse, can it be restored?
JS扁平化数形结构的数组
FreeRTOS 中 RISC-V-Qemu-virt_GCC 的 锁机制 分析
509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
BUU-Crypto-[GXYCTF2019]CheckIn
Etcd database source code analysis - initialization overview
检漏继电器JY82-2P