Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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..




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 12:19 PM
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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.