I can get the buttons to either be 5x1 or 1x5, but I can't make it 5x5, Hell I can't even add another button without the alignment going AWAL.
Here's my code;
package layout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Buttons extends JFrame {
JButton applyButton = new JButton("Exit");
GridLayout experimentLayout = new GridLayout(1,10);
public Buttons(String name) {
super(name);
setResizable(false);
}
public void addComponentsToPane(final Container pane) {
final JPanel compsToExperiment = new JPanel();
compsToExperiment.setLayout(experimentLayout);
JPanel controls = new JPanel();
//Set up components preferred size
JButton b = new JButton("Just fake button");
Dimension buttonSize = b.getPreferredSize();
//Add buttons to experiment with Grid Layout
compsToExperiment.add(new JButton("Category 1"));
compsToExperiment.add(new JButton("Category 2"));
compsToExperiment.add(new JButton("Category 3"));
compsToExperiment.add(new JButton("Category 4"));
compsToExperiment.add(new JButton("Category 5"));
//Process the Apply gaps button press
applyButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
experimentLayout.layoutContainer(compsToExperiment);
}
});
pane.add(compsToExperiment, BorderLayout.NORTH);
pane.add(new JSeparator(), BorderLayout.CENTER);
pane.add(controls, BorderLayout.SOUTH);
}
private static void createAndShowGUI() {
//Create and set up the window.
Buttons frame = new Buttons("Buttons");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
frame.addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
/* Use an appropriate Look and Feel */
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
UIManager.put("swing.boldMetal", Boolean.FALSE);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}















