Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,511 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,927 people online right now. Registration is fast and FREE... Join Now!




Centering buttons in a frame

 

Centering buttons in a frame

speedster545

22 Jun, 2009 - 02:52 PM
Post #1

New D.I.C Head
*

Joined: 9 Jun, 2009
Posts: 9


My Contributions
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.

User is offlineProfile CardPM
+Quote Post


puttyman

RE: Centering Buttons In A Frame

22 Jun, 2009 - 05:22 PM
Post #2

New D.I.C Head
*

Joined: 25 Sep, 2008
Posts: 14



Thanked: 1 times
My Contributions
see comments in code below.

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");
                // The correct design is to add ur buttons to a jpanel then add the jpanel to ur frame
                final JPanel jp = new JPanel();
                
                // To center use a layout manager, here using GridLayout ,
                // there are many of them, FlowLayout, GridBagLayout, BorderLayout
                jp.setLayout(new GridLayout(4,1));
              
                jp.add(button1);
                jp.add(button2);
                jp.add(button3);
                jp.add(label);
                frame.add(jp);
                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);
      
        }
}


User is offlineProfile CardPM
+Quote Post

pbl

RE: Centering Buttons In A Frame

22 Jun, 2009 - 05:30 PM
Post #3

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
put them in an Horizontal Box and pad it with Box.horizontalGlue()
User is offlineProfile CardPM
+Quote Post

toshiro

RE: Centering Buttons In A Frame

1 Jul, 2009 - 04:16 PM
Post #4

New D.I.C Head
Group Icon

Joined: 27 Jun, 2009
Posts: 11



Thanked: 1 times
Dream Kudos: 25
My Contributions
I recommend as follows:

CODE

JPanel panel = new JPanel();
panel.setSize();
panel.setPreferredSize(panel.getSize());
panel.setLayoutManager(new FlowLayout());
//add your JButtons
panel.add(button, x, y);

This direct corr, along with a flow layout is a pretty stable combo, to refresh the screen, use panel.validate()

This post has been edited by toshiro: 1 Jul, 2009 - 04:16 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 05:12AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month