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();
}
}