cashregister.java
来自「32个java程序源代码」· Java 代码 · 共 549 行 · 第 1/2 页
JAVA
549 行
// Exercise: 11.16 CashRegister.java// Application simulates the behavior of a cash register, allowing// the user to input prices, keep a running subtotal, and// calculate the sales tax and total.import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.text.*;public class CashRegister extends JFrame{ double subtotal = 0.0; // stores subtotal of items purchased // JLabel and JTextField to enter amount private JLabel amountJLabel; private JTextField amountJTextField; // JButtons to enter numbers in amountJTextField 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; private JButton pointJButton; // JButton to add value in amountJTextField to subtotal private JButton enterJButton; // JButton to determine tax and calculate final total private JButton totalJButton; // JButton to delete value displayed in amountJTextField private JButton deleteJButton; // JButton to clear results private JButton clearJButton; // JLabel and JTextField to display subtotal private JLabel subtotalJLabel; private JTextField subtotalJTextField; // JLabel and JTextField to display tax private JLabel taxJLabel; private JTextField taxJTextField; // JLabel and JTextField to display final total private JLabel totalJLabel; private JTextField totalJTextField; // DecimalFormat to format dollar amounts private DecimalFormat dollars = new DecimalFormat( "$0.00" ); // no-argument constructor public CashRegister() { 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 amountJLabel amountJLabel = new JLabel(); amountJLabel.setBounds( 20, 20, 15, 20 ); amountJLabel.setText( "$" ); contentPane.add( amountJLabel ); // set up amountJTextField amountJTextField = new JTextField(); amountJTextField.setBounds( 40, 20, 260, 20 ); contentPane.add( amountJTextField ); // set up oneJButton oneJButton = new JButton(); oneJButton.setBounds( 55, 70, 45, 20 ); oneJButton.setText( "1" ); contentPane.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( 100, 70, 45, 20 ); twoJButton.setText( "2" ); contentPane.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( 145, 70, 45, 20 ); threeJButton.setText( "3" ); contentPane.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( 55, 90, 45, 20 ); fourJButton.setText( "4" ); contentPane.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( 100, 90, 45, 20 ); fiveJButton.setText( "5" ); contentPane.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( 145, 90, 45, 20 ); sixJButton.setText( "6" ); contentPane.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( 55, 110, 45, 20 ); sevenJButton.setText( "7" ); contentPane.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( 100, 110, 45, 20 ); eightJButton.setText( "8" ); contentPane.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( 145, 110, 45, 20 ); nineJButton.setText( "9" ); contentPane.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( 100, 130, 45, 20 ); zeroJButton.setText( "0" ); contentPane.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 pointJButton pointJButton = new JButton();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?