运行程序时,输入正确的组合后不会显示正确的信息。由于某种原因,无论输入什么组合,都会显示错误消息。我的代码如下。如果可以,请帮助我。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class Program3 extends JFrame
{
// JLabel that displays header on application window
private JLabel instructionsJLabel;
// JSpinners for combination lock
private JSpinner oneJSpinner;
private JSpinner twoJSpinner;
private JSpinner threeJSpinner;
private JSpinner fourJSpinner;
private JSpinner fiveJSpinner;
// JLabel for buttons
private JButton openJButton;
private JButton resetJButton;
// Message area
private JLabel displayJLabel;
// no-argument constructor
public Program3()
{
createUserInterface();
}
// create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
contentPane.setLayout( null );
//set up instructionsJLabel
instructionsJLabel = new JLabel();
instructionsJLabel.setBounds( 10, 5, 200, 30 );
instructionsJLabel.setText( "Combination Lock" );
instructionsJLabel.setFont(
new Font( "Arial", Font.PLAIN, 16 ) );
contentPane.add( instructionsJLabel );
//set up JSpinner one
oneJSpinner = new JSpinner(
new SpinnerNumberModel( 1, 1, 9, 1 ) );
oneJSpinner.setBounds( 16, 50, 30, 20);
oneJSpinner.setValue( 5 );
contentPane.add( oneJSpinner );
//set up JSpinner two
twoJSpinner = new JSpinner(
new SpinnerNumberModel( 1, 1, 9, 1) );
twoJSpinner.setBounds( 66, 50, 30, 20);
twoJSpinner.setValue( 5 );
contentPane.add( twoJSpinner );
//set up JSpinner three
threeJSpinner = new JSpinner(
new SpinnerNumberModel( 1, 1, 9, 1 ) );
threeJSpinner.setBounds( 116, 50, 30, 20);
threeJSpinner.setValue( 5 );
contentPane.add( threeJSpinner );
//set up JSpinner four
fourJSpinner = new JSpinner(
new SpinnerNumberModel( 1, 1, 9, 1) );
fourJSpinner.setBounds( 166, 50, 30, 20);
fourJSpinner.setValue( 5 );
contentPane.add( fourJSpinner );
//set up JSpinner five
fiveJSpinner = new JSpinner(
new SpinnerNumberModel( 1, 1, 9, 1 ) );
fiveJSpinner.setBounds( 216, 50, 30, 20);
fiveJSpinner.setValue( 5 );
contentPane.add( fiveJSpinner );
// set up openJButton and register its event handler
openJButton = new JButton();
openJButton.setBounds( 16, 100, 80, 25 );
openJButton.setText( "Open" );
openJButton.setFont(
new Font( "Arial", Font.PLAIN, 12 ) );
contentPane.add( openJButton );
openJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when user clicks openJButton
public void actionPerformed( ActionEvent event )
{
openJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up resetJButton and register its event handler
resetJButton = new JButton();
resetJButton.setBounds( 166, 100, 80, 25 );
resetJButton.setText( "Reset" );
resetJButton.setFont(
new Font( "Arial", Font.PLAIN, 12 ) );
contentPane.add( resetJButton );
resetJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when user clicks resetJButton
public void actionPerformed( ActionEvent event )
{
resetJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up displayJLabel
displayJLabel = new JLabel();
displayJLabel.setBounds( 16, 150, 300, 25 );
displayJLabel.setText( "" );
contentPane.add( displayJLabel );
setTitle("Locker");
setSize (300, 250);
setVisible(true);
}// } end method createUserInterface
// method called when user clicks openJButton
private int combination=26611;
private void openJButtonActionPerformed( ActionEvent event )
{
Integer one = ( Integer )oneJSpinner.getValue();
Integer two = ( Integer )twoJSpinner.getValue()*10;
Integer three = ( Integer )threeJSpinner.getValue()*100;
Integer four = ( Integer )fourJSpinner.getValue()*1000;
Integer five = ( Integer )fiveJSpinner.getValue()*10000;
combination = one + two + three + four + five;
if(combination==26611)
{
displayJLabel.setText ( "Combination Opened" );
}
else {
displayJLabel.setText ( "Wrong Combination. Hit Reset and Try Again" );
} // end else
} // end method openJButtonActionPerformed
private void resetJButtonActionPerformed( ActionEvent event )
{
oneJSpinner.setValue ( 5 );
twoJSpinner.setValue( 5 );
threeJSpinner.setValue( 5 );
fourJSpinner.setValue( 5 );
fiveJSpinner.setValue( 5 );
displayJLabel.setText( "" );
}
//
public static void main ( String[] args )
{
Program3 application = new Program3();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} // end method main
} // end class Program 3.
答案 0 :(得分:0)
根据用户的输入输出您计算的组合。这将帮助您调试应用程序,您会注意到,为什么总是会弹出错误消息:
combination = one + two + three + four + five;
int expected = 26611;
System.out.println("User input: " + combination + ", expected: " + expected);
if(combination==expected)
{
displayJLabel.setText ( "Combination Opened" );
}