自定义布局:
1.取消布局器
contentPane.setLayout(null);//取消布局器
2.使用setBounds()对子控件手动布局 new Rectangle(x,y,宽,高)
jLabel1.setBounds(new Rectangle(0,0,200,200)); jLabel2.setBounds(new Rectangle(100,100,200,100));
package com.java.gui10;
import javax.swing.*;
import java.awt.*;
/**
* @author zyx
* @create 2021-06-04 9:11
*
* 自定义布局
*/
public class MyJFrame extends JFrame {
public MyJFrame(String title) {
super(title);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);//取消布局器
JLabel jLabel1 = new JLabel("1");
JLabel jLabel2 = new JLabel("2");
jLabel1.setBackground(Color.yellow);
jLabel2.setBackground(Color.blue);
//设置控件位置
jLabel1.setBounds(new Rectangle(0,0,200,200));//控件1会覆盖掉部分控件二
jLabel2.setBounds(new Rectangle(100,100,200,100));
jLabel1.setOpaque(true);//设置控件是否透明,true:不透明

这篇博客探讨了如何在Java GUI中取消默认布局器,转而使用setBounds()方法进行手动布局,详细介绍了设置子组件位置和大小的过程。
最低0.47元/天 解锁文章

445

被折叠的 条评论
为什么被折叠?



