Java Reference
In-Depth Information
"1", "2", "3", "=", "+", "-", "*", "/"
);
JTextField resultBox = new JTextField("*** BATTERY EMPTY ***");
resultBox.setEditable(false); // Prevent user editing
frame.getContentPane().add(buttonPanel, BorderLayout.CENTER);
frame.getContentPane().add(resultBox, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
private static void addButtons(Container contentPane, String... strings) {
for (String label : strings) {
contentPane.add(new JButton(label));
}
}
}
The result will now look like Figure 11-13.
figure 11-13  
How It Works
This is how it works:
1.
This class is similar to the previous one, but now you have specified a GridLayout object as the
layout manager.
2.
In the second version, a new JPanel container is constructed, and its layout manager is set to be
a GridLayout . Then it is added to the center of the JFrame . A JTextField is then added in the
north position. Note the use of the setEditable method to prevent users from typing in characters
in the textbox.
Search WWH ::




Custom Search