import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
public class Example implements ActionListener {
public static Class c;
public Example() {
c = new Class1();
}
public static void main(String[] args) {
new Example();
}
public void actionPerformed(ActionEvent e) {
int r = (int) (Math.random() * 3);
switch (r) {
case 1: {
c.dispose();
c = null;
c = new Class1();
break;
}
case 2: {
c.dispose();
c = null;
c = new Class2();
break;
}
default: {
c.dispose();
c = null;
c = new Class3();
}
}
}
abstract class Class {
public abstract void dispose();
}
class Class1 extends Class {
private JWindow w = new JWindow();
private JLabel l = new JLabel(" ");
private JButton b = new JButton("Change");
private Calendar cal = new GregorianCalendar();
private Image img = Toolkit.getDefaultToolkit().getImage("b1.jpg");
public Class1() {
w.setAlwaysOnTop(true);
w.setLocationByPlatform(true);
w.add(l, BorderLayout.NORTH);
w.add(new JLabel(new ImageIcon(img)), BorderLayout.CENTER);
w.add(b, BorderLayout.SOUTH);
w.getContentPane().setBackground(Color.BLUE);
l.setFont(new Font(l.getFont().getFamily(), Font.PLAIN, 20));
b.addActionListener(Example.this);
new Thread() {
public void run() {
while (true) {
cal.setTimeInMillis(System.currentTimeMillis());
l.setText(cal.getTime().toString());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
w.pack();
}
}
}.start();
w.setVisible(true);
}
public void dispose() {
w.dispose();
b.removeActionListener(Example.this);
img.flush();
img = null;
}
}
class Class2 extends Class {
private JWindow w = new JWindow();
private JLabel l = new JLabel(" ");
private JButton b = new JButton("Change");
private Calendar cal = new GregorianCalendar();
private Image img = Toolkit.getDefaultToolkit().getImage("b2.jpg");
public Class2() {
w.setAlwaysOnTop(true);
w.setLocationByPlatform(true);
w.add(l, BorderLayout.NORTH);
w.add(new JLabel(new ImageIcon(img)), BorderLayout.CENTER);
w.add(b, BorderLayout.SOUTH);
w.getContentPane().setBackground(Color.RED);
l.setFont(new Font(l.getFont().getFamily(), Font.PLAIN, 30));
b.addActionListener(Example.this);
new Thread() {
public void run() {
while (true) {
cal.setTimeInMillis(System.currentTimeMillis());
l.setText(cal.getTime().toString());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
w.pack();
}
}
}.start();
w.setVisible(true);
}
public void dispose() {
w.dispose();
b.removeActionListener(Example.this);
img.flush();
img = null;
}
}
class Class3 extends Class {
private JWindow w = new JWindow();
private JLabel l = new JLabel(" ");
private JButton b = new JButton("Change");
private Calendar cal = new GregorianCalendar();
private Image img = Toolkit.getDefaultToolkit().getImage("b3.jpg");
public Class3() {
w.setAlwaysOnTop(true);
w.setLocationByPlatform(true);
w.add(l, BorderLayout.NORTH);
w.add(new JLabel(new ImageIcon(img)), BorderLayout.CENTER);
w.add(b, BorderLayout.SOUTH);
w.getContentPane().setBackground(Color.YELLOW);
l.setFont(new Font(l.getFont().getFamily(), Font.PLAIN, 50));
b.addActionListener(Example.this);
new Thread() {
public void run() {
while (true) {
cal.setTimeInMillis(System.currentTimeMillis());
l.setText(cal.getTime().toString());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
w.pack();
}
}
}.start();
w.setVisible(true);
}
public void dispose() {
w.dispose();
b.removeActionListener(Example.this);
img.flush();
img = null;
}
}
}