Hi All,
Here is a nice example of how to create your
own action components. Every component knows
how to run itself....
Regards,
- DL
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class ClosableFrame extends JFrame {
Container c = getContentPane();
public ClosableFrame() {
setSize(200,200);
addWindowListener(
new WindowHandler());
c.setLayout(new FlowLayout());
c.add(new RunButton("Left"){
public void run() {
leftButton();
}
});
c.add(new RunButton("Right"){
public void run() {
rightButton();
}
});
c.add(new RunButton("quit"){
public void run() {
closeWindow();
}
});
c.add(new RunCheckBox("Checking"){
public void run() {
checkBox();
}
});
setVisible(true);
}
public void checkBox() {
System.out.println("Checking!");
}
public void leftButton() {
System.out.println("Left button");
}
public void rightButton() {
System.out.println("Right button");
}
public void closeWindow() {
setVisible(false);
dispose();
System.exit(0);
}
class WindowHandler extends WindowAdapter {
public void windowClosing() {
closeWindow();
}
}
}
public class RunButton extends
JButton implements ActionListener, Runnable {
public RunButton(String label) {
super(label);
addActionListener(this);
}
public RunButton(String l, Icon i) {
super(l,i);
addActionListener(this);
}
public RunButton(Icon i) {
super(i);
addActionListener(this);
}
public RunButton() {
addActionListener(this);
}
public void run() {
System.out.println("Running the run button!");
}
public void actionPerformed(ActionEvent e) {
run();
}
}
public class RunCheckBox extends
JCheckBox implements ItemListener, Runnable {
public RunCheckBox(String label) {
super(label);
addItemListener(this);
}
public RunCheckBox(Icon i, boolean b) {
super(i,b);
addItemListener(this);
}
public RunCheckBox(String s, boolean b) {
super(s,b);
addItemListener(this);
}
public RunCheckBox(Icon i) {
super(i);
addItemListener(this);
}
public RunCheckBox() {
addItemListener(this);
}
public void run() {
System.out.println("Running the run RunCheckBox! isSelected="
+isSelected());
}
public void itemStateChanged(ItemEvent e) {
run();
}
}
public class TrivialApplication {
public static void main(String args[]) {
ClosableFrame cf = new ClosableFrame();
}
}
|