Java Reference
In-Depth Information
The following class, which still has no event handling, implements the new object-
oriented version of the GUI:
1 // A GUI to compute a person's body mass index (BMI).
2 // Object-oriented version without event handling.
3
4 import java.awt.*;
5 import javax.swing.*;
6
7 public class BmiGui2 {
8 // onscreen components stored as fields
9 private JFrame frame;
10 private JTextField heightField;
11 private JTextField weightField;
12 private JLabel bmiLabel;
13 private JButton computeButton;
14
15 public BmiGui2() {
16 // set up components
17 heightField = new JTextField(5);
18 weightField = new JTextField(5);
19 bmiLabel = new JLabel(
20 "Type your height and weight");
21 computeButton = new JButton("Compute");
22
23 // layout
24 JPanel north = new JPanel( new GridLayout(2, 2));
25 north.add( new JLabel("Height: "));
26 north.add(heightField);
27 north.add( new JLabel("Weight: "));
28 north.add(weightField);
29
30 // overall frame
31 frame = new JFrame("BMI");
32 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
33 frame.setLayout( new BorderLayout());
34 frame.add(north, BorderLayout.NORTH);
35 frame.add(bmiLabel, BorderLayout.CENTER);
36 frame.add(computeButton, BorderLayout.SOUTH);
37 frame.pack();
38 frame.setVisible( true );
39 }
40 }
Search WWH ::




Custom Search