I did, but it didn't leave any spacing around the edge of the button. I then
tried the following with the BoxLayout:
button_panel.add(Box.createHorizontalStrut(10));
button_panel.add(add_class_button);
button_panel.add(Box.createHorizontalStrut(10));
button_panel.add(Box.createVerticalStrut(10));
button_panel.add(Box.createHorizontalStrut(10));
button_panel.add(add_arrow_button);
button_panel.add(Box.createHorizontalStrut(10));
button_panel.add(Box.createVerticalGlue());
It creates a gap too big for my liking vertically between the two buttons.
I've no idea what I have done wrong.
When there is one button on the JPanel, and without stating which layout to
use, it shows up quite nicely with right sort of spacing around the edges of
the button. But if I add another button, it would not add it vertically. I'm
guessing there is a way of adding buttons in the vertical direction, but I
just don't know what it is.
>I did, but it didn't leave any spacing around the edge of the button. I
>then tried the following with the BoxLayout:
[quoted text clipped - 16 lines]
> vertically. I'm guessing there is a way of adding buttons in the vertical
> direction, but I just don't know what it is.
look into GridLayout
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/GridLayout.html
particularly the constructor which includes hgap, vgap
here's a simple demo (4 buttons aligned vertically) with no gaps
import java.awt.*;
import javax.swing.*;
class Testing extends JFrame
{
final int ROWS = 4,COLS = 0;
public Testing()
{
setLocation(400,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(ROWS,COLS));
for(int x = 0; x < ROWS; x++) panel.add(new JButton("Button "+x));
getContentPane().add(panel);
pack();
}
public static void main(String[] args){new Testing().setVisible(true);}
}
same program, with spacing of 10, vertically
import java.awt.*;
import javax.swing.*;
class Testing extends JFrame
{
final int ROWS = 4,COLS = 0;
public Testing()
{
setLocation(400,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(ROWS,COLS,0,10));//<---------10
spacing vertically
for(int x = 0; x < ROWS; x++) panel.add(new JButton("Button "+x));
getContentPane().add(panel);
pack();
}
public static void main(String[] args){new Testing().setVisible(true);}
}
Camford - 07 Sep 2005 00:30 GMT
Is there anyway to stop the buttons from resizing if you resize the main
window?
Michael Dunn - 07 Sep 2005 00:40 GMT
> Is there anyway to stop the buttons from resizing if you resize the main
> window?
import java.awt.*;
import javax.swing.*;
class Testing extends JFrame
{
final int ROWS = 4,COLS = 0;
public Testing()
{
setLocation(400,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel holdingPanel = new JPanel(new GridBagLayout());
JPanel panel = new JPanel(new GridLayout(ROWS,COLS));
for(int x = 0; x < ROWS; x++) panel.add(new JButton("Button "+x));
holdingPanel.add(panel,new GridBagConstraints());
getContentPane().add(holdingPanel);
pack();
}
public static void main(String[] args){new
Testing().setVisible(true);}
}