📄 microwaveoven.java
字号:
sevenJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when sevenJButton is pressed
public void actionPerformed( ActionEvent event )
{
sevenJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up eightJButton
eightJButton = new JButton();
eightJButton.setBounds( 54, 107, 41, 24 );
eightJButton.setText( "8" );
controlJPanel.add( eightJButton );
eightJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when eightJButton is pressed
public void actionPerformed( ActionEvent event )
{
eightJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up nineJButton
nineJButton = new JButton();
nineJButton.setBounds( 95, 107, 41, 24 );
nineJButton.setText( "9" );
controlJPanel.add( nineJButton );
nineJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when nineJButton is pressed
public void actionPerformed( ActionEvent event )
{
nineJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up zeroJButton
zeroJButton = new JButton();
zeroJButton.setBounds( 54, 131, 41, 24 );
zeroJButton.setText( "0" );
controlJPanel.add( zeroJButton );
zeroJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when zeroJButton is pressed
public void actionPerformed( ActionEvent event )
{
zeroJButtonActionPerformed( event );
}
} // 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()
{
return "";
} // end method formatTime
// start the microwave oven
private void startJButtonActionPerformed( ActionEvent event )
{
System.out.println("cooktime is:"+timeToDisplay);
String minute = new String(timeToDisplay.substring(0,2)); //得到字符型的分
String second = new String(timeToDisplay.substring(2,4)); //得到字符型的秒
//System.out.println("cooktime is:"+minute);
//System.out.println("cooktime is:"+second);
MicrowaveTime.setMinute(Integer.parseInt(minute)); //将字符型的分转换为整型分
MicrowaveTime.setSecond(Integer.parseInt(second)); //将字符型的秒转换为整型秒
displayJTextField.setText(timeFormat.format(MicrowaveTime.getMinute())+":"+timeFormat.format(MicrowaveTime.getSecond()));
clockTimer.start(); //启动定时器
windowJPanel.setBackground(new Color(255,255,255)); //填充windowJPanel的颜色
} // end method startJButtonActionPerformed
// clear the microwave oven
private void clearJButtonActionPerformed( ActionEvent event )
{
clockTimer.stop(); //关闭定时器
timeToDisplay = "";
displayJTextField.setText("00:00");
windowJPanel.setBackground(new Color(255,255,255));
} // end method clearJButtonActionPerformed
// display formatted time in displayJTextField
private void displayTime( String digit )
{
displayJTextField.setEditable(true);
timeToDisplay = timeToDisplay + digit; //设置时间
displayJTextField.setText(timeToDisplay);
} // end method displayTime
// decrement displayJTextField by one second
private void clockTimerActionPerformed( ActionEvent event )
{
MicrowaveTime.process();
if (!MicrowaveTime.OK())
{
displayJTextField.setText(timeFormat.format(MicrowaveTime.getMinute())+":"+timeFormat.format(MicrowaveTime.getSecond()));
}
else
{
clockTimer.stop(); //关闭定时器
displayJTextField.setText("00:00");
windowJPanel.setBackground(new Color(255,255,0)); //填充windowJPanel的颜色
}
} // 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -