The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Locker Combination with JSpinners
Discuss Locker Combination with JSpinners in the Java Help forum on Dev Shed. Locker Combination with JSpinners Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 8th, 2010, 12:02 PM
|
|
Registered User
|
|
Join Date: Oct 2010
Posts: 3
Time spent in forums: 43 m 20 sec
Reputation 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);
}
}
|

March 1st, 2013, 08:59 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 1
Time spent in forums: 11 m 47 sec
Reputation Power: 0
|
|
your missing a "checker"
**Read the bottom message**
Quote: | 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!

|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|