Results 1 to 3 of 3
Thread: GridBagConstraints
- 07-21-2008, 01:53 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 10
- Rep Power
- 0
GridBagConstraints
Hi all
i have a problem to set button position.How can i set the button position.When i am going to add two button then showing two space between both button.Plz help me..Here is code
Java Code:import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.*; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; import javax.swing.JRootPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class GridBagLayoutDemo { private static JLabel jlbName; private static JLabel jlbEmail; private static JLabel jlbProductKey; private static JTextField jtfName; private static JTextField jtfEmail; private static JTextField jtfProductKey; private static JTextField jtfState; private static JTextField jtfCity; private static Container pane = null; private static JButton jbtLogin; private static JButton jbtExit; public static void addComponentsToPane(JFrame frame) { pane = frame.getContentPane(); ButtonGroup group = new ButtonGroup(); GridBagConstraints gBC = new GridBagConstraints(); gBC.fill = GridBagConstraints.HORIZONTAL; gBC.insets = new Insets(5, 5, 0, 5); jlbName = new JLabel("Name : "); gBC.weightx = 0.5; gBC.gridx = 0; gBC.gridy = 0; pane.add(jlbName, gBC); jtfName = new JTextField(20); gBC.weightx = 0.5; gBC.gridx = 1; gBC.gridy = 0; pane.add(jtfName, gBC); jlbEmail = new JLabel("EmailID : "); gBC.weightx = 0.5; gBC.gridx = 0; gBC.gridy = 1; pane.add(jlbEmail, gBC); jtfEmail = new JTextField(20); gBC.weightx = 0.5; gBC.gridx = 1; gBC.gridy = 1; gBC.fill = GridBagConstraints.NONE; pane.add(jtfEmail, gBC); gBC.fill = GridBagConstraints.HORIZONTAL; jlbProductKey = new JLabel("Product Key : "); gBC.gridx = 0; gBC.gridy = 2; pane.add(jlbProductKey, gBC); jtfProductKey = new JTextField(20); gBC.gridx = 1; gBC.gridy = 2; gBC.fill = GridBagConstraints.NONE; pane.add(jtfProductKey, gBC); jbtLogin = new JButton("Login"); gBC.gridx = 1; gBC.gridy = 6; pane.add(jbtLogin, gBC); gBC.fill = GridBagConstraints.BOTH; jbtLogin.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { login(); } }); jbtExit = new JButton("Exit"); gBC.gridx = 2; gBC.gridy = 6; pane.add(jbtExit, gBC); } private static void login() { System.out.println("Hello"); } private static void createAndShowGUI() { JFrame frame = new JFrame("GridBagLayout Demo"); // frame.setTitle("Hello"); frame.setUndecorated(false); frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new GridBagLayout()); // Set up the content pane. frame.setSize(200,200); addComponentsToPane(frame); frame.setResizable(false); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
- 07-21-2008, 07:03 PM #2
Java Code:import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.*; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; import javax.swing.JRootPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class GBDemo { private static JLabel jlbName; private static JLabel jlbEmail; private static JLabel jlbProductKey; private static JTextField jtfName; private static JTextField jtfEmail; private static JTextField jtfProductKey; private static JTextField jtfState; private static JTextField jtfCity; private static Container pane = null; private static JButton jbtLogin; private static JButton jbtExit; public static void addComponentsToPane(JFrame frame) { pane = frame.getContentPane(); ButtonGroup group = new ButtonGroup(); GridBagConstraints gBC = new GridBagConstraints(); // gBC.fill = GridBagConstraints.HORIZONTAL; gBC.insets = new Insets(5, 5, 0, 5); gBC.anchor = GridBagConstraints.WEST; jlbName = new JLabel("Name : "); // gBC.weightx = 0.5; gBC.gridx = 0; gBC.gridy = 0; gBC.gridwidth = 1; pane.add(jlbName, gBC); jtfName = new JTextField(20); // gBC.weightx = 0.5; gBC.gridx = 1; gBC.gridy = 0; gBC.gridwidth = 2; pane.add(jtfName, gBC); jlbEmail = new JLabel("EmailID : "); // gBC.weightx = 0.5; gBC.gridx = 0; gBC.gridy = 1; gBC.gridwidth = 1; pane.add(jlbEmail, gBC); jtfEmail = new JTextField(20); // gBC.weightx = 0.5; gBC.gridx = 1; gBC.gridy = 1; gBC.gridwidth = 2; // gBC.fill = GridBagConstraints.NONE; pane.add(jtfEmail, gBC); // gBC.fill = GridBagConstraints.HORIZONTAL; jlbProductKey = new JLabel("Product Key : "); gBC.gridx = 0; gBC.gridy = 2; gBC.gridwidth = 1; pane.add(jlbProductKey, gBC); jtfProductKey = new JTextField(20); gBC.gridx = 1; gBC.gridy = 2; gBC.gridwidth = 2; // gBC.fill = GridBagConstraints.NONE; pane.add(jtfProductKey, gBC); jbtLogin = new JButton("Login"); gBC.anchor = GridBagConstraints.CENTER; gBC.insets.bottom = 2; gBC.weightx = 1.0; gBC.gridwidth = 1; gBC.gridx = 1; // gBC.gridy = 6; // you have only four rows gBC.gridy = 3; pane.add(jbtLogin, gBC); // gBC.fill = GridBagConstraints.BOTH; jbtLogin.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Hello"); } }); jbtExit = new JButton("Exit"); gBC.gridx = 2; // gBC.gridy = 6; gBC.gridy = 3; pane.add(jbtExit, gBC); } private static void createAndShowGUI() { JFrame frame = new JFrame("GridBagLayout Demo"); frame.setUndecorated(false); frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new GridBagLayout()); frame.setSize(200,200); addComponentsToPane(frame); frame.setResizable(false); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
- 07-21-2008, 09:08 PM #3
Member
- Join Date
- Jul 2008
- Posts
- 67
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks