流式布局管理器(javax.swing,界面GUI)

实验64:流式布局管理器

编写一个java程序,在程序中生成一个框架窗口,设置窗口的布局管理器为流式布局管理器,往窗口中加入3个按钮

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class MyFlowLayout extends JFrame{
	public MyFlowLayout(String filename) {
		super(filename);
	}
	public static void main(String[] args) {
		MyFlowLayout frm = new MyFlowLayout("流式布局管理器");
		// 创建一个流式布局管理器的一个实例flow,指定对齐方式为居中对齐
		FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
		JButton b1 = new JButton("第一个按钮");
		JButton b2 = new JButton("第二个按钮");
		JButton b3 = new JButton("第三个按钮");
		// 设置frm的页面布局为flow
		frm.setLayout(flow);
		frm.setSize(200, 150);
		// 设置背景颜色
		Container c = frm.getContentPane();
		c.setBackground(Color.PINK);
		frm.add(b1);
		frm.add(b2);
		frm.add(b3);
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frm.setVisible(true);
	}
}

效果截图:


拖动:


猜你喜欢

转载自blog.csdn.net/weixin_39778570/article/details/80571187
今日推荐