写在一个类的内部,方法的外边
好处:
(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