📄 microwaveoven.java
字号:
} } // end anonymous inner class ); // end call to addActionListener // set up startJButton startJButton = new JButton(); startJButton.setBounds( 7, 171, 64, 24 ); startJButton.setText( "Start" ); controlJPanel.add( startJButton ); startJButton.addActionListener( new ActionListener() // anonymous inner class { // event handler called when startJButton is pressed public void actionPerformed( ActionEvent event ) { startJButtonActionPerformed( event ); } } // end anonymous inner class ); // end call to addActionListener // set up clearJButton clearJButton = new JButton(); clearJButton.setBounds( 79, 171, 64, 24 ); clearJButton.setText( "Clear" ); controlJPanel.add( clearJButton ); clearJButton.addActionListener( new ActionListener() // anonymous inner class { // event handler called when clearJButton is pressed public void actionPerformed( ActionEvent event ) { clearJButtonActionPerformed( event ); } } // end anonymous inner class ); // end call to addActionListener // set up timerActionListener ActionListener timerActionListener = new ActionListener() // anonymous inner class { // event handler called every 1000 milliseconds public void actionPerformed( ActionEvent event ) { clockTimerActionPerformed( event ); } }; // end anonymous inner class // set up clockTimer clockTimer = new Timer( 1000, timerActionListener ); // set properties of application's window setTitle( "Microwave Oven" ); // set title bar string setSize( 536, 261 ); // set window size setVisible( true ); // display window } // end method createUserInterface // add digit 1 to timeToDisplay private void oneJButtonActionPerformed( ActionEvent event ) { displayTime( "1" ); // display time input properly } // end method oneJButtonActionPerformed // add digit 2 to timeToDisplay private void twoJButtonActionPerformed( ActionEvent event ) { displayTime( "2" ); // display time input properly } // end method twoJButtonActionPerformed // add digit 3 to timeToDisplay private void threeJButtonActionPerformed( ActionEvent event ) { displayTime( "3" ); // display time input properly } // end method threeJButtonActionPerformed // add digit 4 to timeToDisplay private void fourJButtonActionPerformed( ActionEvent event ) { displayTime( "4" ); // display time input properly } // end method fourJButtonActionPerformed // add digit 5 to timeToDisplay private void fiveJButtonActionPerformed( ActionEvent event ) { displayTime( "5" ); // display time input properly } // end method fiveJButtonActionPerformed // add digit 6 to timeToDisplay private void sixJButtonActionPerformed( ActionEvent event ) { displayTime( "6" ); // display time input properly } // end method sixJButtonActionPerformed // add digit 7 to timeToDisplay private void sevenJButtonActionPerformed( ActionEvent event ) { displayTime( "7" ); // display time input properly } // end method sevenJButtonActionPerformed // add digit 8 to timeToDisplay private void eightJButtonActionPerformed( ActionEvent event ) { displayTime( "8" ); // display time input properly } // end method eightJButtonActionPerformed // add digit 9 to timeToDisplay private void nineJButtonActionPerformed( ActionEvent event ) { displayTime( "9" ); // display time input properly } // end method nineJButtonActionPerformed // add digit 0 to timeToDisplay private void zeroJButtonActionPerformed( ActionEvent event ) { displayTime( "0" ); // display time input properly } // end method zeroJButtonActionPerformed // format the time so that it has exactly four digits private String formatTime() { // declare String currentTime to manipulate output String currentTime = timeToDisplay; // add zeros until currentTime is at least 4 characters long for ( int i = currentTime.length(); i < 4; i++ ) { currentTime = "0" + currentTime; } // if the length of currentTime is greater than four if ( currentTime.length() > 4 ) { // shorten currentTime to the first four characters currentTime = currentTime.substring( 0, 4 ); } return currentTime; } // end method formatTime // start the microwave oven private void startJButtonActionPerformed( ActionEvent event ) { // get the time as four digits String fourDigitTime = formatTime(); // extract minutes and seconds String minute = fourDigitTime.substring( 0, 2 ); String second = fourDigitTime.substring( 2 ); // initialize CookingTime object to time entered by user microwaveTime.setMinute( Integer.parseInt( minute ) ); microwaveTime.setSecond( Integer.parseInt( second ) ); // display formatted starting time in displayJTextField displayJTextField.setText( timeFormat.format( microwaveTime.getMinute() ) + ":" + timeFormat.format( microwaveTime.getSecond() ) ); timeToDisplay = ""; // clear timeToDisplay for future input clockTimer.start(); // start timer windowJPanel.setBackground( Color.YELLOW ); // turn "light" on } // end method startJButtonActionPerformed // clear the microwave oven private void clearJButtonActionPerformed( ActionEvent event ) { // stop Timer and reset variables to their initial settings clockTimer.stop(); displayJTextField.setText( "Microwave Oven" ); timeToDisplay = ""; windowJPanel.setBackground( new Color( 204, 204, 204 ) ); } // end method clearJButtonActionPerformed // display formatted time in displayJTextField private void displayTime( String digit ) { // append digit to timeToDisplay timeToDisplay += digit; // get the time as four digits String fourDigitTime = formatTime(); // extract minutes and seconds String minute = fourDigitTime.substring( 0, 2 ); String second = fourDigitTime.substring( 2 ); // display number of minutes, ":", then number of seconds displayJTextField.setText( minute + ":" + second ); } // end method displayTime // decrement displayJTextField by one second private void clockTimerActionPerformed( ActionEvent event ) { // decrement microwaveTime by one second microwaveTime.tick(); // if microwaveTime has not reached zero if ( !microwaveTime.isDone() ) { // display remaining cooking time in displayJTextField displayJTextField.setText( timeFormat.format( microwaveTime.getMinute() ) + ":" + timeFormat.format( microwaveTime.getSecond() ) ); } // end if else // microwaveTime has reached zero { clockTimer.stop(); // stop timer // inform user timer is finished displayJTextField.setText( "Done!" ); windowJPanel.setBackground( new Color( 204, 204, 204 ) ); } // end else } // end method clockTimerActionPerformed // main method public static void main( String args[] ) { MicrowaveOven application = new MicrowaveOven(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main} // end class MicrowaveOven/************************************************************************** * (C) Copyright 1992-2004 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * **************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -