JavaAwtSwing FlowLayout可以居左居中居右

  • new FlowLayout(FlowLayout.LEFT);
  • new FlowLayout(FlowLayout.CENTER);
  • new FlowLayout(FlowLayout.RIGHT);
package flowlayout;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;

public class FlowLayout可以居左居中居右 {
	
	static Frame frame = new Frame(Thread.currentThread().getStackTrace()[1].getClassName());
	static {
		frame.addWindowListener(new WindowAdapter() {
			@Override public void windowClosing(WindowEvent event) { System.exit(0); }
		});
		
		frame.setBounds(100, 100, 1600, 800);
		
		GridLayout layoutRoot = new GridLayout(0, 1); frame.setLayout(layoutRoot);
		
	}
	

	public static void main(String[] arguments)throws Exception{
		
		
		f1(1); f1(10); f1(100);
		
		
		
		frame.setVisible(true);
		
		
	}
	
	static void f1(int quantity) {
		GridLayout gridLayoutA = new GridLayout(0, 3, 0, 0);
		Panel panelZ1 = new Panel(gridLayoutA); frame.add(panelZ1);

		
		
		FlowLayout flowLayoutZ2Left = new FlowLayout(FlowLayout.LEFT);
		Panel panelZ2Left = new Panel(flowLayoutZ2Left); panelZ1.add(panelZ2Left);
		panelZ2Left.setBackground(new Color((int)(Math.random()*256*256*256)));
		
		FlowLayout flowLayoutZ2Center = new FlowLayout(FlowLayout.CENTER);
		Panel panelZ2Center = new Panel(flowLayoutZ2Center); panelZ1.add(panelZ2Center);
		panelZ2Center.setBackground(new Color((int)(Math.random()*256*256*256)));
		
		FlowLayout flowLayoutZ2Right = new FlowLayout(FlowLayout.RIGHT);
		JPanel panelZ2Right = new JPanel(flowLayoutZ2Right); panelZ1.add(panelZ2Right);
		panelZ2Right.setBackground(new Color((int)(Math.random()*256*256*256)));
		
		
		for(int i=0; i<quantity; i++) {
			String h = """
					<html><head><style>
						body{font-size:16px; }
					</style></head><body>
					"""
					+
					"<b style='font-size:16px; background-color:yellow;'>"+String.valueOf(i)+"</b>"
			+"</body></html>";
			
			
			JLabel jlabel = new JLabel(h); jlabel.setOpaque(true); jlabel.setBackground(Color.GREEN);
			panelZ2Left.add(jlabel);
			
			
			jlabel = new JLabel(h); jlabel.setOpaque(true); jlabel.setBackground(Color.GREEN);
			panelZ2Center.add(jlabel);
			
			
			
			jlabel = new JLabel(h); jlabel.setOpaque(true); jlabel.setBackground(Color.GREEN);
			panelZ2Right.add(jlabel);
		}
	}
	
	
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kfepiza

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值