---
title: "swing 监听 匿名、内部、外部类 第六讲091212A"
author: "atomcow"
date: "2009-12-17T10:59:20Z"
tags: []
categories: ["企业架构"]
slug: "swing-091212a"
draft: false
lastmod: "2009-12-17T11:07:54Z"
id: 550326
url: "https://atomcow.iteye.com/blog/550326"
---

```java
import javax.swing.JFrame;
public class TestWin extends JFrame {//继承JFrame。swing类的组件前面大都有个字母“J”，和AWT类区别。	
	public static void main(String[] args) {
		TestWin w = new TestWin();
		TestWin q = new TestWin();
		w.setTitle("title1");
		w.setBounds(100, 100, 180, 300);//设置出现的指标位置X,Y，设置长宽H,W
		w.setVisible(true);
		q.setTitle("title2");//设置标题
		q.setBounds(500, 100, 180, 300);
		q.setVisible(true); //最后要设置为可见，方可显示
	}
}
```
输出：
 

----------------------------------------------------------------------


```java
import javax.swing.JFrame;
public class MyWin extends JFrame {
	MyWin(String tit, int x, int y) {
		super(tit);
		this.setBounds(x, y, 100, 100);
	}
	void Run() {
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new MyWin("qwqewe",500,500).Run();
		new MyWin("zqwqwe",500,0).Run();
		new MyWin("xqwqwe",0,500).Run();
	}
}
 
```

----------------------------------------------------------------------

```java

import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class TestFlowLayout extends JFrame {
//	FlowLayout fl = new FlowLayout();//种布局格式为普通的布局格式，默认为逐行居中显示	
GridLayout gl=new GridLayout(4,5,5,5);//网格形布局

	TestFlowLayout() {
//		this.setLayout(fl);
 		this.setLayout(gl);
		for (int i = 0; i < 10; i++) {
			this.add(new JButton("333"));			
		}		
		
		this.setBounds(100, 100, 300, 300);
		this.setVisible(true);
	}

	public static void main(String[] args) {
		new TestFlowLayout();
	}
}
```

 

----------------------------------------------------------------------



```java
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class TestBorder extends JFrame {
	JButton bt1 = new JButton("N");
	JButton bt2 = new JButton("S");
	JButton bt3 = new JButton("W");
	JButton bt4 = new JButton("E");
	JButton bt5 = new JButton("C");

	TestBorder() {
		BorderLayout bl = new BorderLayout();
		this.setLayout(bl); //这种方式设置布局
		this.add(bt1, BorderLayout.NORTH); //设置显示的位置，一般用JPanel组件填充
		this.add(bt2, BorderLayout.SOUTH);
		this.add(bt3, BorderLayout.WEST);
		this.add(bt4, BorderLayout.EAST);
		this.add(bt5, BorderLayout.CENTER);
		this.setVisible(true);
		this.setBounds(100, 100, 300, 300);
	}
	public static void main(String[] args) {
		new TestBorder();
	}
}
```
 

----------------------------------------------------------------------


```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.TextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CalcDemo extends JFrame {//制作一个计算器的界面	
	BorderLayout bLayout = new BorderLayout();
	GridLayout gLayout = new GridLayout(4, 2, 5, 5);
	JPanel p2 = new JPanel();
	TextField tf = new TextField();

	CalcDemo() {
		for (int i = 0; i < 10; i++) {
			p2.add(new JButton("" + i));
		}		
		p2.setBackground(Color.BLUE);
		p2.setLayout(gLayout);
		this.setLayout(bLayout);
		this.add(tf,BorderLayout.NORTH);
		this.add(p2,BorderLayout.CENTER);		
		this.pack();//自动适应大小
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new CalcDemo();
	}
}
```

 

----------------------------------------------------------------------

```java
import java.awt.FlowLayout;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Test extends JFrame {
	private static final Vector<String> String = null;
	JRadioButton rbt1 = new JRadioButton("A");//单选框
	JRadioButton rbt2 = new JRadioButton("B");
	ButtonGroup bg = new ButtonGroup();
	JLabel lab = new JLabel("aaa");
	JComboBox ta = new JComboBox(this.VFun());//下拉框的内容用一个vector集合表达出来
	FlowLayout f = new FlowLayout();
	JList list = new JList(this.VFun());//一个LIST同样用一个vector集合表达
	JLabel jl =new JLabel();
	JPanel img =new JPanel();

	Test() {
		bg.add(rbt1);//单选框成组（ButtonGroup），然后再把按钮添加到舞台上
		bg.add(rbt2);
		this.add(rbt1);
		this.add(rbt2);
		this.setBounds(100, 100, 300, 150);
		this.add(ta);
		this.add(lab);
		this.add(list);
		this.setLayout(f);
		this.setVisible(true);
	}

	public static void main(String[] args) {
		new Test();

	}

	Vector<String> VFun() {
		Vector<String> v = new Vector<String>();
		v.add("1");
		v.add("2");
		v.add("3");
		return v;

	}
}
```
 

----------------------------------------------------------------------


```java
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Testlisten extends JFrame {
	JButton bt1 = new JButton("bt1");
	JButton bt2 = new JButton("bt2");
	FlowLayout f = new FlowLayout();

	public Testlisten() {
		this.setLayout(f);
		this.add(bt1);
		this.add(bt2);
		this.pack();
		this.setVisible(true);
		bt1.addActionListener(new ActionListener() {//匿名类实现ActionListener接口，实现actionPerformed方法，进行监听
			public void actionPerformed(ActionEvent e) {
				System.out.println("bt1 action");
			}
		});
		bt2.addActionListener(new Action2());//用外部类Action2来实现ActionListener接口
	}

	public static void main(String[] args) {
		new Testlisten();
	}

}
class Action2 implements ActionListener{
 	public void actionPerformed(ActionEvent e) {
 		System.out.println("bt2 action");			
	}
	
}
```
 

----------------------------------------------------------------------

```java

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
/**
 * 计算器的监听使用三种实现方法，
 * 第一个用匿名类实现，适用于只使用一次的类
 * 第二个用外部类实现，故需要构造方法传递参数
 * 第三个用内部类实现，很好的对前两种进行了综合，不需要传递参数。
 * */
public class AddTest extends JFrame {
	JTextField t1 = new JTextField(5);
	JTextField t2 = new JTextField(5);
	JTextField t3 = new JTextField(5);
	JLabel lab = new JLabel("+");
	JButton bt = new JButton("=");
	FlowLayout f = new FlowLayout();

	public AddTest() {
		this.setLayout(f);		
		this.add(t1);
		this.add(lab);
		this.add(t2);
		this.add(bt);
		this.add(t3);
		this.pack();
		this.setVisible(true);
		bt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int a=Integer.parseInt(t2.getText())+Integer.parseInt(t1.getText());
				t3.setText(a+"");
			}
		});
	}

	public static void main(String[] args) {
		new AddTest();
	}
}
```

 

用外部类实现：

```java
public class AddTest2 extends JFrame {
	JTextField t1 = new JTextField(5);
	JTextField t2 = new JTextField(5);
	JTextField t3 = new JTextField(5);
	JLabel lab = new JLabel("+");
	JButton bt = new JButton("=");
	FlowLayout f = new FlowLayout();

	public AddTest2() {
		this.setLayout(f);
		this.add(t1);
		this.add(lab);
		this.add(t2);
		this.add(bt);
		this.add(t3);
		this.pack();
		this.setVisible(true);
		bt.addActionListener(new Action(t1, t2, t3));

	}

	public static void main(String[] args) {
		new AddTest2();
	}
}

class Action implements ActionListener {
	JTextField j1;
	JTextField j2;
	JTextField j3;

	public Action(JTextField t1, JTextField t2, JTextField t3) {
		this.j1 = t1;
		this.j2 = t2;
		this.j3 = t3;
	}

	public void actionPerformed(ActionEvent e) {
		int a = Integer.parseInt(j2.getText()) + Integer.parseInt(j1.getText());
		j3.setText(a + "");

	}
}
```

用内部类实现：

```java
public class AddTest3 extends JFrame {
	JTextField t1 = new JTextField(5);
	JTextField t2 = new JTextField(5);
	JTextField t3 = new JTextField(5);
	JLabel lab = new JLabel("+");
	JButton bt = new JButton("=");
	FlowLayout f = new FlowLayout();

	public AddTest3() {
		this.setLayout(f);
		this.add(t1);
		this.add(lab);
		this.add(t2);
		this.add(bt);
		this.add(t3);
		this.pack();
		this.setVisible(true);
		bt.addActionListener(new Action());
	}

	public static void main(String[] args) {
		new AddTest3();
	}
	class Action implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int a = Integer.parseInt(t2.getText()) + Integer.parseInt(t1.getText());
			t3.setText(a + "");

		}
	}
}
```

----------------------------------------------------------------------


```java
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;

public class TestKeybord extends JFrame { //按什么键，输出键所对应的编码	
	public TestKeybord() {		 
		this.setVisible(true);		 
		this.addKeyListener(new KeyAdapter() {// 键盘监听，实现KeyListener的适配器。
					public void keyPressed(KeyEvent e) {
						System.out.println(e.getKeyCode());
					}
				});
	}
	public static void main(String[] args) {
		new TestKeybord();
	}
}
```

----------------------------------------------------------------------



```java
import java.awt.Frame;
import java.awt.Graphics;

public class TestGrap extends Frame {
	public static void main(String[] args) {
		new TestGrap();
	}

	public void paint(Graphics g) {
		 g.drawLine(0, 0, 200, 200);
		 g.drawString("hahahaha", 100, 100);
		for (int i = 0; i < 10; i++) {
			g.drawLine(10, 10, i*5, i*5);
			System.out.println("ss");
		}
	}

	public TestGrap() {
		this.setBounds(200, 200, 200, 200);
		this.setVisible(true);
	}
}
```


 

----------------------------------------------------------------------


```java
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;

public class TestArc extends Frame {
//	Map m=hashMap();
	
	public void paint(Graphics g) {
		g.setColor(Color.magenta); //设置“画笔”颜色
		g.fillArc(50, 50, 150, 150, 90, 90); //开始的X，Y坐标，长，宽，开始的角度位置，需要画多少度的弧线
		g.setColor(Color.pink);
		g.fillArc(50, 50, 150, 150, 180, 120);
		super.paint(g);
	}

	public TestArc() {
		this.setBounds(0, 0, 400, 400);
		this.setVisible(true);
	}

	public static void main(String[] args) {
		new TestArc();

	}
 
```

输出结果：
![](http://dl.iteye.com/upload/attachment/181587/609a53c4-cca3-3202-946a-3ec30a65b3e8.png)