Radio Buttons and Check Box Create a java Application and add the following cont
ID: 3708015 • Letter: R
Question
Radio Buttons and Check Box
Create a java Application and add the following control layout: Dental Payment form. Add code to the ‘Compute Total’ button.
The code should compute total cost based on the type of services selected and based on the Discount selected.
Display the name of the patient as well as the total cost.
Note: if you select other, you need to provide the cost related to ‘other’ services. You can create a border around a panel by adding the following code panel.setBorder(BorderFactory.createTitledBorder(name));
You can try different border lines
Dental Payment Form Patient name: Cleaning S65 Cavity Filling $200 Root Canal $250 Other Discounts Holiday 15% off Special 25% off No Discount Today Compute Total Total Write down the patient name. then The User Selected when the user click on the compute total"Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
public class DentalPayment extends JFrame
{
// JLabel that displays header on application window
private JLabel dentalPaymentFormJLabel;
// JLabel and JTextField for patient name
private JLabel patientNameJLabel;
private JTextField patientNameJTextField;
// JCheckBox and JLabel for cleaning
private JCheckBox cleaningJCheckBox;
private JLabel cleaningPriceJLabel;
// JCheckBox and JLabel for cavity filling
private JCheckBox cavityFillingJCheckBox;
private JLabel cavityFillingPriceJLabel;
// JCheckBox and JLabel for X-Ray
private JCheckBox xRayJCheckBox;
private JLabel xRayPriceJLabel;
// JLabel and JTextField for total fee
private JLabel totalJLabel;
private JTextField totalJTextField;
// JButton to initiate calculation of fee
private JButton calculateJButton;
// no-argument constructor
public DentalPayment()
{
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 dentalPaymentFormJLabel
dentalPaymentFormJLabel = new JLabel();
dentalPaymentFormJLabel.setBounds( 19, 19, 235, 28 );
dentalPaymentFormJLabel.setText( "Dental Payment Form" );
dentalPaymentFormJLabel.setFont(
new Font( "Default", Font.PLAIN, 22 ) );
dentalPaymentFormJLabel.setHorizontalAlignment(
JLabel.CENTER );
contentPane.add( dentalPaymentFormJLabel );
// set up patientNameJLabel
patientNameJLabel = new JLabel();
patientNameJLabel.setBounds( 19, 65, 91, 21 );
patientNameJLabel.setText( "Patient name:" );
contentPane.add( patientNameJLabel );
// set up patientNameJTextField
patientNameJTextField = new JTextField();
patientNameJTextField.setBounds( 132, 65, 117, 21 );
contentPane.add( patientNameJTextField );
// set up cleaningJCheckBox
cleaningJCheckBox = new JCheckBox();
cleaningJCheckBox.setBounds( 16, 112, 122, 24 );
cleaningJCheckBox.setText( "Cleaning" );
contentPane.add( cleaningJCheckBox );
// set up cleaningPriceJLabel
cleaningPriceJLabel = new JLabel();
cleaningPriceJLabel.setBounds( 211, 112, 38, 24 );
cleaningPriceJLabel.setText( "$35" );
cleaningPriceJLabel.setHorizontalAlignment( JLabel.RIGHT );
contentPane.add( cleaningPriceJLabel );
// set up cavityFillingJCheckBox
cavityFillingJCheckBox = new JCheckBox();
cavityFillingJCheckBox.setBounds( 16, 159, 122, 24 );
cavityFillingJCheckBox.setText( "Cavity Filling" );
contentPane.add( cavityFillingJCheckBox );
// set up cavityFillingPriceJLabel
cavityFillingPriceJLabel = new JLabel();
cavityFillingPriceJLabel.setBounds( 211, 159, 38, 24 );
cavityFillingPriceJLabel.setText( "$150" );
cavityFillingPriceJLabel.setHorizontalAlignment(
JLabel.RIGHT );
contentPane.add( cavityFillingPriceJLabel );
// set up xRayJCheckBox
xRayJCheckBox = new JCheckBox();
xRayJCheckBox.setBounds( 16, 206, 122, 24 );
xRayJCheckBox.setText( "X-Ray" );
contentPane.add( xRayJCheckBox );
// set up xRayPriceJLabel
xRayPriceJLabel = new JLabel();
xRayPriceJLabel.setBounds( 211, 206, 38, 24 );
xRayPriceJLabel.setText( "$85" );
xRayPriceJLabel.setHorizontalAlignment( JLabel.RIGHT );
contentPane.add( xRayPriceJLabel );
// set up totalJLabel
totalJLabel = new JLabel();
totalJLabel.setBounds( 144, 256, 41, 21 );
totalJLabel.setText( "Total:" );
contentPane.add( totalJLabel );
// set up totalJTextField
totalJTextField = new JTextField();
totalJTextField.setBounds( 192, 256, 56, 21 );
totalJTextField.setEditable( false );
totalJTextField.setHorizontalAlignment( JTextField.CENTER );
contentPane.add( totalJTextField );
// set up calculateJButton
calculateJButton = new JButton();
calculateJButton.setBounds( 155, 296, 94, 24 );
calculateJButton.setText( "Calculate" );
contentPane.add( calculateJButton );
calculateJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when user clicks calculateJButton
public void actionPerformed( ActionEvent event )
{
calculateJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set properties of application's window
setTitle( "Dental Payment" ); // set title bar string
setSize( 272, 364 ); // set window size
setVisible( true ); // display window
} // end method createUserInterface
// calculate cost of patient's visit
private void calculateJButtonActionPerformed( ActionEvent event )
{
// get patient's name
String patient = patientNameJTextField.getText();
// display error message if no name entered or
// no JCheckBox is selected
if ( ( patient.equals( "" ) ) ||
( !cleaningJCheckBox.isSelected() &&
!cavityFillingJCheckBox.isSelected() &&
!xRayJCheckBox.isSelected() ) )
{
// display error message
JOptionPane.showMessageDialog( null,
"Please enter a name and check at least one item.",
"Missing Information", JOptionPane.ERROR_MESSAGE );
}
else // otherwise, do calculations
{
double total = 0.0; // sum of all services provided
// if patient had a cleaning
if ( cleaningJCheckBox.isSelected() )
{
total += 35; // add 35 to total
}
// if patient had cavity filled
if ( cavityFillingJCheckBox.isSelected() )
{
total += 150; // add 150 to total
}
// if patient had x-ray taken
if ( xRayJCheckBox.isSelected() )
{
total += 85; // add 85 to total
}
// specify display format
DecimalFormat dollars = new DecimalFormat( "$0.00" );
// display total
totalJTextField.setText( dollars.format( total ) );
} // end else
} // end method calculateJButtonActionPerformed
// main method
public static void main( String[] args )
{
DentalPayment application = new DentalPayment();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} // end method main
} // end class DentalPayment
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.