Results 1 to 6 of 6
- 05-08-2012, 08:58 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 41
- Rep Power
- 0
component positioning spacing problem
can anyone help me how I can position those buttons so that they can have spaces among them?
I have the following code with GridBagLayout. where I have Five Buttons. I tried to positioned them so that they have spaces among each other. But I couldnt add any spaces among them. Please help
Java Code:package test.gui; import javax.swing.JPanel; import java.awt.GridBagLayout; import javax.swing.JButton; import java.awt.GridBagConstraints; import java.awt.Insets; import javax.swing.ImageIcon; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class layer1 extends JPanel { public layer1() { GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; setLayout(gridBagLayout); JButton btnNewButton = new JButton("button1"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { btnActionPerformed (e); } }); btnNewButton.setIcon(new ImageIcon("Images/pic1.gif")); GridBagConstraints gbc_btnNewButton = new GridBagConstraints(); gbc_btnNewButton.insets = new Insets(0, 0, 5, 5); gbc_btnNewButton.gridx = 13; gbc_btnNewButton.gridy = 3; add(btnNewButton, gbc_btnNewButton); JButton btnNewButton_1 = new JButton("button2"); btnNewButton_1.setIcon(new ImageIcon("Images/pic2.gif")); GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints(); gbc_btnNewButton_1.insets = new Insets(0, 0, 5, 5); gbc_btnNewButton_1.gridx = 17; gbc_btnNewButton_1.gridy = 3; add(btnNewButton_1, gbc_btnNewButton_1); JButton btnNewButton_4 = new JButton("button middle"); btnNewButton_4.setIcon(new ImageIcon("Images/pic3.gif")); GridBagConstraints gbc_btnNewButton_4 = new GridBagConstraints(); gbc_btnNewButton_4.insets = new Insets(0, 0, 5, 5); gbc_btnNewButton_4.gridx = 15; gbc_btnNewButton_4.gridy = 9; add(btnNewButton_4, gbc_btnNewButton_4); JButton btnNewButton_2 = new JButton("button3"); btnNewButton_2.setIcon(new ImageIcon("Images/pic4.gif")); GridBagConstraints gbc_btnNewButton_2 = new GridBagConstraints(); gbc_btnNewButton_2.insets = new Insets(0, 0, 5, 5); gbc_btnNewButton_2.gridx = 13; gbc_btnNewButton_2.gridy = 12; add(btnNewButton_2, gbc_btnNewButton_2); JButton btnNewButton_3 = new JButton("button4"); btnNewButton_3.setIcon(new ImageIcon("Images/pic5.gif")); GridBagConstraints gbc_btnNewButton_3 = new GridBagConstraints(); gbc_btnNewButton_3.insets = new Insets(0, 0, 5, 5); gbc_btnNewButton_3.gridx = 17; gbc_btnNewButton_3.gridy = 12; add(btnNewButton_3, gbc_btnNewButton_3); } private void btnActionPerformed (ActionEvent evt) { removeAll (); // layerTwo createLayerTwo = new layerTwo (); test createLayerTwo = new test (); add (createLayerTwo, new GridBagLayout ()); validate (); } }Java Code:package test.gui; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagLayout; import javax.swing.JTabbedPane; import javax.swing.border.CompoundBorder; import javax.swing.border.LineBorder; import java.awt.GridBagConstraints; //public class tabs extends JPanel { public class tabs extends JFrame { JFrame myFrame = null; public static void main(String [] args) { new tabs(); } public tabs() { GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWidths = new int[]{0, 0}; gridBagLayout.rowHeights = new int[]{0, 0}; gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE}; gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE}; setLayout(gridBagLayout); JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); GridBagConstraints gbc_tabbedPane = new GridBagConstraints(); gbc_tabbedPane.fill = GridBagConstraints.BOTH; gbc_tabbedPane.gridx = 0; gbc_tabbedPane.gridy = 0; add(tabbedPane, gbc_tabbedPane); JPanel panelRoom1 = new JPanel(); panelRoom1.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 2), new CompoundBorder())); tabbedPane.addTab("tab1", null, panelRoom1, null); layer1 createLayer1 = new layer1 (); panelRoom1.add(createLayer1); JPanel panel_1 = new JPanel(); tabbedPane.addTab("tab2", null, panel_1, null); test createLayerTwo = new test (); panel_1.add(createLayerTwo); JPanel panel_2 = new JPanel(); tabbedPane.addTab("tab3", null, panel_2, null); setPreferredSize(new Dimension(1378,768)); pack(); setVisible(true); } }
- 05-08-2012, 09:43 PM #2
Re: component positioning spacing problem
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-09-2012, 07:35 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 41
- Rep Power
- 0
Re: component positioning spacing problem
someone please help
- 05-09-2012, 04:18 PM #4
Re: component positioning spacing problem
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. Your problem is achieving a GridBagLayout with spaces between buttons, so post code that makes a GUI with only 2 to 4 buttons in a GBL.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-09-2012, 05:12 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 41
- Rep Power
- 0
Re: component positioning spacing problem
ok I modified my code and made the code with two buttons. Please help me figure out how I can add horizontal and vertical spaces. Here is my new code.
Java Code:package test.gui; import javax.swing.JPanel; import java.awt.GridBagLayout; import javax.swing.JButton; import java.awt.GridBagConstraints; import java.awt.Insets; import javax.swing.ImageIcon; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class layer1 extends JPanel { public layer1() { GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; setLayout(gridBagLayout); JButton btnNewButton_2 = new JButton("button3"); btnNewButton_2.setIcon(new ImageIcon("Images/pic4.gif")); GridBagConstraints gbc_btnNewButton_2 = new GridBagConstraints(); gbc_btnNewButton_2.insets = new Insets(0, 0, 5, 5); gbc_btnNewButton_2.gridx = 13; gbc_btnNewButton_2.gridy = 12; add(btnNewButton_2, gbc_btnNewButton_2); JButton btnNewButton_3 = new JButton("button4"); btnNewButton_3.setIcon(new ImageIcon("Images/pic5.gif")); GridBagConstraints gbc_btnNewButton_3 = new GridBagConstraints(); gbc_btnNewButton_3.insets = new Insets(0, 0, 5, 5); gbc_btnNewButton_3.gridx = 17; gbc_btnNewButton_3.gridy = 12; add(btnNewButton_3, gbc_btnNewButton_3); } }Java Code:package test.gui; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagLayout; import javax.swing.JTabbedPane; import javax.swing.border.CompoundBorder; import javax.swing.border.LineBorder; import java.awt.GridBagConstraints; public class tabs extends JFrame { JFrame myFrame = null; public static void main(String [] args) { new tabs(); } public tabs() { GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWidths = new int[]{0, 0}; gridBagLayout.rowHeights = new int[]{0, 0}; gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE}; gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE}; setLayout(gridBagLayout); JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); GridBagConstraints gbc_tabbedPane = new GridBagConstraints(); gbc_tabbedPane.fill = GridBagConstraints.BOTH; gbc_tabbedPane.gridx = 0; gbc_tabbedPane.gridy = 0; add(tabbedPane, gbc_tabbedPane); JPanel panelRoom1 = new JPanel(); panelRoom1.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 2), new CompoundBorder())); tabbedPane.addTab("tab1", null, panelRoom1, null); layer1 createLayer1 = new layer1 (); panelRoom1.add(createLayer1); JPanel panel_1 = new JPanel(); tabbedPane.addTab("tab2", null, panel_1, null); layer1 createLayer1 = new layer1 (); panel_1.add(createLayer1); JPanel panel_2 = new JPanel(); tabbedPane.addTab("tab3", null, panel_2, null); setPreferredSize(new Dimension(1378,768)); pack(); setVisible(true); } }
- 05-09-2012, 07:36 PM #6
Similar Threads
-
A componenet positioning problem
By apprerntice in forum AWT / SwingReplies: 10Last Post: 07-22-2011, 07:53 PM -
[Q] Spacing Help
By iriscience in forum New To JavaReplies: 7Last Post: 02-08-2011, 01:44 AM -
problem positioning a JPanel in a JScrollPane
By gib65 in forum AWT / SwingReplies: 4Last Post: 08-02-2010, 10:34 AM -
Spacing/Alignment problem with java 2D printing
By myselfmayur in forum Java 2DReplies: 2Last Post: 06-25-2010, 03:37 PM -
Problem with margins/spacing
By joseche in forum AWT / SwingReplies: 2Last Post: 12-26-2008, 08:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks