⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testgridbaglayout.java

📁 java实例 本实例是初学java的人全程学习的很好辅助材料,从最简单的入门至后面可以自己开发出一个应用程序出来
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;

class TestGridBagLayout
{
	public static void main( String [] args )
	{
		JFrame f = new JFrame( "GridBag Example" );
		Container c = f.getContentPane();
		c.setLayout( new GridBagLayout());
		
		GridBagAdder.add( c, new Canvas(), 3, 2, 1, 1, 1, 0,
			GridBagConstraints.NONE, GridBagConstraints.CENTER );
		GridBagAdder.add( c, new JButton("1"), 0, 0, 5, 1, 0, 0,
			GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER );
		GridBagAdder.add( c, new JButton("2"), 0, 1, 1, 1, 0, 0,
			GridBagConstraints.BOTH, GridBagConstraints.CENTER );
		GridBagAdder.add( c, new JButton("3"), 1, 1, 1, 1, 1, 0,
			GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER );
		GridBagAdder.add( c, new JButton("4"), 2, 1, 1, 1, 0, 0,
			GridBagConstraints.BOTH, GridBagConstraints.CENTER );
		GridBagAdder.add( c, new JButton("5"), 3, 1, 2, 1, 0, 0,
			GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER );
		GridBagAdder.add( c, new JButton("6"), 0, 2, 1, 4, 0, 0,
			GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER );
		GridBagAdder.add( c, new JButton("7"), 1, 2, 3, 4, 0, 0,
			GridBagConstraints.BOTH, GridBagConstraints.CENTER );
		GridBagAdder.add( c, new JButton("8"), 4, 2, 1, 1, 0, 1,
			GridBagConstraints.BOTH, GridBagConstraints.CENTER );
		GridBagAdder.add( c, new JButton("9"), 4, 3, 1, 1, 0, 1,
			GridBagConstraints.BOTH, GridBagConstraints.CENTER );
		GridBagAdder.add( c, new JButton("10"), 4, 4, 1, 1, 0, 1,
			GridBagConstraints.BOTH, GridBagConstraints.CENTER );
		GridBagAdder.add( c, new JButton("11"), 4, 5, 1, 1, 0, 1,
			GridBagConstraints.BOTH, GridBagConstraints.CENTER );
		GridBagAdder.add( c, new JButton("12"), 0, 6, 5, 1, 0, 0,
			GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER );
		f.pack();
		f.setVisible(true);
	}


	static class GridBagAdder
	{
		static GridBagConstraints cons = new GridBagConstraints();  // Ok to reuse
		public static void add( Container container, Component comp, 
			int x, int y, int width, int height, int weightx, int weighty,
			int fill, int anchor )
		{
			cons.gridx = x;
			cons.gridy = y;
			cons.gridwidth = width;
			cons.gridheight = height;
			cons.weightx = weightx;
			cons.weighty = weighty;
			cons.fill = fill;
			cons.anchor = anchor;
			container.add( comp, cons );
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -