JFrame中添加和设置JPanel的方法实例解析
作者:轻雨漫步 时间:2022-07-02 06:04:41
Swing 程序用JFrame 对象实现了它们的窗口。JFrame 类是AWT Frame 类的一个子类。它还加入了一些Swing 所独有的特性。与 Frame 的使用十分相似。唯一的区别在于,你不能将组件加入到JFrame中。你可以或者将组件加入到JFrame 的content pane(内容面板) 中,或者提供一个新的content pane(内容面板)。
面板与顶层容器的不同点:面板不能独立存在,必须被添加到其他容器内部(面板可以嵌套)。
JFrame 有一个 Content Pane,窗口能显示的所有组件都是添加在这个 Content Pane 中。JFrame 提供了两个方法: getContentPane 和 setContentPane 就是用于获取和设置其 Content Pane 的。
对JFrame添加组件有两种方式:
1)用 getContentPane ()方法获得JFrame的内容面板,再对其加入组件:frame. getContentPane ().add(childComponent)
2)建立一个Jpanel或JDesktopPane之类的中间容器,把组件添加到容器中,用setContentPane()方法把该容器置为JFrame的内容面板:
JPanel contentPane = new JPanel();
……//把其它组件添加到Jpanel中;
frame.setContentPane(contentPane);
//把contentPane对象设置成为frame的内容面板
实例程序:
import java.awt.*;
import javax.swing.*;
public class JFrameWithPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("Frame With Panel");
Container contentPane = frame.getContentPane();
contentPane.setBackground(Color.CYAN); // 将JFrame实例背景设置为蓝绿色
JPanel panel = new JPanel(); // 创建一个JPanel的实例
panel.setBackground(Color.yellow); // 将JPanel的实例背景设置为黄色
JButton button = new JButton("Press me");
panel.add(button); // 将JButton实例添加到JPanel中
contentPane.add(panel, BorderLayout.SOUTH); // 将JPanel实例添加到JFrame的南侧
frame.setSize(300, 200);
frame.setVisible(true);
}
}
截图:
来源:http://blog.csdn.net/lyxaiclr/article/details/7366145/
标签:java,jframe,jpanel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot2启动时执行,初始化(或定时任务)servletContext问题
2022-10-18 13:14:30
图文详解SpringBoot中Log日志的集成
2023-05-27 09:55:13
![](https://img.aspxhome.com/file/2023/5/62425_0s.png)
java高并发的并发级别详解
2023-08-05 15:44:38
![](https://img.aspxhome.com/file/2023/9/78259_0s.jpg)
基于Java实现的图的广度优先遍历算法
2021-06-02 06:51:20
Mybatis-plus多数据源配置的两种方式总结
2023-07-24 05:22:48
![](https://img.aspxhome.com/file/2023/3/57613_0s.png)
Java多线程的临界资源问题解决方案
2021-12-29 07:44:35
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
2021-06-01 14:28:00
![](https://img.aspxhome.com/file/2023/3/66253_0s.jpg)
java的main方法中调用spring的service方式
2023-03-29 00:12:16
idea打包java程序(包含依赖的所有jar包)
2023-02-13 00:10:26
![](https://img.aspxhome.com/file/2023/7/74977_0s.png)
springBoot Junit测试用例出现@Autowired不生效的解决
2023-01-24 12:57:59
![](https://img.aspxhome.com/file/2023/0/73120_0s.png)
springboot更新配置Swagger3的一些小技巧
2023-08-28 06:31:43
基于JavaMail的Java邮件发送
2022-10-30 18:56:56
![](https://img.aspxhome.com/file/2023/1/60931_0s.jpg)
java睡眠排序算法示例实现
2022-04-01 00:13:32
![](https://img.aspxhome.com/file/2023/2/69882_0s.png)
Springboot使用test无法启动问题的解决
2021-07-03 22:09:23
![](https://img.aspxhome.com/file/2023/2/83432_0s.png)
Spring Boot Admin实践详解
2023-08-25 06:57:53
详解AngularJs与SpringMVC简单结合使用
2023-10-22 04:19:08
SpringCloud之@FeignClient()注解的使用方式
2022-05-16 04:22:40
![](https://img.aspxhome.com/file/2023/9/82979_0s.jpg)
全面详解Maven打包及其相关插件和高级特性
2022-03-03 21:10:00
Java结构型设计模式中建造者模式示例详解
2023-04-24 13:37:31
![](https://img.aspxhome.com/file/2023/6/60286_0s.png)
Java8新特性之默认方法(default)浅析
2023-10-03 10:41:13
![](https://img.aspxhome.com/file/2023/1/62051_0s.png)