Java Reference
In-Depth Information
29 computeButton.addActionListener( this );
30
31 // layout
32 JPanel north = new JPanel( new GridLayout(2, 2));
33 north.add( new JLabel("Height: "));
34 north.add(heightField);
35 north.add( new JLabel("Weight: "));
36 north.add(weightField);
37
38 // overall frame
39 frame = new JFrame("BMI");
40 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
41 frame.setLayout( new BorderLayout());
42 frame.add(north, BorderLayout.NORTH);
43 frame.add(bmiLabel, BorderLayout.CENTER);
44 frame.add(computeButton, BorderLayout.SOUTH);
45 frame.pack();
46 frame.setVisible( true );
47 }
48
49 // Handles clicks on Compute button by computing the BMI.
50 public void actionPerformed(ActionEvent event) {
51 // read height/weight info from text fields
52 String heightText = heightField.getText();
53 double height = Double.parseDouble(heightText);
54 String weightText = weightField.getText();
55
double weight = Double.parseDouble(weightText);
56
57 // compute BMI and display it onscreen
58 double bmi = weight / (height * height) * 703;
59 bmiLabel.setText("BMI: " + bmi);
60 }
61 }
Example 2: Credit Card GUI
Credit card numbers contain several pieces of information for performing validity
tests. For example, Visa card numbers always begin with 4, and a valid Visa card
number always passes a digit-sum test known as the Luhn checksum algorithm.
Luhn's algorithm states that if you add the digits of any valid credit card number in
a certain way, the sum will be a multiple of 10. Systems that accept credit cards
perform a Luhn test before they contact the credit card company for final verifica-
tion of the number. This test allows them to filter out fake or mistyped credit card
numbers.
 
Search WWH ::




Custom Search