954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help moving buttons in center of JFrame

I'm in the process of creating a program that would change a background color by pressing a button. Now, I already have everything set up. The background image is just an image of a rainbow, and when a button is clicked, it would change the whole background color. The thing is, I do not want the buttons at the top...I want the buttons to be in the center (see the images I included...2nd image is what I would like - I just edited it with Windows Paint). Is there an easy way to do this? Thanks.

import javax.swing.*;

import java.awt.*;

public class Buttons extends JPanel{
	
	public Buttons(){
		setOpaque(false);
		setLayout(new FlowLayout());
	}
	
	public void paint(Graphics g){
		Image a = Toolkit.getDefaultToolkit().getImage("C:\\Documents and Settings\\adamh\\My Documents\\My Pictures\\rainbow.jpg");
		g.drawImage(a,0,0, getSize().width,getSize().height,this);
		super.paint(g);
	}
	
	public static void main(String[] args){
		JFrame myFrame = new JFrame("Rainbow Color");
		
		JButton redButton =  new JButton("Red");
		JButton blueButton =  new JButton("Blue");
		JButton yellowButton =  new JButton("Yellow");
		
		Buttons b = new Buttons();
		b.add(redButton);
		b.add(blueButton);
		b.add(yellowButton);
		myFrame.add(b);
		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		myFrame.setSize(350,700);
		myFrame.setVisible(true);
	}
	
        //more code to go here for actionPerformed...

}
Attachments rainbowScreenshot.JPG 26.45KB rainbowScreenshot2.JPG 26.71KB
jabanista
Newbie Poster
2 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

http://download.oracle.com/javase/tutorial/uiswing/layout/index.html

that's job for two JPanels


Thanks for the input...it worked!!!

jabanista
Newbie Poster
2 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: