0

I have this program in which I am trying to change the JLabel for each condition, but the problem is that the JLabel is in a different JFrame.

For example, if the user select JRadioButton1 and JRadioButton2 I want a frame to open and Label named Label1. If the user selects JRadioButton3 and JRadioButton4 I want a frame to open and Label named Label2.

This is the code

package button;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

public class button extends JFrame implements ItemListener {

    JRadioButton r1 = new JRadioButton("JRadioButton 1");
    JRadioButton r2 = new JRadioButton("JRadioButton 2");
    JRadioButton r3 = new JRadioButton("JRadioButton 3");
    JRadioButton r4 = new JRadioButton("JRadioButton 4");
    JRadioButton r5 = new JRadioButton("JRadioButton 5");
    JRadioButton r6 = new JRadioButton("JRadioButton 6");
    ButtonGroup g1 = new ButtonGroup();
    ButtonGroup g2 = new ButtonGroup();

    button() {

        setTitle("Mode");
        setSize(300, 250);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(3, 1));
        g1.add(r1);
        g2.add(r2);
        g1.add(r3);
        g2.add(r4);
        g1.add(r5);
        g2.add(r6);
        r1.addItemListener(this);
        r2.addItemListener(this);
        r3.addItemListener(this);
        r4.addItemListener(this);
        r5.addItemListener(this);
        r6.addItemListener(this);
        add(r1);
        add(r2);
        add(r3);
        add(r4);
        add(r5);
        add(r6);
    }

    public void itemStateChanged(ItemEvent e) {

        if (r1.isSelected()&&r2.isSelected()) {
            frameapplication f1 = new frameapplication();
            // add((new JLabel("Question 1     14  -  5  = ")));
        } else if (r3.isSelected()&&r4.isSelected()) {
            frameapplication f1 = new frameapplication();
        } else if (r5.isSelected()&&r6.isSelected()) {
            frameapplication f1 = new frameapplication();
        }
    }

    public class frameapplication {

        JFrame f1;
        JPanel panel1, panel4;
        JLabel label_1;
        JTextField t1;
        JButton b1, b2;

        public frameapplication() {

            f1 = new JFrame("MathTest - Test Page");
            f1.setVisible(true);
            f1.setSize(400, 150);
            f1.setLayout(new GridLayout(4, 0));
            panel1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
            panel1.add(label_1 = new JLabel("label_1"));
            panel1.add(label_1);
            panel1.add(new JTextField(10));
            panel1.add(b1 = new JButton("Submit Answer"));
            panel4 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
            panel4.add(b2 = new JButton("   Cancel Test   "));
            b2.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent x) {

                    System.exit(0);
                }
            });
            f1.add(panel1);
            f1.add(panel4);
        }
    }

    public static void main(String[] args) {

        button b1 = new button();
    }
}
user1803551
  • 12,965
  • 5
  • 47
  • 74
user3447091
  • 1
  • 1
  • 2
  • 1
    1) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson May 10 '14 at 12:08
  • And class names should start with an uppercase according to Java naming conventions. Use underscore in variable names only if they are constants. – user1803551 May 10 '14 at 12:29

1 Answers1

0

Don't think of this as working with JLabels and JFrames. Instead think about what you're trying to do overall: change the state of a component of one object from within another. You often do this the same way: give the object that needs to have its state changed a public method that the other object can call. Also, you will want to change the variable names that you're using. Names like b1, b2, and t1 and t2 do not convey much useful information, and instead you will want to give your variables names that describe their action or purpose, so that your code becomes self-commenting and much easier for us and your future self to understand.

Here, I'd give the method the name of something like: setNotificationLabelText(String text). For example:

public void setNotificationLabelText(String text) {
  notificationLabel.setText(String text);
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373