Java总结之GUI编程

【AWT】
AWT(Abstract Window Toolkit)包括了很多类和接口,用于Java Application的GUI(Graphics User Interface图形用户界面)编程。
GUI的各种元素(如:窗口,按钮,文本框等)有Java类来实现。
使用AWT所涉及的类一般在java.awt包及其子包中。
Container和Component是AWT中的两个核心类。
Component{Button,TextArea,Label,TextField,List;Container[Window(Frame,Dialog),Panel(Applet)]}
【Component & Container】
Java的图形用户界面的最基本组成部分是Component,Component类及其子类的对象用来描述以图形化的方式显示在屏幕上并能与用户进行交互的GUI元素,例如,一个按钮,一个标签等。
一般的Component对象不能独立地显示出来,必须将其“放在”某一个Container对象中在可以显示出来。
Container是Component子类,Container子类对象可以“容纳”别的Component对象。
Container对象可以使用方法add(..)向其中添加其他Component对象。
Container是Component的子类,因此Container对象也可以被当做Component对象添加到其他Container对象中。
有两种常用的Container:
  Window:其对象表示自由停泊的顶级窗口
  Panel:其对象可作为容纳其他Component对象,但不能独立存在,必须被添加到其他Container(如Window或Applet)
【Frame】
Frame是Window的子类,由Frame或其子类创建的对象为一个窗体。
Frame的常用构造方法:
  Frame()
  Frame(String s) 创建标题栏为字符串s的窗口。
setBounds(int x,int y,int width,int height)
setSize(int width,int height)
setLocation(int x,int y)
setBackground(Color c)
setVisible(boolean b) 设置是否可见
setTitle(String name) String getTitle()
setResizable(boolean b) 设置是否可以调整大小
【布局管理器】
Java语言中,提供了布局管理器类的对象可以管理
  管理Component在Container中的布局,不比直接设置Component位置和大小。
  每个Container都有一个布局管理器对象,当容器需要对某个组件进行定位或判断其大小尺寸时,就会调用其对应的布局管理器,调用Container的setLayout方法改变其布局管理器对象。
Awt提供了5种布局管理器类:
  FlowLayout
  BorderLayout
  GridLayout
  CardLayout
  GridBagLayout
【FlowLayout布局管理器】
FlowLayout是Panel类的默认布局管理器。
 FlowLayout布局管理器对组建逐行定位,行内从左到右,一行排满后换行。
 不改变组建的大小,按组件原有尺寸显示组件,可设置不同的组建举例,航局以及对齐方式。
FlowLayout布局管理器默认的对齐方式是剧中。
【FlowLayout的构造方法】
new FlowLayout(FlowLayout.RIGHT,20,40);
 右对齐,组件之间水平间距20个像素,垂直间距40个像素。
new FlowLayout(FlowLayout.LEFT);
 左对齐,水平和垂直间距缺省值(5)。
new FlowLayout();
 使用缺省的居中对齐方式,水平和垂直间距为缺省值(5)。
范例:
import java.awt.*;
public class TestFlowLayout {
	public static void main(String[] args) {
		Frame f = new Frame("Flow Layout");
		Button button1 = new Button("OK");
		Button button2 = new Button("Open");
		Button button3 = new Button("Close");
		f.setLayout(new FlowLayout());
		f.add(button1);
		f.add(button2);
		f.add(button3);
		f.setSize(100,100);
		f.setVisible(true);
	}
}


【BorderLayout布局管理器】
BorderLayout是Frame类的默认布局管理器。
BorderLayout将整个容器的布局划分成:东(EAST)西(WEST)南(SOUTH)北(NORTH)中(CENTER)五个区域,组建只能被添加到指定的区域。
如不指定组建的假如不为,则默认加入到CENTER区。
每个区域只能加入一个组件,如加入多个,则先前加入的会被覆盖。
范例:
import java.awt.*;
public class TestBorderLayout {
	public static void main(String[] args) {
		Frame f;
		f = new Frame("Border Layout");
		Button bn = new Button("BN");
		Button bs = new Button("BS");
		Button bw = new Button("BW");
		Button be = new Button("BE");
		Button bc = new Button("BC");
		f.add(bn , "North");
		f.add(bs , "South");
		f.add(bw , "West");
		f.add(be , "East");
		f.add(bc , "Center");
		//也可使用下述语句
		/*
		f.add(bn , BorderLayout.NORTH);
		f.add(bs , BorderLayout.SOUTH);
		f.add(bw , BorderLayout.WEST);
		f.add(be , BorderLayout.EAST);
		f.add(bc , BorderLayout.CENTER);
		*/
		f.setSize(200,200);
		f.setVisible(true);
	}
}


【GridLayout布局管理器】
GridLayout型布局管理器将空间划分成规则的矩形网络,每个单元格区域大小相等。组建被添加到每个单元格中,先从左到右填满一行后换行,再从上到下。
在GridLayout构造方法中指定分割的行数和列数,如:GridLayout(3,4)
【布局管理器总结】
Frame是一个顶级窗口,Frame的缺省布局管理器为BorderLayout
Panel无法单独显示,必须添加到某个容器中。
Panel的缺省布局管理器为FlowLayout。
当把Panel作为一个组件添加到某个容器中后,该Panel任然可以有自己的布局管理器。
使用布局管理器时,布局管理器负责各个组件的大小和位置,因此用户无法在这种情况下设置组件大小和位置属性,如果试图使用Java语言提供的setLocation(),setSize(),setBounds()等方法,则都会被布局管理器覆盖。
如果用户确实需要亲自设置组件大小或位置,则应取消该容器的布局管理器,方法为:
 setLayout(null)
【ActionEvent】
范例:
import java.awt.*;
import java.awt.event.*;
public class TestActionEvent {
	public static void main(String[] args) {
		Frame f = new Frame("Test");
		Button b = new Button("Press me!");
		Monitor bh = new Monitor();
		b.addActionListener(bh);
		f.add(b,BorderLayout.CENTER);
		f.pack();
		f.setVisible(true);
	}
}
class Monitor implements ActionListener {
	public void actionPerformed(ActionEvent e) {
		System.out.println("a button has been pressed");
	}
}


【TextField类】
java.awt.TextField类用来创建文本框对象。
TextField有如下常用方法:
 TextField()
 TextField(int columns)
 TextField(String text)
 TextField(String text,int columns)
 public void setText(String t)
 public String getText()
 public void setEchoChar(char c) 设置回显字符
 public void setEditable(boolean b)
 public boolean isEditable()
 public void setBackground(Color c)
 public void select(int selectionStart,int selectionEnd)
 public void selectAll()
 public void addActionListener(ActionListener l) 添加动作监听器。
【TextField事件监听】
TextField对象可能发生Action(光标在文本框内敲回车)事件。与改时间对应的事件类是java.awt.event.ActionEvent。
用来处理ActionEvent事件是实现了java.awt.event.ActionListener接口类的对象。ActionListener接口定义有方法:
 public void actionPerformed(ActionEvent e)
实现该接口的类要在该方法中添加处理该事件(Action)的语句。
使用addActionListener(ActionListener l)方法为TextField对象注册一个ActionListener对象,当TextField对象发生Action时间时,会生成一个ActionEvent对象,该对象作为参数传递给ActionListener对象的actionPerformer方法在方法中可以获取该对象的信息,并做相应的处理。
TFActionEvent.java //TextField事件监听初步
import java.awt.*;
import java.awt.event.*;
public class TFActionEvent {
	public static void main(String[] args) {
		new TFFrame();
	}
}
class TFFrame extends Frame {
	TFFrame() {
		TextField tf = new TextField();
		add(tf);
		tf.addActionListener(new TFActionListener());
		pack();
		setVisible(true);
	}
}
class TFActionListener implements ActionListener {
	public void actionPerformed(ActionEvent e) {
		TextField tf = (TextField)e.getSource();
		System.out.println(tf.getText());
		tf.setText("");
	}
}
TFPassword.java //密码
import java.awt.*;
import java.awt.event.*;
public class TFPassword {
	public static void main(String[] args) {
		new TFFrame2();
	}
}
class TFFrame2 extends Frame {
	TFFrame2() {
		TextField tf = new TextField();
		add(tf);
		tf.addActionListener(new TFActionListener2());
		tf.setEchoChar('*');
		pack();
		setVisible(true);
	}
}
class TFActionListener2 implements ActionListener {
	public void actionPerformed(ActionEvent e) {
		TextField tf = (TextField)e.getSource();
		System.out.println(tf.getText());
		tf.setText("");
	}
}


【持有对方引用】
TFMath.java //数学运算 内部类
import java.awt.*;
import java.awt.event.*;
public class TFMath {
	public static void main(String[] args) {
		new TFFrame().launchFrame();
	}
}
class TFFrame extends Frame {
	TextField[] num = new TextField[3];
	public void launchFrame() {
		for(int i=0;i<3;i++) 
			num[i] = new TextField(10);
			Label lblPlus = new Label("+");
			Button btnEqual = new Button("=");
			btnEqual.addActionListener(new MyMonitor(this));
			setLayout(new FlowLayout());
			add(num[0]); add(lblPlus);
			add(num[1]); add(btnEqual);
			add(num[2]);
			pack();
			setVisible(true);
	}
}
class MyMonitor implements ActionListener {
	TFFrame tf = null;
	public MyMonitor(TFFrame tf) {this.tf = tf;}
	public void actionPerformed(ActionEvent e) {
		int n0 = Integer.parseInt(tf.num[0].getText());
		int n1 = Integer.parseInt(tf.num[1].getText());
		tf.num[2].setText(""+(n0+n1));
	}
}


【内部类版本】(改变上述TFMath.java)
import java.awt.*;
import java.awt.event.*;
public class TFMath {
	public static void main(String[] args) {
		new TFFrame().launchFrame();
	}
}
class TFFrame extends Frame {
	TextField[] num = new TextField[3];
	public void launchFrame() {
		for(int i=0;i<3;i++) 
			num[i] = new TextField(10);
			Label lblPlus = new Label("+");
			Button btnEqual = new Button("=");
			btnEqual.addActionListener(new MyMonitor());
			setLayout(new FlowLayout());
			add(num[0]); add(lblPlus);
			add(num[1]); add(btnEqual);
			add(num[2]);
			pack();
			setVisible(true);
	}
	class MyMonitor implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int n0 = Integer.parseInt(num[0].getText());
			int n1 = Integer.parseInt(num[1].getText());
			num[2].setText(""+(n0+n1));
		}
	}
}


【内部类】
好处:
  可以方便的访问包装类的成员
  可以更清楚的组织逻辑,防止不应该被其他类访问的类进行访问。
何时使用:
  该类不允许或不需要其他类进行访问时。
【Graphics类】
每个Component都有一个paint(Graphics g)用于实现绘图目的,每次重画该Component时都自动调用paint方法。
Graphics类中提供了许多绘图方法,如:
 drawRect(int x,int y,int width,int height)
 fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)等
范例:
import java.awt.*;
public class TestPaint {
	public static void main(String[] args) {
		new PaintFrame().launchFrame();
	}
}
class PaintFrame extends Frame {
	public void launchFrame() {
		setBounds(200,200,640,480);
		setVisible(true);
	}
	public void paint(Graphics g) {
		Color c = g.getColor();
		g.setColor(Color.red);
		g.fillOval(50,50,30,30);
		g.setColor(Color.green);
		g.fillRect(80,80,40,40);
		g.setColor(c);
	}
}


【鼠标事件适配器】
抽象类java.awt.event.MouseAdapter实现了MouseListener接口,可以使用妻子类作为MouseEvent的监听器,只要重写其相应的方法即可。
对于其他的监听器,也有对应的适配器。
使用适配器可以避免监听器类定义没有必要的空方法。
repaint() = 先update() - 再paint(); //双缓冲
范例:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class MyMouseAdapter {
	public static void main(String[] args) {
		new MyFrame("drawing...");
	}
}
class MyFrame extends Frame {
	ArrayList points = null;
	MyFrame(String s) {
		super(s);
		points = new ArrayList();
		setLayout(null);
		setBounds(300,300,400,300);
		this.setBackground(new Color(204,204,255));
		setVisible(true);
		this.addMouseListener(new Monitor());
	}
	public void paint(Graphics g) {
		Iterator i = points.iterator();
		while(i.hasNext()) {
			Point p = (Point)i.next();
			g.setColor(Color.BLUE);
			g.fillOval(p.x,p.y,10,10);
		}
	}
	public void addPoint(Point p) {
		points.add(p);
	}
} 
class Monitor extends MouseAdapter {
	public void mousePressed(MouseEvent e) {
		MyFrame f = (MyFrame)e.getSource();
		f.addPoint(new Point(e.getX(),e.getY()));
		f.repaint();
	}
}


【Window事件】
Window时间所对应的事件类为WindowEvent,所对应的事件监听接口为WindowListener。
WindowListener定义的方法有:
 public void windowOpened(WindowEvent e)
 public void windowClosing(WindowEvent e)
 public void windowClosed(WindowEvent e)
 public void windowIconfied(WindowEvent e)
 public void windowDeiconified(WindowEvent e)
 public void windowActivated(WindowEvent e)
 public void windowDeactivated(WindowEvent e)
与WindowListener对应的适配器为WindowAdapter。
【KeyEvent】(匿名类用法)
范例:
import java.awt.*;
import java.awt.event.*;
public class TestAnoymous {
	Frame f = new Frame("Test");
	TextField tf = new TextField(10);
	Button b1 = new Button("Start");
	public TestAnoymous() {
		f.add(b1 , "North");
		f.add(tf , "South");
		b1.addActionListener(new ActionListener() {
			private int i;
			public void actionPerformed(ActionEvent e) {
				tf.setText(e.getActionCommand() + ++i);
			}
		});
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		f.pack();
		f.setVisible(true);
	}
	public static void main(String[] args) {
		new TestAnoymous();
	}
}


你可能感兴趣的:(java)