?? carinsurance.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CarInsurance extends JFrame {
private String output;
private int age,NumOfTrafficTickets;
private double milesDrivingPerDay;
private boolean owningSportCar,drivingUnderAlcoholInfluence;
private String gender ,carColor;
private JLabel age1 ,NumOfTrafficTickets1 ,milesDrivingPerDay1,owningSportCar1,drivingUnderAlcoholInfluence1,gender1 ,carColor1 ;
private JTextField ageTextField, NumOfTrafficTicketsTextField,milesDrivingPerDayTextField,owningSportCarTextField,drivingUnderAlcoholInfluenceTextField,genderTextField,carColorTextField;
private JButton exitButton ,insuranceButton;
private JTextArea textArea;
public CarInsurance() {
super("Car Insurance");
// Create an instance of inner class ActionEventHandler
ActionEventHandler handler = new ActionEventHandler();
// set up GUI
Container container = getContentPane();
container.setLayout( new FlowLayout() );
// set up Label Fields and Text Fields for number1
age1= new JLabel( "What is your age?" );
ageTextField= new JTextField( 10 );
ageTextField.addActionListener( handler );
container.add(age1 );
container.add( ageTextField );
// for number2
NumOfTrafficTickets1= new JLabel( "Are you male or female?" );
NumOfTrafficTicketsTextField= new JTextField( 10 );
NumOfTrafficTicketsTextField.addActionListener( handler );
container.add( NumOfTrafficTickets1 );
container.add( NumOfTrafficTicketsTextField );
// for sum
milesDrivingPerDay1= new JLabel( "On average, how many miles do you drive per day?" );
milesDrivingPerDayTextField= new JTextField( 10 );
milesDrivingPerDayTextField.addActionListener( handler );
container.add( milesDrivingPerDay1);
container.add( milesDrivingPerDayTextField );
// set up Add Button
owningSportCar1= new JLabel( "Do you own a sport car?" );
owningSportCarTextField= new JTextField( 10 );
owningSportCarTextField.addActionListener( handler );
container.add( owningSportCar1);
container.add( owningSportCarTextField );
////////////////////////////////////
drivingUnderAlcoholInfluence1= new JLabel( "Have you ever been convicted of driving under the influence of alcohol?" );
drivingUnderAlcoholInfluenceTextField= new JTextField( 10 );
drivingUnderAlcoholInfluenceTextField.addActionListener( handler );
container.add( drivingUnderAlcoholInfluence1);
container.add( drivingUnderAlcoholInfluenceTextField );
//////////////////////////////////////
gender1= new JLabel( "Do you own a sport car?" );
genderTextField= new JTextField( 10 );
genderTextField.addActionListener( handler );
container.add( gender1);
container.add( genderTextField );
//////////////////////////////////////
carColor1= new JLabel( "What is the color of your car?" );
carColorTextField= new JTextField( 10 );
carColorTextField.addActionListener( handler );
container.add( carColor1);
container.add( carColorTextField );
//////////////////////////////////////
insuranceButton = new JButton( "Insurance price" );
insuranceButton.addActionListener( handler );
container.add( insuranceButton);
// set up Exit Button
exitButton = new JButton( "Exit" );
exitButton.addActionListener( handler );
container.add( exitButton );
}// end constructor
// Display The Sum of the Two Numbers in displayfield
public void displayAddition() {
textArea.setText(output);
}// end display method
private class ActionEventHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
// process Text Fields: number1 and number2 as well as Add Button events
if (event.getSource() == ageTextField)
age = Integer.parseInt(event.getActionCommand());
else if (event.getSource() == NumOfTrafficTicketsTextField)
NumOfTrafficTickets = Integer.parseInt(event.getActionCommand());
else if (event.getSource() == milesDrivingPerDayTextField)
milesDrivingPerDay= Integer.parseInt(event.getActionCommand());
else if (event.getSource() == owningSportCarTextField)
owningSportCar = Boolean.parseBoolean(event.getActionCommand());
else if (event.getSource() == drivingUnderAlcoholInfluenceTextField)
drivingUnderAlcoholInfluence = Boolean.parseBoolean(event.getActionCommand());
else if (event.getSource() == genderTextField)
gender = (event.getActionCommand());
else if (event.getSource() == carColorTextField)
carColor = event.getActionCommand();
else if (event.getSource() ==insuranceButton ){
output = age+NumOfTrafficTickets+"" ;
textArea.setText(output);
}
else System.exit(0);
}
}// end inner class ActionEventHandler
} // end Class GUIAddition
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -