I'm having some issues with one of my bonus assignments for school. In my code the purpose of the project is posted, but this is what I have so far.
/********************************************
*File Name: Combination (Assignment 8 Part 1)
*Purpose: Create a frame with ten buttons, labeled 0 through 9 (Hint: You may create an array of
*10 buttons). To exit the program, the user must click on the correct three buttons in order,
*something like 7-3-5. If the wrong combination is used, a JOptionPane message, "Wrong, try again,"
*will be displayed. You may use GridLayout Manager to arrange the buttons nicely.
*Programmer: Shanel Fowler
*Date: 5/14/14
********************************************/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Combination extends JFrame
{
private JPanel panel = new JPanel();
private String input;
private String passCode = "247";
private int count = 0;
//Created ten JButton to add to the panel.
private JButton zero = new JButton("0");
private JButton one = new JButton("1");
private JButton two = new JButton("2");
private JButton three = new JButton("3");
private JButton four = new JButton("4");
private JButton five = new JButton("5");
private JButton six = new JButton("6");
private JButton seven = new JButton("7");
private JButton eight = new JButton("8");
private JButton nine = new JButton("9");
//Created a constructor to set the title, size, and whether or not the window is visible.
public Combination()
{
setTitle("Lock");
setSize(250, 100);
createComponents();
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//Created components method to determine what will be displayed and sent into the panel.
public void createComponents()
{
panel.setLayout(new GridLayout(2,5));
createNumButtons();
//Adds the panel to the frame.
add(panel);
}
public void createNumButtons()
{
//Adds the buttons to the panel.
panel.add(zero);
panel.add(one);
panel.add(two);
panel.add(three);
panel.add(four);
panel.add(five);
panel.add(six);
panel.add(seven);
panel.add(eight);
panel.add(nine);
buttonListener listen = new buttonListener();
zero.addActionListener(listen);
one.addActionListener(listen);
two.addActionListener(listen);
three.addActionListener(listen);
four.addActionListener(listen);
five.addActionListener(listen);
six.addActionListener(listen);
seven.addActionListener(listen);
eight.addActionListener(listen);
nine.addActionListener(listen);
}
/**
*Allow the user to click three buttons. If the combination is incorrect display to the screen
*that the entry was incorrect and to try again. Keep repeating the process until the user has
*correctly entered the pass code.
*/
private class buttonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
StringBuffer temp = new StringBuffer();
//Determines what will be done based on the user's selection.
while(count < 3)
{
if(zero.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(one.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(two.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(three.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(four.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(five.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(six.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(seven.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(eight.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(nine.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
}
if(temp.equals(passCode))
{
JOptionPane.showMessageDialog(null, "That is correct!");
}
else
{
JOptionPane.showMessageDialog(null, "That not correct. Please try again.");
while(count < 3)
{
if(zero.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(one.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(two.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(three.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(four.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(five.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(six.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(seven.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(eight.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
if(nine.isSelected())
{
input = e.getSource().toString();
temp.append(input);
count++;
}
}
}
}
}
public static void main(String[] args)
{
Combination obj = new Combination();
}
}