欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java GridBagLayout简单电子邮件发送界面的实现

程序员文章站 2022-03-27 09:23:57
GridBagLayout网格块布局运行示例:代码:import java.awt.Component;import java.awt.Frame;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Label;import java.awt.TextArea;import java.awt.TextField;import java.awt.Window;import ....
  1. GridBagLayout网格块布局
    运行示例:
    Java GridBagLayout简单电子邮件发送界面的实现
    代码:
import java.awt.Component;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class EmailTest extends Frame{
	public EmailTest(){
		Label receiver=new Label("收件人:");
		Label send=new Label("抄送:");
		Label theme=new Label("主题:");
		TextField re=new TextField();
		TextField se=new TextField();
		TextField th=new TextField();
		TextArea con=new TextArea();
		setLayout(new GridBagLayout());
		GridBagConstraints gb=new GridBagConstraints();
		gb.fill=GridBagConstraints.HORIZONTAL;//水平填充
		gb.weightx=0;//行长不变
		gb.weighty=0;//列高不变
		addToBag(receiver, gb, 0, 0, 1, 1);
		addToBag(send, gb, 0, 1, 1, 1);
		addToBag(theme, gb, 0,2, 1, 1);
		gb.weightx=100;//行自适应缩放
		gb.weighty=0;//列高不变
		addToBag(re, gb,1,0, 1, 1);
		addToBag(se, gb,1,1, 2, 1);
		addToBag(th, gb,1,2, 2, 1);
		gb.fill=GridBagConstraints.BOTH;//剩余全部填充
		gb.weightx=100;//行自适应缩放
		gb.weighty=100;//列高自适应缩放
		addToBag(con, gb,0,4,3,1);
		addWindowListener(new closeWin());//窗口关闭的监听事件
		/*addWindowListener(new WindowAdapter(){      
       	    	
       	    	public void windowClosing(WindowEvent e){
       	    		dispose();
       	    	}
       	    });
		*/
		setSize(300,300);
		setVisible(true);
	}
	void addToBag(Component c,GridBagConstraints g,int x,int y,int w,int h) {
		g.gridx=x;
		g.gridy=y;//x、y为在网格中(x,y)坐标
		g.gridwidth=w;//横向占几个网格
		g.gridheight=h;//竖向占几个网格
		add(c,g);
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		new EmailTest();
	}
}
/*
 * 如果不写closeWin类,右上角×关闭窗口按钮可能会失效
 */
class closeWin extends WindowAdapter{
	public void windowClosing(WindowEvent e) {
		Window w=e.getWindow();
		w.dispose();
	}
	
}

注意窗口关闭类不重写会失效。

本文地址:https://blog.csdn.net/qq_44467720/article/details/112007314

相关标签: java