I need to take the following code and instead of having the ActionListener implemented by the JFrame class, I have to create a separate class. I think I know how to call the separate ActionListener class from the JFrame class, however, creating the separate ActionListener class is posing a challenge. Anyone??
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JFrame5 extends JFrame implements ActionListener {
Container c = this.getContentPane();
Toolkit kit = Toolkit.getDefaultToolkit();
ImageIcon img1 = new ImageIcon("c:\\Javagrl\\One.jpg");
ImageIcon img2 = new ImageIcon("c:\\Javagrl\\Two.JPG");
ImageIcon img3 = new ImageIcon("c:\\Javagrl\\Three.JPG");
JPanel top = new JPanel();
JPanel bottom = new JPanel();
JPanel center = new JPanel();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JLabel lab1 = new JLabel(img1);
JLabel lab2 = new JLabel(img2);
JLabel lab3 = new JLabel(img3);
JButton ImageOne = new JButton("Image One");
JButton ImageTwo = new JButton("Image Two");
JButton ImageThree = new JButton("Image Three");
CardLayout card = new CardLayout();
FlowLayout flow = new FlowLayout();
// remember that a frame has a default layout of border
public JFrame5() {
super();
}
public JFrame5(
String title) {
super(title);
}
public void createJFrame() {
this.setDefaultCloseOperation(3);
this.setSize(500, 500);
this.show();
}
public void makePanels() {
bottom.setLayout(flow);
flow.setHgap(10);
top.add(ImageOne);
top.add(ImageTwo);
top.add(ImageThree);
ImageOne.addActionListener(this);
ImageTwo.addActionListener(this);
ImageThree.addActionListener(this);
center.setLayout(card);
jp1.add(lab1);
jp2.add(lab2);
jp3.add(lab3);
center.add(jp1, "firstPanel");
center.add(jp2, "secondPanel");
center.add(jp3, "thirdPanel");
card.show(center, "firstPanel");
c.add("South", bottom);
c.add("Center", center);
c.add("North", top);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == ImageOne) {
card.first(center);
}
if (source == ImageTwo) {
card.next(center);
}
if (source == ImageThree) {
card.last(center);
}
}
}