Robert
Robert

Reputation: 4406

Why won't the second panel with the radio buttons show up?

We are not allowed to use IDE's in class this is being done in text pad. I am writing a traffic light program that when I click on the associated traffic light color it fills the color and makes it look like that light is active.

I cannot currently get the second panel with the radio buttons on it to show. I have it instantiated and added.

They are supposed to be separate panels.

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



public class Lab4Frame extends JFrame {

    Lab4Frame(){
        setTitle("Lab 4 - Application #1");
        Lab4Panel p = new Lab4Panel();
        Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel();
        setLayout(new GridLayout(1,2));
        add(p);
        add(p2);
    }

    public static void main(String[] args){

            Lab4Frame frame = new Lab4Frame();
            frame.setTitle("Lab4 Application # 1");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(600, 600);
            frame.setVisible(true);
    }

}

class Lab4RadioButtonPanel extends JPanel implements MouseListener {

    public Lab4RadioButtonPanel() {
        JRadioButton jrbRed = new JRadioButton("Red", true);
        JRadioButton jrbYellow = new JRadioButton("Yellow");
        JRadioButton jrbGreen = new JRadioButton("Green");
        ButtonGroup group = new ButtonGroup();
        group.add(jrbRed);
        group.add(jrbYellow);
        group.add(jrbGreen);

        jrbRed.setMnemonic('E');
        jrbGreen.setMnemonic('G');
        jrbYellow.setMnemonic('Y');
    }




            public void mouseClicked(MouseEvent e)
                {
                    /*if (e.getSource() == red){

                    }

                    else if (e.getSource() == yellow){

                    }

                    else if (e.getSource() == green){

                    }*/



                }

        public void mouseExited(MouseEvent e){}
        public void mouseReleased(MouseEvent e){}
        public void mousePressed(MouseEvent e){}
        public void mouseMoved(MouseEvent e){}
        public void mouseEntered(MouseEvent e){}
}

class Lab4Panel extends JPanel{


    public Lab4Panel(){
    }



    int height, width;
    int radius = 5;
    int x = -1;
    int y = -1;

    protected void paintComponent(Graphics g){
        if (x<0 || y<0) {
            x = getWidth() / 2 - radius;
            y = getHeight() / 2 - radius;
        }
        super.paintComponent(g);
        g.drawRect(x - 10,y - 90, 40, 120);
        //g.drawOval(x,y - 80, 4 * radius, 4 * radius);
        //g.drawOval(x,y - 40, 4 * radius, 4 * radius);
        //g.drawOval(x,y, 4 * radius, 4 * radius);
        g.drawRect(x - 5,y - 90, 40, 120);
        g.setColor(Color.RED);
        g.fillOval(x,y - 80, 4 * radius, 4 * radius);
        g.setColor(Color.YELLOW);
        g.fillOval(x,y - 40, 4 * radius, 4 * radius);
        g.setColor(Color.GREEN);
        g.fillOval(x,y, 4 * radius, 4 * radius);

    }


}

Upvotes: 1

Views: 330

Answers (2)

Jasonw
Jasonw

Reputation: 5064

From the javadoc

This class is used to create a multiple-exclusion scope for a set of buttons. Creating a set of buttons with the same ButtonGroup object means that turning "on" one of those buttons turns off all other buttons in the group.

and ButtonGroup is not extending from Component. ButtonGroup is "managing" the state of the added buttons. So we have to add the buttons instead of the ButtonGroup. Something like in the constructor Lab4RadioButtonPanel in class Lab4RadioButtonPanel

public Lab4RadioButtonPanel()
{
   JRadioButton jrbRed = new JRadioButton("Red", true);
   JRadioButton jrbYellow = new JRadioButton("Yellow");
   JRadioButton jrbGreen = new JRadioButton("Green");
   ButtonGroup group = new ButtonGroup();
   group.add(jrbRed);
   group.add(jrbYellow);
   group.add(jrbGreen);
   add(jrbRed);
   add(jrbYellow);
   add(jrbGreen);

   jrbRed.setMnemonic('E');
   jrbGreen.setMnemonic('G');
   jrbYellow.setMnemonic('Y');
}

Upvotes: 1

colbadhombre
colbadhombre

Reputation: 813

The JRadioButtons are never added to a container.

Upvotes: 3

Related Questions