当前位置:网站首页>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);
}
}
边栏推荐
- tutle时钟改进版
- 每周小结(*63):关于正能量
- Write a complete answer applet (including single choice questions, judgment questions and multiple topics) (III) single choice questions, judgment questions, and the first question display
- Halcon图片标定,使得后续图片处理过后变成与模板图片一样
- Penetration tool - sqlmap
- 1480. Dynamic sum of one-dimensional array
- Signification des lettres du module optique et abréviation des paramètres Daquan
- Leakage detection relay jy82-2p
- BUU-Crypto-[HDCTF2019]basic rsa
- JS get the attribute values nested in the object
猜你喜欢

C language simple student management system (including source code)

JS how to convert seconds into hours, minutes and seconds display

一键过滤选择百度网盘文件

How much computing power does transformer have

我的NVIDIA开发者之旅——优化显卡性能

509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费

LM small programmable controller software (based on CoDeSys) note XXI: error 3703

Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor

Etcd database source code analysis - initialization overview
![[wechat applet] template and configuration (wxml, wxss, global and page configuration, network data request)](/img/78/63ab1a8bb1b6e256cc740f3febe711.jpg)
[wechat applet] template and configuration (wxml, wxss, global and page configuration, network data request)
随机推荐
Input displays the currently selected picture
input显示当前选择的图片
left_ and_ right_ Net normal version
BUU-Pwn-test_ your_ nc
fastjson
拓扑排序和关键路径的图形化显示
C语言简易学生管理系统(含源码)
补某视频网站的js,进行视频解密
Void convolution, deformable convolution, deformable ROI pooling
tutle时钟改进版
509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
Arc135 a (time complexity analysis)
(4) Canal multi instance use
Wechat applet +php realizes authorized login
安装 Pytorch geometric
FreeRTOS 中 RISC-V-Qemu-virt_GCC 的 锁机制 分析
ANSYS command
Basic concept of bus
Qt发布多语言国际化翻译
JS arguments parameter usage and explanation