mardi 22 novembre 2016

Second CheckboxGroup Not Working

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

public class Applet2 extends Applet implements ItemListener, ActionListener
{
int colourNum;
int shapeNum;
Font textFont;
int size = 100;
int x;
int y;
boolean start = false;
CheckboxGroup group;
CheckboxGroup group1;
Checkbox colourRed, colourGreen, colourBlue, colourYellow, shapeSquare, shapeRoundSquare, shapeCircle, shapeSemiCircle;
Button bttn1 = new Button ("Draw");
TextField txt1 = new TextField (10);


public void init ()
{
    group = new CheckboxGroup ();

    colourRed = new Checkbox ("Red", group, true);
    colourGreen = new Checkbox ("Green", group, false);
    colourBlue = new Checkbox ("Blue", group, false);
    colourYellow = new Checkbox ("Yellow", group, false);

    shapeSquare = new Checkbox ("Square", group1, true);
    shapeRoundSquare = new Checkbox ("Rounded Square", group1, false);
    shapeCircle = new Checkbox ("Circle", group1, false);
    shapeSemiCircle = new Checkbox ("Semi Circle", group1, false);

    setBackground (Color.white);
    colourNum = 1;
    textFont = new Font ("Calibri", Font.BOLD, 24);

    colourRed.addItemListener (this);
    colourGreen.addItemListener (this);
    colourBlue.addItemListener (this);
    colourYellow.addItemListener (this);
    shapeSquare.addItemListener (this);
    shapeRoundSquare.addItemListener (this);
    shapeCircle.addItemListener (this);
    shapeSemiCircle.addItemListener (this);


    JPanel radioPanel = new JPanel (new GridLayout (0, 1));
    radioPanel.add (colourRed);
    radioPanel.add (colourGreen);
    radioPanel.add (colourBlue);
    radioPanel.add (colourYellow);
    add (radioPanel, BorderLayout.LINE_START);

    JPanel radioPanel1 = new JPanel (new GridLayout (0, 1));
    radioPanel1.add (shapeSquare);
    radioPanel1.add (shapeRoundSquare);
    radioPanel1.add (shapeCircle);
    radioPanel1.add (shapeSemiCircle);
    add (radioPanel1, BorderLayout.LINE_START);

    add (txt1);
    bttn1.addActionListener (this);
    add (bttn1);

}

When I run my program, the first J Panel with the first CheckboxGroup works, as only 1 option can be selected at once. However in the second JPanel with the second CheckboxGroup, it doesn't work. The second JPanel has square checkboxes instead of circular ones and can select more than 1 option.

If someone can help me with is, it would be appreciated.




Aucun commentaire:

Enregistrer un commentaire