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

📄 mygridbagdemo.java

📁 用java语言编写的两个网格袋布局管理器
💻 JAVA
字号:
package com.libin.gridbaglayout;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MyGridBagDemo extends JFrame{
	
	
	private JTextArea  tx1,tx2;
	private JButton b1,b2,b3;
	private JTextField tf1;
	private String[] names={"iron","steel","temp"};
	private JComboBox combox1;
	
	private Container content;
	
	private GridBagLayout gbLayout;
	private GridBagConstraints  gbConstraints=new GridBagConstraints();
	
	
	
	public MyGridBagDemo(){
		
		this.setTitle("网格袋布局管理器使用例子");
		this.setBounds(150,200,400,300);
		
		content=this.getContentPane();
		gbLayout=new GridBagLayout();
		content.setLayout(gbLayout);
		
	    tx1=new JTextArea();
	    tx1.setText("TextArea1");
	    
	    tx2=new JTextArea();
	    tx2.setText("TextArea2");
	    
	    b1=new JButton("Button1");
	    b2=new JButton("Button2");
	    b3=new JButton("Button3");
	    
	    
	    tf1=new JTextField(30);
	    tf1.setText("TextField1");
	    
	    combox1=new JComboBox(names);
	    
	    
	    //需要调用添加组件的方法向容器中添加组件
	    gbConstraints.weightx=gbConstraints.weighty=100;
	    gbConstraints.fill=GridBagConstraints.BOTH;
	    this.addComponent(tx1,0,0,1,3);
	    
	    gbConstraints.weightx=gbConstraints.weighty=50;
	    gbConstraints.fill=GridBagConstraints.BOTH;
	    this.addComponent(b1,0,1,2,1);
	    
	    
	    gbConstraints.weightx=gbConstraints.weighty=1000;
	    gbConstraints.fill=GridBagConstraints.BOTH;
	    this.addComponent(b2,1,1,1,1);
	    
	    gbConstraints.weightx=gbConstraints.weighty=1000;
	    gbConstraints.fill=GridBagConstraints.BOTH;
	    this.addComponent(b3,1,2,1,1);
	    
	    
	    gbConstraints.weightx=gbConstraints.weighty=50;
	    gbConstraints.fill=GridBagConstraints.BOTH;
	    this.addComponent(combox1,2,1,2,1);
	    
	    
	    gbConstraints.weightx=gbConstraints.weighty=1000;
	    gbConstraints.fill=GridBagConstraints.BOTH;
	    this.addComponent(tf1,3,0,2,1);
	    
	    
	    gbConstraints.weightx=gbConstraints.weighty=1000;
	    gbConstraints.fill=GridBagConstraints.BOTH;
	    this.addComponent(tx2,3,2,1,1);
	    
	    
	    
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
		
	}
	
	
	/*
	 * 用户定义一个向容器中添加组件的方法
	 * */
	  private void addComponent( Component c,
		      int row, int column, int width, int height )
		   {
		      // set gridx and gridy 
		      gbConstraints.gridx = column;
		      gbConstraints.gridy = row;

		      // set gridwidth and gridheight
		      gbConstraints.gridwidth = width;   
		      gbConstraints.gridheight = height;

		      // set constraints
		      gbLayout.setConstraints( c, gbConstraints );  
		      content.add( c );      // add component 
		   }
	
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyGridBagDemo app=new MyGridBagDemo();
	}

}

⌨️ 快捷键说明

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