GUI
Animation:
x and y are coordinates.
public class GUI {
JFrame frame;
JButton button;
MyPanel myPanel;
int x = 60;
int y = 60;
public GUI() {
//Make a frame
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
// Make a button
button = new JButton("Click me");
button.addActionListener(new ButtonListener());
// Make a panel
myPanel = new MyPanel();
// Add the button to the frame
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.getContentPane().add(BorderLayout.CENTER, myPanel);
}
public void go() {
for(int i = 0; i < 130; i++) {
x++;
y++;
myPanel.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
GUI gui = new GUI();
gui.go();
}
class ButtonListener implements ActionListener {
private ButtonListener(){}
@Override
public void actionPerformed(ActionEvent e) {
button.setText("You've clicked!");
}
}
class MyPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
//Clear the whole panel
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
//Move the oval
g.setColor(Color.orange);
g.fillOval(x, y, 40, 40);
}
}
}
Call-back:
对于Button Event有兴趣的Object向Button注册(addActionListener). 这样Button就有了这个Object的reference。
当Button Event发生时,Button会调用注册列表里每个有兴趣Object的actionPerformed方法(Call-back)。 这其实是Observer Pattern。
Listener:
1. Implement the interface (event handling)。 ActionListener Interface, actionPerformed method.
2. register with the button
Event source:
1. Accept registrations (add listeners)
2. Get event
3. Call the listener's event-handling method.
Event Object:
Argument to the event call-back method, carrying data about the event.
Interface is the bridge between the button and listener.
Inner Class (内部类):
静态内部类static inner class (also called nested class)
成员内部类member inner class
局部内部类local inner class
匿名内部类anonymous inner class
1. 静态内部类:
静态内部类可以只能访问外部类的静态成员和静态方法
class Inner {
private static int staticVar = 4;
private static String staticMethod() {
return "static method";
}
// 静态内部类
static class StaticInner {
public void test() {
staticMethod();
System.out.println(staticVar);
}
}
}
public class InnerTest {
public static void main(String[] args) {
Inner.StaticInner innerStatic = new Inner.StaticInner();
innerStatic.test();
}
}
class MemberInner {
private int d = 1;
private int a = 2;
// 定义一个成员内部类
public class Inner2 {
private int a = 8;
public void doSomething() {
// 直接访问外部类对象
System.out.println(d);
// 访问外部类重名成员
System.out.println(MemberInner.this.a);
// 访问内部类成员
System.out.println(a);
}
}
}
public class MemberInnerClassTest {
public static void main(String[] args) {
// 创建成员内部类的对象
// 需要先创建外部类的实例
MemberInner.Inner2 inner = new MemberInner().new Inner2();
inner.doSomething();
}
}
</pre>3. 局部内部类</p><p>定义在方法里的类</p><p><pre name="code" class="java">class LocalInner {
int a = 1;
public void doSomething() {
int b = 2;
// 定义一个局部内部类
class Inner3 {
public void test() {
// 访问外部类成员
System.out.println(a);
// 访问方法局部变量
System.out.println(b);
}
}
// 创建局部内部类的实例并调用方法
new Inner3().test();
}
}
public class LocalInnerClassTest {
public static void main(String[] args) {
// 创建外部类对象
LocalInner inner = new LocalInner();
// 调用外部类的方法
inner.doSomething();
}
}
匿名内部类隐式地继承了一个父类或者实现了一个接口
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
//do something
}
});