JAVA学习之GUI(下篇)

一、内部类
写在一个类的内部,方法的外边
好处:
(1)可以方便的访问包装类的成员
(2)可以防止被其他类访问

何时使用:该类不需要被其他类访问

举例:

public class TFMath {
	public static void main(String[] args) {
		new TFFrame3().launchFrame();
	}
}

class TFFrame3 extends Frame{
	TextField num1,num2,num3;
	public void launchFrame(){
		num1 = new TextField(10);
		num2 = new TextField(10);
		num3 = new TextField(15);
		Label label = new Label("+");
		Button button = new Button("=");
		add(num1);
		add(label);
		add(num2);
		add(button);
		add(num3);
		setLayout(new FlowLayout());
		button.addActionListener(new TFActionLitener3());
		pack();
		setVisible(true);
		
	}
	
	class TFActionLitener3 implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			int n1 = Integer.parseInt(num1.getText());
			int n2 = Integer.parseInt(num2.getText());
			num3.setText(""+(n1+n2));
			
		}
	}
}


运行结果:


二、paint方法
Graphics类

每个Component都有paint(Graphics g)用于实现绘图目的,每次重画该Component都会自动调用paint方法

举例:

public class TestPaint {
	public static void main(String[] args) {
		new PaintFrame().launchFrame();
	}
}

class PaintFrame extends Frame{
	public void launchFrame(){
		setBounds(200, 200, 640, 680);
		setVisible(true);
	}
	
	public void paint(Graphics g){
		Color color = g.getColor();
		g.setColor(Color.red);
		g.fillOval(50, 50, 30, 30);
		g.setColor(Color.BLUE);
		g.fillRect(80, 80, 40, 40);
		g.setColor(color);
	}
}

运行结果:


三、鼠标事件适配器
MouseAdapter实现了MouseListener,可以作为MouseEvent的监听器,可以避免重写接口的全部方法
几种鼠标事件:mouseClicked、mouseEntered(鼠标移入)、mouseExited(鼠标移出)、mousePressed(鼠标按下)

mouseReleased(鼠标抬起)

public class MyMouseAdapter {
	public static void main(String[] args) {
		new MyFrame3("drawing………………");
	}
}

class MyFrame3 extends Frame{
	ArrayList points = null;
	 MyFrame3(String s) {
		super(s);
		points = new ArrayList();
		setVisible(true);
		setLayout(null);
		setBounds(300, 300, 400, 300);
		setBackground(new Color(204, 204, 255));
		addMouseListener(new Monitor3());
	}
	public void addPoint(Point p){
		points.add(p);
	}
	
	public void paint(Graphics g){
		Iterator iterator = points.iterator();
		while(iterator.hasNext()){
			Point point = (Point) iterator.next();
			g.setColor(Color.blue);
			g.fillOval(point.x, point.y, 10, 10);
		}
	}
	
}

class Monitor3 extends MouseAdapter{
	public void mousePressed(MouseEvent e){
		MyFrame3 f = (MyFrame3) e.getSource();
		f.addPoint(new Point(e.getX(), e.getY()));
		f.repaint();
	}
}

运行结果:


鼠标点一次,会在框内显示一个蓝点

四、Window事件
事件:WindowEvent   监听器:WindowListener   适配器:WindowAdapter 
一般关闭窗口选择closing方法,setVisible(false);  System.exit(0);
扩展:匿名内部类:
addWindowListener(new WindowAdapter() {//父类,可以是接口,一般逻辑简单时可以使用
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}

});

举例:

public class TestWindowClose {
	public static void main(String[] args) {
		new MyFrame55("frame closing.....");
	}
}

class MyFrame55 extends Frame{
	public MyFrame55(String s) {
		super(s);
		setVisible(true);
		setLayout(null);
		setBackground(new Color(204, 204, 255));
		setBounds(300, 300, 400, 300);
		//匿名内部类
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				setVisible(false);
				System.exit(0);
			}
		});
	}
	
//	class MyMonitor extends WindowAdapter{
//		@Override
//		public void windowClosing(WindowEvent e) {
//			setVisible(false);
//			System.exit(0);
//		}
//	}
}

运行结果:点击叉,关闭

五、键盘事件
一般有键盘按下,键盘抬起  

事件:KeyEvent   监听器:KeyListener   适配器:KeyAdapter 

举例:

public class TestKey {
	public static void main(String[] args) {
		new KeyFrame().launchFrame();;
	}
}

class KeyFrame extends Frame{
	public void launchFrame(){
		setVisible(true);
		setBounds(300, 300, 200, 200);
		setBackground(new Color(204, 204, 255));
		addKeyListener(new MyKeyMonitor());
	}
	
	class MyKeyMonitor extends KeyAdapter{
		public void keyPressed(KeyEvent e){
			int keyCode = e.getKeyCode();
			if(keyCode == KeyEvent.VK_UP){
				System.out.println("up");
			}
		}
	}
}

运行结果:

按下键盘向上键,控台显示up



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值