A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Swing / AWT / SWT
Author
BoxLayout quirk
Eric Larsen
Ranch Hand
Joined: Mar 28, 2009
Posts: 35
posted
May 07, 2009 18:58:28
0
Why does my my
JLabel
appear all the way on the right-hand side of my GUI?
The source code for the class:
package com.teslarobotics.gui; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MultipleScreens { JFrame frame; JPanel login; JPanel loginFailed; JPanel loginSuccess; public static void main (String[] args) { MultipleScreens screens = new MultipleScreens(); } public MultipleScreens () { frame = new JFrame("Multiple Screens"); login = new JPanel(); JTextField username = new JTextField(15); JLabel usernameLabel = new JLabel("Username: "); JTextField password = new JTextField(15); JLabel passwordLabel = new JLabel("Password: "); JButton loginButton = new JButton("Log in"); //loginButton.addActionListener(new LoginButtonListener()); JButton createAccountButton = new JButton("Create an account"); //createAccountButton.addActionListener(new CreateAccountButtonListener()); JLabel pleaseLogIn = new JLabel("Please log in to Mindows Wessenger:"); JPanel usernamePane = new JPanel(); usernamePane.setLayout(new BoxLayout(usernamePane, BoxLayout.LINE_AXIS)); usernamePane.add(usernameLabel); usernamePane.add(username); usernamePane.add(Box.createRigidArea(new Dimension(300, 0))); JPanel passwordPane = new JPanel(); passwordPane.setLayout(new BoxLayout(passwordPane, BoxLayout.LINE_AXIS)); passwordPane.add(passwordLabel); passwordPane.add(password); passwordPane.add(Box.createRigidArea(new Dimension(300, 0))); JPanel buttonPane = new JPanel(); buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); buttonPane.add(Box.createHorizontalGlue()); buttonPane.add(loginButton); buttonPane.add(Box.createRigidArea(new Dimension(5, 0))); buttonPane.add(createAccountButton); login.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); login.setLayout(new BoxLayout(login, BoxLayout.PAGE_AXIS)); login.add(pleaseLogIn); login.add(Box.createRigidArea(new Dimension(0, 5))); login.add(usernamePane); login.add(passwordPane); login.add(Box.createRigidArea(new Dimension(0, 10))); login.add(buttonPane); frame.getContentPane().add(login); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 200); frame.setVisible(true); } }
Attached is a screenshot of the GUI output.
Wessenger_screen.JPG
The GUI and the offending JLabel at the top.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
May 07, 2009 21:16:49
0
add these lines
usernamePane.setAlignmentX(0);
passwordPane.setAlignmentX(0);
buttonPane.setAlignmentX(0);
comment out this line
//buttonPane.add(Box.createHorizontalGlue());
and they'll all be left-aligned
I agree. Here's the link:
http://aspose.com/file-tools
subject: BoxLayout quirk
Similar Threads
panels not displayed fullly in Jframe
Why are my JFrames closing?
Help with Chat software
Getting information into a JTable from a drawing Canvas
How to implement editable field in table to restrict to accept fixed length
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter