Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Ron McLeod
Jeanne Boyarsky
Paul Clapham
Sheriffs:
Liutauras Vilda
Henry Wong
Devaka Cooray
Saloon Keepers:
Tim Moores
Stephan van Hulst
Tim Holloway
Al Hobbs
Carey Brown
Bartenders:
Piet Souris
Mikalai Zaikin
Himai Minh
Forum:
Swing / AWT / SWT
BoxLayout quirk
Eric Larsen
Ranch Hand
Posts: 35
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Posts: 4632
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
How to implement editable field in table to restrict to accept fixed length
Why are my JFrames closing?
panels not displayed fullly in Jframe
Getting information into a JTable from a drawing Canvas
Help with Chat software
More...