I'm new to Java and following an online tutorial to learn swing.
I have just gotten to a part where we are adding in JCheckBox
and I can't figure out why they are not showing up when I run the program. I'm running it on Eclipse on a Mac if that makes a difference.
I've tried re-writing everything but that didn't work. I know that they are added but they are just not showing up. The JRadioButtons
are showing up just fine.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Userimputcomplex
{
public static void main(String[] args)
{
RadioDemo r = new RadioDemo();
}
}
class RadioDemo extends JFrame
{
JTextField t1; //Input Text
JButton b; //Button
JRadioButton r1,r2; //Check buttons
JLabel l; //Text
JCheckBox c1,c2; //Multiple check buttons
public RadioDemo()
{
t1 = new JTextField(15);
b = new JButton("Ok");
r1 = new JRadioButton("Male");
r2 = new JRadioButton("Female");
l = new JLabel("Greetings");
c1 = new JCheckBox("Dancing");
c2 = new JCheckBox("Singing");
ButtonGroup bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
add(t1);
add(r1);
add(r2);
add(c1);
add(c2);
add(b);
add(l);
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String name = t1.getText();
if(r1.isSelected())
{
name = "Mr." + name;
}
else
{
name = "Ms." + name;
}
if(c1.isSelected())
{
name = name + " Dancer";
}
if(c2.isSelected())
{
name = name + " Singer";
}
l.setText(name);
}
});
setLayout(new FlowLayout());
setVisible(true);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}