Reputation: 747
I am learning java GUI with swing library. I know how to make a JFrame
and add JButton
, add ActionListener
e.t.c but today JFrame
is not showing. I am doing everything as usual. Please have a look at my code and suggest where am I doing wrong..
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.TimeUnit;
public class MyGroup extends JFrame {
private ButtonGroup myGroup = new ButtonGroup();
public MyGroup(){
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
JRadioButton b1 = new JRadioButton("check1");
JRadioButton b2 = new JRadioButton("check2");
JRadioButton b3 = new JRadioButton("check3");
JRadioButton b4 = new JRadioButton("check4");
add(b1);
add(b2);
add(b3);
add(b4);
myGroup.add(b1);
myGroup.add(b2);
myGroup.add(b3);
myGroup.add(b4);
setVisible(true);
}
public static void main(String[] args) throws Exception{
ButtonGroup m = new ButtonGroup();
}
}
Upvotes: 0
Views: 74
Reputation: 68935
You need to add your ButtonGroup myGroup
to your frame.
//your code
getContentPane().add(myGroup);
setVisible(true);
Also in your main method you need to create your JFrame object and not ButtonGroup.
MyGroup myFrame = new MyGroup();
Upvotes: 0
Reputation: 337
You did a simple typo in the main method. Instead of calling the ButtonGroup
myGroup
, consider changing it to calling the object MyGroup
like this:
public static void main(String[] args) throws Exception{
MyGroup group = new MyGroup();
}
Upvotes: 2
Reputation: 72284
You're creating a new ButtonGroup
in your main method, not a new MyGroup
. I would imagine that since the latter executes your constructor and makes your frame visible, that's where your mistake lies!
Upvotes: 3