1

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
enjeru
  • 55
  • 1
  • 9
  • 1
    The problem statement implies to change the color of a **panel** yet the actions in the listeners are setting the BG color of **the button they are attached to.** Is that intentional? Note also that if it is required to change the color of the panel, it will be implied necessary to make some of that panel visible to the user. The easiest way to do that is to add an `EmptyBorder`. – Andrew Thompson Apr 12 '15 at 04:37
  • @AndrewThompson how to add `EmptyBorder` please? ._. – enjeru Apr 12 '15 at 04:44
  • `panel.setBorder(new EmptyBorder(...));` – Andrew Thompson Apr 12 '15 at 04:45
  • And just noticed, you failed to answer **my** question, which was intended for me (and possibly others) to better understand the requirement so we can **provide the best help.** Please answer any question asked, to the best of your ability. If you do not understand the question, ask for clarification. – Andrew Thompson Apr 12 '15 at 04:57
  • @AndrewThompson sorry. Which one that I failed? I already tried to add an `EmptyBorder` like yours [EmptyBorder](http://stackoverflow.com/questions/14342314/change-emptyborder-space-colour) but nothing happen :( – enjeru Apr 12 '15 at 05:02
  • 1) I misunderstood when I first saw the example, that the `panel` was using `BorderLayout`. A `JPanel` defaults to `FlowLayout`, so to get a border layout it is necessary to pass it in the constructor or otherwise set it as the layout. 2) So adding a border did not have the effect I was expecting. (Try setting the border layout to see what I mean - the buttons will be stretched to entirely fill the bottom panel). 3) Did you change the `setBackground(..)` calls to the panel? Show your current code as an edit to the question. – Andrew Thompson Apr 12 '15 at 05:19
  • *"please help me :("* Please be patient! I do not expected to be 'prompted' for a response before 24 hours have gone by. Even then, I might be busy doing other things.. – Andrew Thompson Apr 12 '15 at 05:22
  • *"Which one that I failed?"* Oh right.. the 1st sentence in my first comment explains something I am a little confused about. The 2nd sentence is the question. (Or to put it another way - **do we need to change the color of the buttons, or the panel they are in?**) – Andrew Thompson Apr 12 '15 at 05:26

3 Answers3

1
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");
    JPanel panel = new JPanel();

    public LatihanEvent2() {
        JFrame frame = new JFrame("Contoh Event");

        panel.add(buttonRed, BorderLayout.WEST);
        panel.add(buttonGreen, BorderLayout.CENTER);
        panel.add(buttonBlue, BorderLayout.EAST);

        frame.add(panel, BorderLayout.SOUTH);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        buttonRed.addActionListener(this);
        buttonGreen.addActionListener(this);
        buttonBlue.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        String btnName = e.getActionCommand();
        if(btnName.equalsIgnoreCase("red")) {
            panel.setBackground(Color.red);
        }
        else if(btnName.equalsIgnoreCase("green")) {
            panel.setBackground(Color.green);
        }
        else {
            panel.setBackground(Color.blue);
        }
    }

    public static void main(String[] args) {
        new LatihanEvent2();
    }
}
Bhashkar
  • 11
  • 3
  • Yes, that is showing the change to the panel, as I expect is the requirement here. – Andrew Thompson Apr 12 '15 at 05:24
  • sorry, but I mean using 3 methods, yours just using 1 method hehe ._. – enjeru Apr 12 '15 at 05:24
  • @enjeru It is trivial to adapt your code to use the *idea* in this code. I've played with your code and made the change (using the 3 different methods) and seen the result you might see in this code, because it changes the color of the **panel,** not the buttons. Please post *your latest code* showing the (attempted) use of `panel.setBackground(..);`. – Andrew Thompson Apr 12 '15 at 05:31
  • I already get what Bhaskar mean, but my teacher want like this, sorry I just edit it with photoshop --> [output](http://i859.photobucket.com/albums/ab156/ten79ryuu/Screen%20Shot%202015-04-12%20at%201.34.55%20PM.png) @AndrewThompson – enjeru Apr 12 '15 at 05:42
1

This example adds a second panel (ui) to which each action sets the background. See comments in code for further tips.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class LatihanEvent2 implements ActionListener {

    private JButton buttonRed = new JButton("Red");
    private JButton buttonGreen = new JButton("Green");
    private JButton buttonBlue = new JButton("Blue");
    JPanel ui = new JPanel(new BorderLayout(5, 5));

    public LatihanEvent2() {
        JFrame frame = new JFrame("Contoh Event");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // quick and dirty way to provide space in a GUI
        ui.add(new JLabel(new ImageIcon(
                new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB))));

        JPanel panel = new JPanel();
        // border layout constraints are irrelevant to a flow layout!
        //ui.add(buttonRed, BorderLayout.WEST);
        panel.add(buttonRed);
        panel.add(buttonGreen);
        panel.add(buttonBlue);

        //Inner Class
        ListenerRed clickListener = new ListenerRed();
        buttonRed.addActionListener(clickListener);

        //Anonymous Class
        buttonGreen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ui.setBackground(Color.GREEN);
            }
        });

        //Derived Class
        buttonBlue.addActionListener(this); //Step 2

        ui.add(panel, BorderLayout.PAGE_END);

        frame.setContentPane(ui);

        frame.pack();
        //frame.show();  // Deprecated!
        frame.setVisible(true); // should be last..
    }

    public static void main(String[] args) {
        // The GUI should be created and updated on the EDT.  E.G.
        Runnable r = new Runnable() {

            @Override
            public void run() {
                new LatihanEvent2();
            }
        };
        // Here is the iomportant part of actually 
        // starting that runnable on the EDT..
        SwingUtilities.invokeLater(r);
    }

    //Inner Class
    class ListenerRed implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            ui.setBackground(Color.RED);
        }
    }

    //Derived Class
    public void actionPerformed(ActionEvent e) {
        ui.setBackground(Color.BLUE);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • ah! that's what I mean. Your coding use new material (bufferedImage) so I still confused, but I will learn it ^^ Thank you very much. sorry make you confused hehe – enjeru Apr 12 '15 at 05:56
  • *"Your coding use new material (bufferedImage)"* Comment out the line it is added and see what happens. ;) – Andrew Thompson Apr 12 '15 at 05:57
0

In more simple way, we can do it as: (after seeing your requirements)

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");
    JFrame frame = new JFrame("Contoh Event");
    JPanel panelDown = new JPanel();
    JPanel panelUp = new JPanel();

    public LatihanEvent2() {

        panelDown.add(buttonRed, BorderLayout.WEST);
        panelDown.add(buttonGreen, BorderLayout.CENTER);
        panelDown.add(buttonBlue, BorderLayout.EAST);

        frame.add(panelDown, BorderLayout.SOUTH);
        frame.add(panelUp, BorderLayout.CENTER);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        buttonRed.addActionListener(this);
        buttonGreen.addActionListener(this);
        buttonBlue.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        String btnName = e.getActionCommand();
        if(btnName.equalsIgnoreCase("red")) {
            panelUp.setBackground(Color.red);
        }
        else if(btnName.equalsIgnoreCase("green")) {
            panelUp.setBackground(Color.green);
        }
        else {
            panelUp.setBackground(Color.blue);
        }
    }

    public static void main(String[] args) {
        new LatihanEvent2();
    }
}
Bhashkar
  • 11
  • 3
  • this coding like bhaskar, right? Just use 1 method hehe >< my teacher make confused that we must use 3 method. – enjeru Apr 12 '15 at 06:11