I want to show program that 3 button in bottom that if we click red button, the panel change color become red, etc.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LatihanEvent2 implements ActionListener {
private JButton buttonRed = new JButton ("Red");
private JButton buttonGreen = new JButton ("Green");
private JButton buttonBlue = new JButton ("Blue");
public LatihanEvent2() {
JFrame frame = new JFrame("Contoh Event");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.add(buttonRed, BorderLayout.WEST);
panel.add(buttonGreen, BorderLayout.CENTER);
panel.add(buttonBlue, BorderLayout.EAST);
//Inner Class
ListenerRed clickListener = new ListenerRed();
buttonRed.addActionListener(clickListener);
//Anonymous Class
buttonGreen.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent e) {
buttonGreen.setBackground(Color.GREEN);
}
});
//Derived Class
buttonBlue.addActionListener(this); //Step 2
frame.add(panel, BorderLayout.SOUTH);
frame.setVisible(true);
frame.show();
}
public static void main (String[] args) {
new LatihanEvent2();
}
//Inner Class
class ListenerRed implements ActionListener {
public void actionPerformed (ActionEvent e) {
buttonRed.setBackground(Color.RED);
}
}
//Derived Class
public void actionPerformed (ActionEvent e) {
buttonBlue.setBackground(Color.BLUE);
}
}
In my coding there was 3 method. Inner class, Anonymous Class, and Derived Class. How to make the panel change color background with this different method? Help me please