Java tutorial
//package com.java2s; import java.awt.Component; import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JRootPane; public class Main { /** * @param okText * @param cancelText * @param listener * @return newly created JPanel with ok and cancel buttons */ public static JPanel createOkCancelPanel(String okText, String cancelText, ActionListener listener) { return createOkCancelPanel(okText, cancelText, listener, null); } /** * @param okText * @param cancelText * @param listener * @param root * @return newly created JPanel with ok and cancel buttons */ public static JPanel createOkCancelPanel(String okText, String cancelText, ActionListener listener, JRootPane root) { JPanel panel = new JPanel(); JButton ok = new JButton(okText); JButton cancel = new JButton(cancelText); panel.add(ok); panel.add(cancel); ok.addActionListener(listener); ok.setActionCommand(okText); if (root != null) root.setDefaultButton(ok); cancel.addActionListener(listener); cancel.setActionCommand(cancelText); return panel; } /** * @param gx * @param gy * @param gw * @param gh * @param wx * @param wy * @param what * @param where * @param gbc * @param gridbag */ public static void add(int gx, int gy, int gw, int gh, int wx, int wy, Component what, Container where, GridBagConstraints gbc, GridBagLayout gridbag) { gbc.gridx = gx; gbc.gridy = gy; gbc.gridwidth = gw; gbc.gridheight = gh; gbc.weightx = wx; gbc.weighty = wy; gridbag.setConstraints(what, gbc); where.add(what); } }