import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Point; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.UIManager; /** * Demonstrates an application of GridBagLayout to produce a Rectangle shape * properties editor. */ public class RectangleInfoFrame extends JFrame { public static final Insets SURROUND_4 = new Insets(4, 4, 4, 4); private JButton fillColorButton; private JButton outlineColorButton; private JButton cancelButton; private JButton okButton; private JTextField xTextField; private JTextField yTextField; private JTextField widthTextField; private JTextField heightTextField; public RectangleInfoFrame() { super("Rectangle Info"); JPanel cp = new JPanel(new GridBagLayout()); setContentPane(cp); // Create components JLabel xCaption = new JLabel("X:"); JLabel yCaption = new JLabel("Y:"); JLabel widthCaption = new JLabel("Width:"); JLabel heightCaption = new JLabel("Height:"); JLabel colorsCaption = new JLabel("Colors:"); this.xTextField = new JTextField(10); this.yTextField = new JTextField(10); this.widthTextField = new JTextField(10); this.heightTextField = new JTextField(10); this.outlineColorButton = new JButton("Outline"); this.fillColorButton = new JButton("Fill"); this.cancelButton = new JButton("Cancel"); this.okButton = new JButton("OK"); // Set up x,y row GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = SURROUND_4; gbc.gridy = 1; gbc.gridx = 1; gbc.anchor = GridBagConstraints.EAST; gbc.fill = GridBagConstraints.NONE; cp.add(xCaption, gbc); gbc.gridx = 3; cp.add(yCaption, gbc); gbc.anchor = GridBagConstraints.WEST; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 2; cp.add(this.xTextField, gbc); gbc.gridx = 4; cp.add(this.yTextField, gbc); // Set up width row gbc.gridy++; gbc.gridx = 1; gbc.gridwidth = 1; gbc.anchor = GridBagConstraints.EAST; gbc.fill = GridBagConstraints.NONE; cp.add(widthCaption, gbc); gbc.anchor = GridBagConstraints.WEST; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 2; gbc.gridwidth = 3; cp.add(this.widthTextField, gbc); // Set up height row gbc.gridy++; gbc.gridx = 1; gbc.gridwidth = 1; gbc.anchor = GridBagConstraints.EAST; gbc.fill = GridBagConstraints.NONE; cp.add(heightCaption, gbc); gbc.anchor = GridBagConstraints.WEST; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 2; gbc.gridwidth = 3; cp.add(this.heightTextField, gbc); // Set up colors row JPanel colorsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); colorsPanel.add(this.outlineColorButton); colorsPanel.add(this.fillColorButton); gbc.gridy++; gbc.gridx = 1; gbc.gridwidth = 1; gbc.anchor = GridBagConstraints.EAST; gbc.fill = GridBagConstraints.NONE; cp.add(colorsCaption, gbc); gbc.anchor = GridBagConstraints.WEST; gbc.gridx = 2; gbc.gridwidth = 3; gbc.insets = new Insets(0, 0, 0, 0); cp.add(colorsPanel, gbc); // Set up exit buttons row JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); buttonsPanel.add(this.cancelButton); buttonsPanel.add(this.okButton); gbc.gridy++; gbc.gridx = 1; gbc.gridwidth = 4; gbc.anchor = GridBagConstraints.EAST; cp.add(buttonsPanel, gbc); getRootPane().setDefaultButton(this.okButton); pack(); } public void center() { Dimension screenSize = getToolkit().getScreenSize(); Dimension frameSize = getSize(); Point location = new Point((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); setLocation(location); } public static void main(String[] args) throws Exception { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); JFrame.setDefaultLookAndFeelDecorated(true); RectangleInfoFrame f = new RectangleInfoFrame(); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.center(); f.setVisible(true); } }