Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 5th, 2015, 07:40 AM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default set all components to be center of the JFrame

I used FormLayout and I want to set all my components(JLabel and JtextField) to be in the center of the JFrame.

I tried but it doesn't work.
Code:
  panel.setAlignmentX(Component.CENTER_ALIGNMENT);
  panel.setAlignmentY(Component.CENTER_ALIGNMENT);
shown below is my piece of code, please help thanks.

Code:
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

@SuppressWarnings("serial")
public class UI extends JFrame {

	private JPanel panel = new JPanel();

	public UI() {
		
		FormLayout layout = new FormLayout(
				"pref, pref, pref, pref", 
				"pref, pref, pref, pref");
		
		panel.setLayout(layout);
		CellConstraints cc = new CellConstraints();

		panel = new JPanel (layout);

		panel.add(new JLabel("L1 "), cc.xy(2, 2));
	    panel.add(new JTextField(15), cc.xy(3, 2));
	    
	    panel.add(new JLabel("L2 "), cc.xy(2, 3));
	    panel.add(new JTextField(15), cc.xy(3, 3));
	   	    
	    panel.setAlignmentX(Component.CENTER_ALIGNMENT);
	    panel.setAlignmentY(Component.CENTER_ALIGNMENT);
		add(panel);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		pack();
		setSize(510,400);
		setLocationRelativeTo(null);
		setResizable(false);
	}

	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				new UI().setVisible(true);

			}
		});

	}
}

Last edited by khunpaen; May 5th, 2015 at 10:18 AM.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
JFrame class jagan7 Java Basics 2 August 15th, 2007 06:11 AM
jframe background darkfreedom Java GUI 0 August 4th, 2006 01:38 PM
help with JFrame.setBackground(...) dzungrockx Java GUI 0 March 15th, 2006 11:19 AM
JDesktopPane and JFrame mudasirw Java GUI 0 June 23rd, 2005 08:03 PM
Promblem in KeyListener on JFrame bamboat_3 Java GUI 1 May 28th, 2005 03:09 AM



All times are GMT -4. The time now is 02:57 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.