`
chrisongs
  • 浏览: 26010 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
社区版块
存档分类
最新评论

有关GUI编程的一些知识

阅读更多
1. ContainerComponent是AWT中的两个核心类。

2. 两种常用的Container:  WindowPanel。 其中对于panel来说,其对象可以作为容纳其他Component对象,但不能独立存在,必须被添加到其他Container中(如window或Applet)。

3. 对于Frame,有如下小程序:

package com.java.TestFrame;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestFrame2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyFrame f1 = new MyFrame(100, 100, 200, 200, Color.blue);
		MyFrame f2 = new MyFrame(300, 100, 200, 200, Color.red);
		MyFrame f3 = new MyFrame(100, 300, 200, 200, Color.cyan);
		MyFrame f4 = new MyFrame(300, 300, 200, 200, Color.white);
		f1.launchFrame();
		f2.launchFrame();
		f3.launchFrame();
		f4.launchFrame();
	}

}

class MyFrame extends Frame {
	static int id = 0;
	MyFrame(int x, int y, int w, int h, Color color) {
		super("MyFrame" + (++id));
		setBounds(x, y, w, h);
		setBackground(color);
		setLayout(null);
		setVisible(true);
	}
	
	public void launchFrame() {
		MyWindowMonitor mwm = new MyWindowMonitor();
		this.addWindowListener(mwm);
	}
	
	private class MyWindowMonitor extends WindowAdapter {

		@Override
		public void windowClosing(WindowEvent e) {
			// TODO Auto-generated method stub
			setVisible(false);
			System.exit(-1);
		}
		
	}
}


结果为:

并且可以点击X标志关闭!

4. Frame的缺省布局管理器为BorderLayout,而Panel的缺省布局管理器为FlowLayout。当用户需要亲自设置组件位置时,应取消布局管理器:setLayout(null)。

5. 两个简单的TextField事件监听小程序:
(1) 不包含内部类,“管家机制”传送一个类的引用到另一个类:

package com.java.TFMath;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TFMath {

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

}

class MyFrame extends Frame {
	
	TextField num1,num2,num3;
	
	public void launchFrame() {
		 num1 = new TextField(10);
		 num2 = new TextField(10);
		 num3 = new TextField(15);
		Label l = new Label("+");
		Button b = new Button("=");
		
		setLayout(new FlowLayout());
		add(num1);
		add(l);
		add(num2);
		add(b);
		add(num3);
		pack();
		setVisible(true);

		MyMonitor mm = new MyMonitor(this);
		b.addActionListener(mm);
	}
}

class MyMonitor implements ActionListener {
	
	MyFrame mf = null;
	MyMonitor(MyFrame mf) {
		this.mf = mf;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		int n1 = Integer.parseInt(mf.num1.getText());
		int n2 = Integer.parseInt(mf.num2.getText());
		
		mf.num3.setText("" + (n1+n2));
//		mf.num3.setText(Integer.toString(n1+n2));
	}
	
}


程序运行结果为:



(2)包含内部类的实现方法:

package com.java.TestTFMath2;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestTFMath2 {

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

}

class MyFrame extends Frame {
	
	TextField num1;
	TextField num2;
	TextField num3;
	
	public void launchFrame() {
		num1 = new TextField(10);
		num2 = new TextField(10);
		num3 = new TextField(15);
		Label l = new Label("*");
		Button b = new Button("=");
		setLayout(new FlowLayout());
		add(num1);
		add(l);
		add(num2);
		add(b);
		add(num3);
		pack();
		setVisible(true);
		
		b.addActionListener(new MyMonitor());
	}
	
	private class MyMonitor implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			int n1 = Integer.parseInt(num1.getText());
			int n2 = Integer.parseInt(num2.getText());
			num3.setText(Integer.toString(n1*n2));
		}
		
	} 
}


程序运行结果为:



6. Paint方法,有关鼠标事件适配器的一个小程序:
/*
* 同时综合了内部类、MouseAdapter、WindowAdapter、ArrayList、泛型等知识点!
*/

package com.java.MyMouseAdapter;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;

public class MyMouseAdapter {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MyFrame("drawing...").launchFrame();
	}

}

class MyFrame extends Frame {
	MyFrame(String name) {
		super(name);
	}
	
	ArrayList <Point> points = null;
	
	public void launchFrame() {
		points = new ArrayList();
		setLayout(null);
		setBounds(300, 300, 400, 300);
		setBackground(Color.WHITE);
		setVisible(true);
		
		this.addMouseListener(new Monitor());
		this.addWindowListener(new MyWindowMonitor());
		
	}
	
	public void paint(Graphics g) {
		Iterator <Point> i = points.iterator();
		while (i.hasNext()) {
			Point p = i.next();
			Color c = g.getColor();
			g.setColor(Color.BLACK);
			g.fillRect(p.x, p.y, 10, 10);
			g.setColor(c);
		}
	}
	
	public void addPoint(Point p) {
		points.add(p);
	}
	
	class Monitor extends MouseAdapter {

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			addPoint(new Point(e.getX(),e.getY()));
			repaint();
		}
		
	}
	
	class MyWindowMonitor extends WindowAdapter {

		@Override
		public void windowClosing(WindowEvent e) {
			// TODO Auto-generated method stub
			setVisible(false);
			System.exit(-1);
		}
		
	}
}


运行结果为:



7. 有关键盘事件的一个小程序:
package com.java.MyKeyAdapter2;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyKeyAdapter2 {

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

}

class MyFrame extends Frame {
	public void launchFrame() {
		this.setBounds(100, 100, 200, 150);
		setLayout(null);
		setBackground(Color.WHITE);
		setVisible(true);
		
		this.addWindowListener(new MyWindowMonitor());
		this.addKeyListener(new MyKeyMonitor());
	}
	
	private class MyKeyMonitor extends KeyAdapter {

		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub
			int keyCode = e.getKeyCode();
			if (keyCode == KeyEvent.VK_UP) {
				System.out.println("UP!");
			}
		}
		
	}
	
	private class MyWindowMonitor extends WindowAdapter {

		@Override
		public void windowClosing(WindowEvent e) {
			// TODO Auto-generated method stub
			setVisible(false);
			System.exit(-1);
		}
		
	}
}


运行结果为:


每按一次“↑”键,终端自动打印“UP!”
  • 大小: 52.7 KB
  • 大小: 13.5 KB
  • 大小: 16.1 KB
  • 大小: 11.5 KB
  • 大小: 16.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics