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 = """
					
					"""
					+
					""+String.valueOf(i)+""
			+"";
			
			
			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);
		}
	}
	
	
}

JavaAwtSwing FlowLayout可以居左居中居右_第1张图片

你可能感兴趣的:(#,Java图形Awt,Swing,JavaFX,JAVA,java,开发语言)