Java -- AWT , GUI图形界面

1. AWT 容器继承关系

示例1:

public class Main {		
	public static void main(String[] args) throws Exception {
	
		Frame f = new Frame();		
		Panel p = new Panel();    //Panel容器不能单独存在,需要添加组件,放到frame或其他容器中
		p.add(new TextField(20));
		p.add(new Button("clicked me"));	
		
		ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);//ScrollPanle也不能单独存在		
		sp.add(p);
		f.add(sp);
		f.setBounds(30, 30, 300, 300);
		f.setVisible(true);		
	}	
}

 

2. 布局管理器: 默认的布局管理器是 BorderLayout.

FlowLayout布局: 流水一样,向某一方向顺序排列,直到边界再返回 继续顺序排列。

public class Main {
		
	public static void main(String[] args) throws Exception {
	
		Frame f = new Frame();		
		f.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 5));	//Layout不采用默认构造器,左对齐,垂直水平间距为20 5 	
		for(int i=0; i<10; i++)
		{
			f.add(new Button("clicked me" + i));
		}
		
		f.setBounds(30, 30, 300, 300);
		f.setVisible(true);		
	}	
}


BorderLayout : 分为东南西北中5个区域,每个区域只能添加一个组件, 但是容器也是一种组件,所以可以先添加到一个容器里。

public class Main {
		
	public static void main(String[] args) throws Exception {
	
		Frame f = new Frame();		
		f.setLayout(new BorderLayout(20,5)); //设置5部分之间的垂直水平间隔 20 5			
		f.add(new Button("South"), BorderLayout.SOUTH);
		f.add(new Button("North"), BorderLayout.NORTH);
		f.add(new Button("West"), BorderLayout.WEST);
		f.add(new Button("East"), BorderLayout.EAST);
		f.add(new Button("Center"), BorderLayout.CENTER);
		
		f.setBounds(30, 30, 300, 300);
		f.setVisible(true);		
	}	
}


GridLayout : 网格布局

public class Main {
		
	public static void main(String[] args) throws Exception {
	
		Frame f = new Frame();					
		Panel p = new Panel();
		p.setLayout(new GridLayout(3, 5, 4, 4));
		String [] names = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
				"+", "-", "*", "/"};
		for(int i=0; i!=names.length; i++)
		{
			p.add(new Button(names[i]));
		}
		p.add(new Button("Clicked me"), null, 14); //插入指定位置
		
		f.add(p);
		f.setBounds(30, 30, 300, 300);
		f.setVisible(true);		
	}	
}

 

GridBagLayout: 和GridLayout相似,不同的是一个组件可以跨越多个网格,并且可以设置网格的大小互不相同。

参考文章:    GridBagLayout网格包布局管理器:http://wenku.baidu.com/link?url=hVhmI-yAsMyblLShSPOCB89ffR3Nqsur8onMDVPzgdtTOuU-13U7qhtEhFygkkWSRvooneqSdBduX-Ctj6zjPxIUVrTmWGve26MDvcCNVF3

public class Main {
		
	private Frame f = new Frame("test GridBag");
	private GridBagLayout gb = new GridBagLayout();
	private GridBagConstraints gbc = new GridBagConstraints();
	private Button[] bs = new Button[10];
	
	private void init()
	{
		f.setLayout(gb);
		for(int i=0; i<bs.length; i++)
		{
			bs[i] = new Button("Button" + i);
		}
		
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 0;  //设置缩放时的比例
		gbc.weighty = 0;
		addButton(bs[0]);
		addButton(bs[1]);
		addButton(bs[2]);		
		gbc.gridwidth = GridBagConstraints.REMAINDER; //设置为边界
		addButton(bs[3]);
		
		gbc.weightx = 1;   
		gbc.weighty = 1;
		gbc.gridwidth = 1;
		addButton(bs[4]);		
		addButton(bs[5]);
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		addButton(bs[6]);
		
		gbc.gridwidth = 2;   //所占网格个数 
		gbc.gridheight = 2;
		gbc.ipadx = 50;   //组件横纵 所占像素个数
		gbc.ipady = 50;
		addButton(bs[7]);
		
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weighty = 0;
		gbc.ipadx = 0; 
		gbc.ipady = 0;
		addButton(bs[8]);
		
		gbc.gridx = 4;  //组件插入到网格的坐标
		gbc.gridy = 4;
		addButton(bs[9]);
								
		f.pack();
		f.setVisible(true);
	}
	
	private void addButton(Button button)
	{
		gb.setConstraints(button, gbc);
		f.add(button);
	}
	
	public static void main(String[] args) throws Exception {
		new Main().init();
	}	
}



CardLayout: 卡片一样堆列, 每次只能看到最上面的一张。

public class Main {
		
	private Frame f = new Frame("test CardLayout");
	String [] names = {"NO1", "NO2", "NO3", "NO4", "NO5"};
	Panel p1 = new Panel();
	CardLayout c = new CardLayout();
	
	public void init()
	{
		p1.setLayout(c);
		for(int i=0; i<names.length; i++)
		{
			p1.add(names[i], new Button(names[i]));
		}
		
		Panel p2 = new Panel();
		Button previous = new Button("Previous");
		previous.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				c.previous(p1);  //前一张
			}
		});
		
		Button next = new Button("Next");
		next.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				c.next(p1);  //下一张
			}
		});
		
		Button first = new Button("First");
		first.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				c.first(p1);   //第一张
			}
		});
		
		Button last = new Button("Last");
		last.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				c.last(p1);  //最后一张
			}
		});
		
		Button third = new Button("NO3");
		third.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				c.show(p1, "NO3");  //按名字“NO3”查找
			}
		});
		
		p2.add(previous);
		p2.add(next);
		p2.add(first);
		p2.add(last);
		p2.add(third);
		f.add(p2);
		f.add(p1, BorderLayout.NORTH);
		f.pack();
		f.setVisible(true);			
	}

	public static void main(String[] args) throws Exception {
		new Main().init();
	}	
}


2. BOX 容器

public class Main {
		
	private Frame f = new Frame("test CardLayout");
	private Box horizontal = Box.createHorizontalBox();  //水平Box容器
	private Box vertical = Box.createVerticalBox();	//竖直BOX容器	
	public void init()
	{
		horizontal.add(new Button("h_Button1"));   
		horizontal.add(Box.createHorizontalGlue());  //添加可拉伸的水平间距
		horizontal.add(new Button("h_Button2"));
		horizontal.add(Box.createHorizontalStrut(50));  //添加不可拉升的水平间距,固定宽度50
		
		vertical.add(new Button("v_Button1"));
		vertical.add(Box.createVerticalGlue());   //可拉伸
		vertical.add(new Button("v_Button2"));
		vertical.add(Box.createVerticalStrut(50));  //固定50,不可拉伸
		vertical.add(new Button("v_Button3"));
		
		f.add(horizontal, BorderLayout.NORTH);
		f.add(vertical);
		f.pack();
		f.setVisible(true);	
		
	}
	public static void main(String[] args) throws Exception {
		new Main().init();
	}	
}

 

3. 基本组件

public class Main {	        
	public static void main(String[] args) throws Exception {
		
		Frame f = new Frame("test CommonComponent");		
		Button okButton = new Button("OK");
		CheckboxGroup cbg = new CheckboxGroup();
		Checkbox maleCheckbox = new Checkbox("man", cbg, false);
		Checkbox femaleCheckbox = new Checkbox("woman", cbg, false);
		Checkbox marCheckbox = new Checkbox("married ?", false);
		Choice colorChooser = new Choice();
		colorChooser.add("Red");
		colorChooser.add("Green");
		colorChooser.add("Blue");		
		List colorList = new List(6, true);
		colorList.add("Red");
		colorList.add("Green");
		colorList.add("Blue");
		TextArea ta = new TextArea(5, 20);
		TextField name = new TextField(50);
		
		Panel bottom = new Panel();
		bottom.add(name);
		bottom.add(okButton);
		Panel checkPanel = new Panel();
		checkPanel.add(colorChooser);
		checkPanel.add(maleCheckbox);
		checkPanel.add(femaleCheckbox);
		checkPanel.add(marCheckbox);
		
		Box topLeft = Box.createVerticalBox();
		topLeft.add(ta);
		topLeft.add(checkPanel);
		Box top = Box.createHorizontalBox();
		top.add(topLeft);
		top.add(colorList);
		
		f.add(bottom, BorderLayout.SOUTH);
		f.add(top);
		f.pack();
		f.setVisible(true);		
	}	
}



4. Dialog 和普通窗口用法基本一样,只是可以有父控件,有两种模式“mode” "non-mode",在“mode”下子窗口在父窗口之上,并且父窗口不能再获得焦点,构造器如下:

  Dialog d1 = new Dialog(f, "mode", true);
  Dialog d2 = new Dialog(f, "non-mode", false);

Dialog有子类FileDialog 用打开和保存文件。。

public class Main {	        
	public static void main(String[] args) throws Exception {
		
		Frame f = new Frame("test Dialog");
		final FileDialog d1 = new FileDialog(f, "open file", FileDialog.LOAD);
		final FileDialog d2 = new FileDialog(f, "save file", FileDialog.SAVE);
		Button bt1 = new Button("open file");
		Button bt2 = new Button("save file");
		bt1.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				d1.setVisible(true);
				System.out.println(d1.getDirectory() + d1.getFile());
			}
		});
		bt2.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				d2.setVisible(true);
				System.out.println(d1.getDirectory() + d1.getFile());
			}
		});	
				
		f.add(bt1);
		f.add(bt2, BorderLayout.SOUTH);
		f.pack();
		f.setVisible(true);
					
	}	
}


5. 事件处理分类

 如上表 如果直接添加WindowListener 接口那将会有右边那么多方法需要实现,所以Java会有一个WindowAdapter适配器去实现WindowListener的各个方法,但是这些方法只是空方法,User只需要继承WindowAdapter实现自己需要的方法即可。 其他Listener也有其对应的Adapter.

posted @ 2013-11-01 08:04  今晚打酱油_  阅读(395)  评论(0编辑  收藏  举报