-
September 21st, 2004, 01:56 AM
#1
Java Component Alignment Trouble
I was wondering if anyone can shed any light on the following problem I've been having. I finally stumbled upon the GridBagLayout manager and now do not need to suffer stretched components being displayed on my screen, but the problem that I'm having is that the GridBagLayout is centering the components on the screen when I want them placed in the top left hand corner like this ascii diagram shows:
Code:
I currently have the following showing:
------------------
| |
| |
| Label1 |
| Label2 |
| |
| |
------------------
rather than:
------------------
|Label1 |
|Label2 |
| |
| |
| |
| |
------------------
I'm not sure how to change this as I cannot find any methods or public vars that I can use/change to alter where the components are placed. And yes, I have searched through the java api extensively and searched google and the sun java site.
Thanks,
ac
-
September 21st, 2004, 02:29 AM
#2
Make a panel and then use a BorderLayout and then for the location use PAGE_START... look below:
Code:
mainPanel.setLayout(new BorderLayout());
mainPanel.add(topLeftButton, BorderLayout.PAGE_START);
-
September 21st, 2004, 12:24 PM
#3
Senior Member
My suggestion: Use a BoxLayout
Swing tutorial
-
September 21st, 2004, 11:24 PM
#4
Death_Knight, using a BoxLayout solved the problem I posted above, the only problem I have now is that before the labels stretched horizontally so that if you changed the background colour of the label to be different from the background colour of the panel it was added to, the colour would stretch to the end of the panel, now that's not happening. meh! Solved one problem and made another. :P
Anyhow, if you've got a solution I'd be glad to hear it - meanwhile I'll keep looking myself.
ac
-
September 22nd, 2004, 02:19 AM
#5
Senior Member
You can try reading this
Take note of this
What if none of the components has a maximum width? Well, if all the components have identical X alignment, then all components are made as wide as their container. If the X alignments are different, then any component with an X alignment of 0.0 (left) or 1.0 (right) will be smaller. All components with an intermediate X alignment (such as center) will be as wide as their container. Here are two examples:
-
September 22nd, 2004, 07:50 PM
#6
Amazing...I had actually skimmed that and tried changing the Maximum size of my labels but because I didn't set the alignment of them, it didn't change the size correctly. For some reason I just assumed it couldn't have been an alignment issue.
BTW -- if anyone is interested, I'm attaching the program. It's not nearly finished yet, but just so that you can see what I was trying to do. To be honest, the gui is not meant to be taken seriously, it was meant to be a bad joke (one that I won't explain); in fact, I find it ironic that in creating this gui, I'm basically emulating an old dos application :P
The most interesting part of the program is the Menu class which creates a menu by parsing the code written in the menu file (which is changed at the moment by changing the value of a constant in the Menu class.
I want to stress that the program isn't finished...I've still got to add stuff and clean up the mess that MenuGame.java got into from me trying out different layout managers and weird things.
The two menu files included are MENU.txt and JOYCE.tst. And if you're wondering where I got the sad idea for the gui, it's the till system we use at my work (or my copy of it)
ac
-
September 23rd, 2004, 01:12 PM
#7
Senior Member
Did you try using setBound ? Perharps you can try using JBuilder from Borland. This will help you in GUI development
-
September 24th, 2004, 04:02 AM
#8
I didn't, but it doesn't matter because I got the program to do what I want now. I've tried using JBuilder before (just messing about really), but I don't like the way it works (from what I've seen). I quite like the fact that in java it's relatively easy to code fairly complicated GUIs by hand.
ac
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|