---
title: "有关GUI编程的一些知识"
author: "chrisongs"
date: "2010-11-20T20:00:56Z"
tags: ["Java", "UP", "编程"]
categories: ["编程语言"]
slug: "gui"
draft: false
lastmod: "2010-12-01T19:32:56Z"
id: 818157
url: "https://chrisongs.iteye.com/blog/818157"
---

1\. \[color=darkred]Container\[/color] 和 \[color=darkred]Component\[/color]是AWT中的两个核心类。 2. 两种常用的Container: \[color=darkred]Window\[/color] 和 \[color=darkred]Panel\[/color]。 其中对于panel来说，其对象可以作为容纳其他Component对象，但不能独立存在，必须被添加到其他Container中（如window或Applet）。 3. 对于Frame，有如下小程序： \[code="java"]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); } } }\[/code] 结果为： \[img]http://dl.iteye.com/upload/attachment/352280/070161fe-606b-3501-befa-1b140f281a73.jpg\[/img] 并且可以点击X标志关闭！ 4. Frame的缺省布局管理器为BorderLayout，而Panel的缺省布局管理器为FlowLayout。当用户需要亲自设置组件位置时，应取消布局管理器：setLayout（null）。 5. 两个简单的TextField事件监听小程序： （1） 不包含内部类，“管家机制”传送一个类的引用到另一个类： \[code="java"]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)); } }\[/code] 程序运行结果为： \[img]http://dl.iteye.com/upload/attachment/352283/30eb4f47-81ed-38bd-bb4b-9231e2d7c488.jpg\[/img] （2）包含内部类的实现方法： \[code="java"]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)); } } }\[/code] 程序运行结果为： \[img]http://dl.iteye.com/upload/attachment/352285/88863afc-c9ae-3c92-8faf-f50710bafb11.jpg\[/img] 6. Paint方法，有关鼠标事件适配器的一个小程序： /* * 同时综合了内部类、MouseAdapter、WindowAdapter、ArrayList、泛型等知识点！ \*/ \[code="java"]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 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 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); } } }\[/code] 运行结果为： \[img]http://dl.iteye.com/upload/attachment/352287/9debda9d-f129-30f4-b21a-49f478065882.jpg\[/img] 7. 有关键盘事件的一个小程序： \[code="java"]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); } } }\[/code] 运行结果为： \[img]http://dl.iteye.com/upload/attachment/352293/02a28c5d-91f2-3ddb-89ac-a978702fb20a.jpg\[/img] 每按一次“↑”键，终端自动打印“UP！”