Java Reference
In-Depth Information
private static void addButtons(Container contentPane, String ... strings) {
for (String label : strings) {
contentPane.add(new JButton(label));
}
}
}
2.
Run the project from Eclipse. You should see the window shown in Figure 11-12.
figure 11-12  
3.
The buttons don't do anything (yet) and are pretty useless for now, but you can see already that
you will need to add a textbox for any calculations. It is probably also apparent that it's not good
to cram this into a single cell. It would be better to span this text the width of a whole row. Why
not put your buttons in a JPanel and the textfield in the north position of the BorderLayout lay-
out manager? Change your class to look like this:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GridLayoutFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("GridLayout frame");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
addButtons(buttonPanel,
"7", "8", "9", "0", "4", "5", "6", "C",
Search WWH ::




Custom Search