嵌套的面板

本文展示了一个使用Java Swing创建的简单GUI应用示例——嵌套面板布局。该示例包含两个子面板(绿色和红色),它们被嵌套在一个蓝色的主面板中,并通过JFrame显示出来。每个子面板都设定了特定的颜色背景并添加了一个标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//**********************************************
//  NestedPanels.java    Author: Lewis/Loftus
//**********************************************

import java.awt.*;
import javax.swing.*;

public class NestedPanels
{
	//---------------------------------------------------
	// Prestents two colored panels nested with a third
	//---------------------------------------------------
	public static void main(String[] args)
	{
		JFrame frame = new JFrame("Nested Panels");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Set up first subpanel
		JPanel subPanel1 = new JPanel();
		subPanel1.setPreferredSize(new Dimension(150,100));
		subPanel1.setBackground(Color.green);
		JLabel label1 = new JLabel("One");
		subPanel1.add(label1);
		
		// Set up second subpanel
		JPanel subPanel2 = new JPanel();
		subPanel2.setPreferredSize(new Dimension(150,100));
		subPanel2.setBackground(Color.red);
		JLabel label2 = new JLabel("Two");
		subPanel2.add(label2);
		
		// Set up primary panel
		JPanel primary = new JPanel();
		primary.setBackground(Color.blue);
		primary.add(subPanel1);
		primary.add(subPanel2);
		frame.getContentPane().add(primary);
		frame.pack();
		frame.setVisible(true);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值