4

Why is it that when the GUI I can press A, B, and C but if I click A and then C it (the GUI) doesn't change and vice versa but if I click B it will work for all? I don't understand? Please if anyone notices any mistakes or something let me know. And ps I'm a beginner so please if you can explain in layman's terms. Thank you

package Experimental;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EventListener2 extends JFrame implements ActionListener{
    JButton a,b,c;
    JLabel aa,bb,cc;
    JPanel top, bottom;
    EventListener2(){
        super("Event Listener 2");
        setSize(300,300);
        setLayout(new FlowLayout());
        a=new JButton("A");
        aa = new JLabel("Button \"A\" was pressed");
        b=new JButton("B");
        bb = new JLabel("Button \"B\" was pressed");
        c=new JButton("C");
        cc = new JLabel("Button \"C\" was pressed");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        a.addActionListener(this);
        b.addActionListener(this);
        c.addActionListener(this);

        top=new JPanel();
        top.setLayout(new FlowLayout());
        top.add(a);
        top.add(b);
        top.add(c);
        bottom=new JPanel();

        this.add(top);
        this.add(bottom);

        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        // TODO Auto-generated method stub
        Object source = event.getSource();
        bottom.removeAll();
        if (source==a){
            bottom.add(aa);
            System.out.println("a was pressed");
        }
        else if (source==b){
            bottom.add(bb);
            System.out.println("b was pressed");
        }
        else{
            bottom.add(cc);
            System.out.println("c was pressed");
        }
        this.setVisible(true);

    }
    public static void main(String[] args) {
         EventListener2 a = new EventListener2();
    }

}
Bryce Caro
  • 65
  • 10
  • 2
    That entire `actionPerformed(..)` method makes it clear the `bottom` panel should have a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Jul 25 '15 at 21:23

1 Answers1

5

Change your actionPerformed method into this:

public void actionPerformed(ActionEvent event) {
    Object source = event.getSource();
    bottom.removeAll();
    if (source==a){
        bottom.add(aa);
        System.out.println("a was pressed");
    }
    else if (source==b){
        bottom.add(bb);
        System.out.println("b was pressed");
    }
    else{
        bottom.add(cc);
        System.out.println("c was pressed");
    }
    bottom.revalidate();
    bottom.repaint();
    this.setVisible(true);
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35
  • 2
    `@Override` is annotation used to check if we are really overriding method. It is good to add it to methods like `equals` or `hashCode` to make sure we used correct arguments and did not misspelled methods name. There is no point to add it to method invocation. – Pshemo Jul 25 '15 at 21:20
  • No problem :) You can read more about it here: [When do you use Java's @Override annotation and why?](http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why) – Pshemo Jul 25 '15 at 21:23
  • Also your English seems to be OK (at lest to me - non native English speaker) so don't worry about it that much. It doesn't need to be perfect, as long as we are able to understand what you are trying to say. – Pshemo Jul 25 '15 at 21:29