Results 1 to 3 of 3
Thread: [AWT] GridBagLayout Help.
- 05-23-2010, 04:42 PM #1
Member
- Join Date
- May 2010
- Posts
- 12
- Rep Power
- 0
[AWT] GridBagLayout Help.
Hello!!
I'm new in the forum and also in Java.
I read this tutorial of GridBagLayout (How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)) but I can't make it work.
I have a Frame with 3 buttons and 2 canvas. (I'm learning how to use canvas, thats why I have two of them). But the layout doesn't look as I wanto to...
Someone can help me and explain me where is the problem in my code???
I also attach how the layout looks in my "Netbeans's gridbaglayout editor" and how it looks when I run the app!
Java Code:import java.awt.Graphics; public class NewFrame extends java.awt.Frame { /** Creates new form NewFrame */ public NewFrame() { initComponents(); } // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; canvas1 = new java.awt.Canvas(); button1 = new java.awt.Button(); button2 = new java.awt.Button(); canvas2 = new java.awt.Canvas(); button3 = new java.awt.Button(); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); setLayout(new java.awt.GridBagLayout()); canvas1.setBackground(new java.awt.Color(255, 255, 204)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 2; gridBagConstraints.ipadx = 244; gridBagConstraints.ipady = 213; gridBagConstraints.weighty = 1.0; gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 7); add(canvas1, gridBagConstraints); button1.setLabel("Mover"); button1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { button1ActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.ipadx = 4; gridBagConstraints.ipady = 5; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(6, 6, 6, 6); add(button1, gridBagConstraints); button2.setLabel("Limpiar"); button2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { button2ActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.ipadx = 4; gridBagConstraints.ipady = 5; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(6, 6, 6, 6); add(button2, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 0; gridBagConstraints.ipadx = 244; gridBagConstraints.ipady = 213; gridBagConstraints.weighty = 1.0; gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 7); add(canvas2, gridBagConstraints); button3.setLabel("button3"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 1; gridBagConstraints.ipadx = 4; gridBagConstraints.ipady = 5; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(6, 6, 6, 6); add(button3, gridBagConstraints); pack(); }// </editor-fold> private void exitForm(java.awt.event.WindowEvent evt) { System.exit(0); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewFrame().setVisible(true); } }); } // Variables declaration - do not modify private java.awt.Button button1; private java.awt.Button button2; private java.awt.Button button3; private java.awt.Canvas canvas1; private java.awt.Canvas canvas2; // End of variables declaration }
Thanks a lot!!!
-
I'm not sure that I'd use GridBagLayout for a layout such as this as it may be simpler to create a main Container that uses GridLayout(1, 0) to hold everything, and to add two Containers to this, one on the left and one on the right that uses BorderLayout with the Canvas added BorderLayout.CENTER and another GridLayout(1,0) using Container added to the bottom (BorderLayout.SOUTH) that holds the buttons.
for e.g.,
Java Code:import java.awt.*; import javax.swing.*; public class LayoutFu1 { private JPanel mainPanel = new JPanel(); public LayoutFu1() { mainPanel.setLayout(new GridLayout(1, 0)); mainPanel.add(createGridPanel(new String[] {"Button 1", "Button 2"})); mainPanel.add(createGridPanel(new String[] {"Button 3"})); } private JPanel createGridPanel(String[] buttonNames) { JPanel btnPanel = new JPanel(new GridLayout(1, 0)); for (String btnName : buttonNames) { JButton btn = new JButton(btnName); JPanel tempPanel = new JPanel(); tempPanel.add(btn); btnPanel.add(tempPanel); } JLabel label = new JLabel("Canvas", JLabel.CENTER); Dimension labelSize = new Dimension(300, 300); label.setPreferredSize(labelSize ); label.setBorder(BorderFactory.createLineBorder(Color.black)); JPanel panel = new JPanel(new BorderLayout(5, 5)); int eb = 8; panel.setBorder(BorderFactory.createEmptyBorder(eb , eb, eb, eb)); panel.add(label, BorderLayout.CENTER); panel.add(btnPanel, BorderLayout.SOUTH); return panel; } public JPanel getMainPanel() { return mainPanel; } private static void createAndShowUI() { JFrame frame = new JFrame("LayoutFu1"); frame.getContentPane().add(new LayoutFu1().getMainPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
I'd also not use AWT but instead would use Swing unless there are pressing reasons for this (do you have any?).
- 05-23-2010, 08:54 PM #3
Member
- Join Date
- May 2010
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
help with gridbaglayout
By robertbob in forum AWT / SwingReplies: 5Last Post: 05-18-2010, 04:14 AM -
GridBagLayout
By Moncleared in forum New To JavaReplies: 1Last Post: 10-18-2009, 10:12 PM -
GridBagLayout
By carderne in forum New To JavaReplies: 8Last Post: 01-25-2009, 02:06 PM -
GridBagLayout
By MuslimCoder in forum New To JavaReplies: 1Last Post: 01-15-2009, 08:54 PM -
abt gridbaglayout
By pinky in forum AWT / SwingReplies: 1Last Post: 12-15-2008, 08:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks