i'm just making a simple program using swing to make a panel that has a couple of buttons on it. I'd like to center the buttons in the frame but I am having trouble trying to figure out how to do it. so far I'm using just a box layout to show my buttons, what kind of layout would i need to use to center the buttons in the frame?
this is just a simple program to use buttons in a GUI interface, here's my code so far (all this program does is have buttons that change the color of the background of the frame when they are pressed):
CODE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class BackgroundChange
{
public static void main(String[] arg)
{
String color = "none";
final JFrame frame = new JFrame();
final JLabel label = new JLabel(color);
final JButton button1 = new JButton("Red");
final JButton button2 = new JButton("Blue");
final JButton button3 = new JButton("Green");
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.getContentPane().add(button3);
frame.getContentPane().add(label);
label.setForeground(Color.BLACK);
ActionListener a = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
label.setText("Red");
frame.getContentPane().setBackground(Color.RED);
}
};
ActionListener b = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
label.setText("Blue");
frame.getContentPane().setBackground(Color.BLUE);
}
};
ActionListener c = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
label.setText("Green");
frame.getContentPane().setBackground(Color.GREEN);
}
};
button1.addActionListener(a);
button2.addActionListener(b);
button3.addActionListener(c);
frame.getContentPane().setLayout(
new BoxLayout(
frame.getContentPane(),
BoxLayout.Y_AXIS));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
any help with the centering of the frames would be appreciated, right now they all are aligned along the left side of the frame in separate lines.