Can anybody tell me why I'm not getting checkboxes? Sometimes I can get one, but never the three I need.
public class SwingLab
implements ActionListener
{
// instance variables - this instance will get reader's name
private Scanner reader;//name from user
private Random random;
private ArrayList<String> name;//construct a responder, declare a field type ArrayList
private String userName;//stores userName for later use.
private JFrame frame;
private String firstCheck;
private String secondCheck;
private String thirdCheck;
/**
* Constructor for objects of class Calculator
* Create a responder.
* Create the Random and ArrayList object.
* Create the Scanner input object.
*/
public SwingLab()
{
reader = new Scanner(System.in);
name = new ArrayList<String>();
makeFrame();
makeCheckBox();
}
/**
* Saves userName for later use.
*/
private String userName()
{
userName = reader.nextLine();
return userName;
}
public void actionPerformed(ActionEvent event)
{
System.out.println("Item: " + event.getActionCommand());
}
private void start()
{
}
/**
* Create the Swing frame and it's content.
*/
private void makeFrame()
{
frame = new JFrame("SwingLab");
makeMenuBar(frame);
Container contentPane = frame.getContentPane();
System.out.println("What is your name?");
userName=reader.nextLine();
System.out.println("Please enter your first check box label");
firstCheck=reader.nextLine();
System.out.println("You entered " + firstCheck + ". Please enter your second one.");
secondCheck=reader.nextLine();
System.out.println("You entered " + secondCheck + ". Please enter your third one.");
thirdCheck=reader.nextLine();
System.out.println("You entered " + thirdCheck + " for your final entry. Your check boxes will now be created.");
JLabel label = new JLabel("Welcome to my first GUI, " + userName + ". It's pretty, but doesn't really work yet!");
contentPane.add(label);
frame.pack();
frame.setVisible(true);
}
private void makeCheckBox()
{
//this is to create the first checkbox the user created
JCheckBox firstCheck = new JCheckBox(this.firstCheck);
frame.add(firstCheck);
frame.setSize(400, 400);
frame.setVisible(true);
JCheckBox secondCheck = new JCheckBox(this.secondCheck);
frame.add(secondCheck);
frame.setSize(600, 600);
frame.setVisible(true);
JCheckBox thirdCheck = new JCheckBox(this.thirdCheck);
frame.add(thirdCheck);
frame.setSize(700, 700);
frame.setVisible(true);
JPanel checkPanel = new JPanel (new GridLayout(0, 1));
checkPanel.add(firstCheck);
checkPanel.add(secondCheck);
checkPanel.add(thirdCheck);
}
/**
* Create main frame's menu bar.
*/
private void makeMenuBar(JFrame frame)
{
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
//create File menu
JMenu fileMenu = new JMenu("File");
menubar.add(fileMenu);
//create Open menu item.
JMenuItem openItem = new JMenuItem("Open");
openItem.addActionListener(this);
fileMenu.add(openItem);
//create Quit menu item.
JMenuItem quitItem = new JMenuItem("Quit");
quitItem.addActionListener(this);
fileMenu.add(quitItem);
}
}