Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的标签显示的组合不正确_Java - Fatal编程技术网

Java 我的标签显示的组合不正确

Java 我的标签显示的组合不正确,java,Java,当我运行程序时,输入正确的组合后,不会显示正确的消息。由于某些原因,无论输入何种组合,都会显示错误消息。我的代码如下。如果可以的话,请帮助我 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; public class Program3 extends JFrame { // JLabel that displays header on application w

当我运行程序时,输入正确的组合后,不会显示正确的消息。由于某些原因,无论输入何种组合,都会显示错误消息。我的代码如下。如果可以的话,请帮助我

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. 

输出根据用户输入计算的组合。这将帮助您调试应用程序,您会注意到,为什么总是弹出错误消息:

combination = one + two + three + four + five;
int expected = 26611;

System.out.println("User input: " + combination + ", expected: " + expected);

if(combination==expected)
{
  displayJLabel.setText ( "Combination Opened" );
}

您在微调器上输入的数字是多少?是11662吗?因为这是代码检查的数字。@TerryStorm为什么不更正答案,而不是删除它?