#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Locker Combination with JSpinners


    Hello Everyone I'm trying to do a program for my class, but im stuck on one part. I cannot figure out what code I need to add on line 165, which is the private void openJButtonActionPerformed(ActionEvent event line. I have to get the values from the 5 Jspinners. If the number on the JSpinners is 1,2,3,4, and a message dialog box should say the locker is open, but if it is not 1,2,3,4,5 a message box should say locker is not open.

    this is the code I have

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;


    public class Program4 extends JFrame {



    private JLabel nameJLabel;

    private JButton resetJButton;

    private JButton openJButton;

    private JSpinner comb1JSpinner;
    private JSpinner comb2JSpinner;
    private JSpinner comb3JSpinner;
    private JSpinner comb4JSpinner;
    private JSpinner comb5JSpinner;





    public Program4( )
    {
    createUserInterface( );
    }

    private void createUserInterface( )
    {
    Container contentPane = getContentPane( );
    contentPane.setLayout(null);

    //
    //code to create labels, textfields and buttons goes here
    //
    nameJLabel = new JLabel();
    nameJLabel.setBounds(30,30,300,21);
    nameJLabel.setText("Combination Lock");
    contentPane.add(nameJLabel);



    comb1JSpinner = new JSpinner(
    new SpinnerNumberModel(5,1,9,1));
    comb1JSpinner.setBounds(10,68,30,30);
    contentPane.add(comb1JSpinner);



    comb2JSpinner = new JSpinner(
    new SpinnerNumberModel(5,1,9,1));
    comb2JSpinner.setBounds(50,68,30,30);
    contentPane.add(comb2JSpinner);



    comb3JSpinner = new JSpinner(
    new SpinnerNumberModel(5,1,9,1));
    comb3JSpinner.setBounds(90,68,30,30);
    contentPane.add(comb3JSpinner);



    comb4JSpinner = new JSpinner(
    new SpinnerNumberModel(5,1,9,1));
    comb4JSpinner.setBounds(130,68,30,30);
    contentPane.add(comb4JSpinner);



    comb5JSpinner = new JSpinner(
    new SpinnerNumberModel(5,1,9,1));
    comb5JSpinner.setBounds(170,68,30,30);
    contentPane.add(comb5JSpinner);


    openJButton = new JButton ();
    openJButton.setText( "Open" );
    openJButton.setBounds( 13, 120, 70, 25 );
    openJButton.setFont(
    new Font( "Arial", Font.PLAIN, 14 ) );
    contentPane.add( openJButton );
    openJButton.addActionListener(

    new ActionListener() // anonymous inner class
    {
    // called when subtractJButton is pressed
    public void actionPerformed( ActionEvent event )
    {
    openJButtonActionPerformed(event);
    }

    } // end anonymous inner class

    ); // end call to addActionListener



    resetJButton = new JButton ();
    resetJButton.setText( "Reset" );
    resetJButton.setBounds( 120, 120, 80, 25 );
    resetJButton.setFont(
    new Font( "Arial", Font.PLAIN, 14 ) );
    contentPane.add( resetJButton );
    resetJButton.addActionListener(

    new ActionListener() // anonymous inner class
    {
    // called when divideJButton is pressed
    public void actionPerformed( ActionEvent event )
    {
    resetJButtonActionPerformed(event);
    }

    } // end anonymous inner class

    ); // end call to addActionListener

































    setTitle("Locker");
    setSize(225, 200);
    setVisible(true);
    }

    //
    // Put code for displayJButtonActionPerformed
    //
    private void openJButtonActionPerformed(ActionEvent event)
    {

    Integer comb1 = (Integer)comb1JSpinner.getValue();
    Integer comb2 = (Integer)comb2JSpinner.getValue();
    Integer comb3 = (Integer)comb3JSpinner.getValue();
    Integer comb4 = (Integer)comb4JSpinner.getValue();
    Integer comb5 = (Integer)comb5JSpinner.getValue();














    }

    private void resetJButtonActionPerformed(ActionEvent event)
    {


    comb1JSpinner.setValue(new Integer(5));
    comb2JSpinner.setValue(new Integer(5));
    comb3JSpinner.setValue(new Integer(5));
    comb4JSpinner.setValue(new Integer(5));
    comb5JSpinner.setValue(new Integer(5));


    }

    public static void main(String[] args)
    {
    Program4 application = new Program4();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Lightbulb your missing a "checker"


    **Read the bottom message**
    Originally Posted by thor33443
    Hello Everyone I'm trying to do a program for my class, but im stuck on one part. I cannot figure out what code I need to add on line 165, which is the private void openJButtonActionPerformed(ActionEvent event line. I have to get the values from the 5 Jspinners. If the number on the JSpinners is 1,2,3,4, and a message dialog box should say the locker is open, but if it is not 1,2,3,4,5 a message box should say locker is not open.

    this is the code I have

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;


    public class Program4 extends JFrame {



    private JLabel nameJLabel;

    private JButton resetJButton;

    private JButton openJButton;

    private JSpinner comb1JSpinner;
    private JSpinner comb2JSpinner;
    private JSpinner comb3JSpinner;
    private JSpinner comb4JSpinner;
    private JSpinner comb5JSpinner;





    public Program4( )
    {
    createUserInterface( );
    }

    private void createUserInterface( )
    {
    Container contentPane = getContentPane( );
    contentPane.setLayout(null);

    //
    //code to create labels, textfields and buttons goes here
    //
    nameJLabel = new JLabel();
    nameJLabel.setBounds(30,30,300,21);
    nameJLabel.setText("Combination Lock");
    contentPane.add(nameJLabel);



    comb1JSpinner = new JSpinner(
    new SpinnerNumberModel(5,1,9,1));
    comb1JSpinner.setBounds(10,68,30,30);
    contentPane.add(comb1JSpinner);



    comb2JSpinner = new JSpinner(
    new SpinnerNumberModel(5,1,9,1));
    comb2JSpinner.setBounds(50,68,30,30);
    contentPane.add(comb2JSpinner);



    comb3JSpinner = new JSpinner(
    new SpinnerNumberModel(5,1,9,1));
    comb3JSpinner.setBounds(90,68,30,30);
    contentPane.add(comb3JSpinner);



    comb4JSpinner = new JSpinner(
    new SpinnerNumberModel(5,1,9,1));
    comb4JSpinner.setBounds(130,68,30,30);
    contentPane.add(comb4JSpinner);



    comb5JSpinner = new JSpinner(
    new SpinnerNumberModel(5,1,9,1));
    comb5JSpinner.setBounds(170,68,30,30);
    contentPane.add(comb5JSpinner);


    openJButton = new JButton ();
    openJButton.setText( "Open" );
    openJButton.setBounds( 13, 120, 70, 25 );
    openJButton.setFont(
    new Font( "Arial", Font.PLAIN, 14 ) );
    contentPane.add( openJButton );
    openJButton.addActionListener(

    new ActionListener() // anonymous inner class
    {
    // called when subtractJButton is pressed
    public void actionPerformed( ActionEvent event )
    {
    openJButtonActionPerformed(event);
    }

    } // end anonymous inner class

    ); // end call to addActionListener



    resetJButton = new JButton ();
    resetJButton.setText( "Reset" );
    resetJButton.setBounds( 120, 120, 80, 25 );
    resetJButton.setFont(
    new Font( "Arial", Font.PLAIN, 14 ) );
    contentPane.add( resetJButton );
    resetJButton.addActionListener(

    new ActionListener() // anonymous inner class
    {
    // called when divideJButton is pressed
    public void actionPerformed( ActionEvent event )
    {
    resetJButtonActionPerformed(event);
    }

    } // end anonymous inner class

    ); // end call to addActionListener

































    setTitle("Locker");
    setSize(225, 200);
    setVisible(true);
    }

    //
    // Put code for displayJButtonActionPerformed
    //
    private void openJButtonActionPerformed(ActionEvent event)
    {

    Integer comb1 = (Integer)comb1JSpinner.getValue();
    Integer comb2 = (Integer)comb2JSpinner.getValue();
    Integer comb3 = (Integer)comb3JSpinner.getValue();
    Integer comb4 = (Integer)comb4JSpinner.getValue();
    Integer comb5 = (Integer)comb5JSpinner.getValue();

    if((combo1==1)&&(combo2==1)&&(combo3==1)&&(combo4==1)&&(combo5==1))
    System.out.println("Got It");

    }

    private void resetJButtonActionPerformed(ActionEvent event)
    {


    comb1JSpinner.setValue(new Integer(5));
    comb2JSpinner.setValue(new Integer(5));
    comb3JSpinner.setValue(new Integer(5));
    comb4JSpinner.setValue(new Integer(5));
    comb5JSpinner.setValue(new Integer(5));


    }

    public static void main(String[] args)
    {
    Program4 application = new Program4();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    }

    After looking at your code i found out that you are simply missing an "if-statement". I have tried it and it works. here it is:
    if((combo1==1)&&(combo2==1)&&(combo3==1)&&(combo4==1)&&(combo5==1))
    System.out.println("You Got the Combo!");
    I put it in your code so all you have to do is copy and paste!
    Just make sure you change the combo so its not 1-1-1-1-1

    Hope this helped!

IMN logo majestic logo threadwatch logo seochat tools logo