📄 microwaveoven.java
字号:
// Tutorial 18: MicrowaveOven.java// Mimics the behavior of a microwave oven.import java.awt.*;import java.awt.event.*;import java.text.DecimalFormat;import javax.swing.*;import javax.swing.border.*;public class MicrowaveOven extends JFrame{ // JPanel for microwave window private JPanel windowJPanel; // JPanel for microwave controls private JPanel controlJPanel; // JTextField for cooking time private JTextField displayJTextField; // JButtons to set cooking time private JButton oneJButton; private JButton twoJButton; private JButton threeJButton; private JButton fourJButton; private JButton fiveJButton; private JButton sixJButton; private JButton sevenJButton; private JButton eightJButton; private JButton nineJButton; private JButton zeroJButton; // JButtons to start and clear timer private JButton startJButton; private JButton clearJButton; // Timer to count down seconds private Timer clockTimer; // String for storing digits entered by user private String timeToDisplay = ""; // Time instance for storing the current time private CookingTime microwaveTime = new CookingTime( 0, 0 ); // DecimalFormat to format time output private DecimalFormat timeFormat = new DecimalFormat( "00" ); // no-argument constructor public MicrowaveOven() { createUserInterface(); } // create and position GUI components; register event handlers private void createUserInterface() { // get content pane for attaching GUI components Container contentPane = getContentPane(); // enable explicit positioning of GUI components contentPane.setLayout( null ); // set up windowJPanel windowJPanel = new JPanel(); windowJPanel.setBounds( 16, 16, 328, 205 ); windowJPanel.setBorder( new LineBorder( Color.BLACK ) ); contentPane.add( windowJPanel ); // set up controlJPanel controlJPanel = new JPanel(); controlJPanel.setBounds( 368, 16, 149, 205 ); controlJPanel.setBorder( new LineBorder( Color.BLACK ) ); controlJPanel.setLayout( null ); contentPane.add( controlJPanel ); // set up displayJTextField displayJTextField = new JTextField(); displayJTextField.setBounds( 7, 5, 135, 42 ); displayJTextField.setText( "Microwave Oven" ); displayJTextField.setHorizontalAlignment( JTextField.CENTER ); displayJTextField.setEditable( false ); controlJPanel.add( displayJTextField ); // set up oneJButton oneJButton = new JButton(); oneJButton.setBounds( 13, 59, 41, 24 ); oneJButton.setText( "1" ); controlJPanel.add( oneJButton ); oneJButton.addActionListener( new ActionListener() // anonymous inner class { // event handler called when oneJButton is pressed public void actionPerformed( ActionEvent event ) { oneJButtonActionPerformed( event ); } } // end anonymous inner class ); // end call to addActionListener // set up twoJButton twoJButton = new JButton(); twoJButton.setBounds( 54, 59, 41, 24 ); twoJButton.setText( "2" ); controlJPanel.add( twoJButton ); twoJButton.addActionListener( new ActionListener() // anonymous inner class { // event handler called when twoJButton is pressed public void actionPerformed( ActionEvent event ) { twoJButtonActionPerformed( event ); } } // end anonymous inner class ); // end call to addActionListener // set up threeJButton threeJButton = new JButton(); threeJButton.setBounds( 95, 59, 41, 24 ); threeJButton.setText( "3" ); controlJPanel.add( threeJButton ); threeJButton.addActionListener( new ActionListener() // anonymous inner class { // event handler called when threeJButton is pressed public void actionPerformed( ActionEvent event ) { threeJButtonActionPerformed( event ); } } // end anonymous inner class ); // end call to addActionListener // set up fourJButton fourJButton = new JButton(); fourJButton.setBounds( 13, 83, 41, 24 ); fourJButton.setText( "4" ); controlJPanel.add( fourJButton ); fourJButton.addActionListener( new ActionListener() // anonymous inner class { // event handler called when fourJButton is pressed public void actionPerformed( ActionEvent event ) { fourJButtonActionPerformed( event ); } } // end anonymous inner class ); // end call to addActionListener // set up fiveJButton fiveJButton = new JButton(); fiveJButton.setBounds( 54, 83, 41, 24 ); fiveJButton.setText( "5" ); controlJPanel.add( fiveJButton ); fiveJButton.addActionListener( new ActionListener() // anonymous inner class { // event handler called when fiveJButton is pressed public void actionPerformed( ActionEvent event ) { fiveJButtonActionPerformed( event ); } } // end anonymous inner class ); // end call to addActionListener // set up sixJButton sixJButton = new JButton(); sixJButton.setBounds( 95, 83, 41, 24 ); sixJButton.setText( "6" ); controlJPanel.add( sixJButton ); sixJButton.addActionListener( new ActionListener() // anonymous inner class { // event handler called when sixJButton is pressed public void actionPerformed( ActionEvent event ) { sixJButtonActionPerformed( event ); } } // end anonymous inner class ); // end call to addActionListener // set up sevenJButton sevenJButton = new JButton(); sevenJButton.setBounds( 13, 107, 41, 24 ); sevenJButton.setText( "7" ); controlJPanel.add( sevenJButton ); 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 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -