Positioning elements. Is it possible without layouts?
Hi all,
I'm still trying to understand the logic of Java, and it feels like I'm getting stuck more and more :confused:
Right now I wanna create a JFrame and add just one button to it. If I use FlowLayout the button is centered by default, but I want it to be located at x:10 and y:10.
Code Java:
package graphical;
import javax.swing.*;
import java.awt.*;
public class GUI extends JFrame
{
private JButton button;
public GUI()
{
super("JAVA");
setLayout(new FlowLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(640, 480);
this.setVisible(true);
button = new JButton("some button");
button.setLocation(10, 10); // ???
this.add(button);
}
}
I've googled out several ways to create custom layouts but they all seem to be too large.
As I'm mostly an AS3 coder, I'd normally do this by just setting the button's x and y values like so:
button.x = 10;
button.y = 10;
And this approach would work fine in ActionScript 3, but doesn't seem to work in Java.
Is there a simple way to do the same in Java?
Re: Positioning elements. Is it possible without layouts?
Quote:
Originally Posted by
goodguy
Hi all,
I'm still trying to understand the logic of Java, and it feels like I'm getting stuck more and more :confused:
Right now I wanna create a JFrame and add just one button to it. If I use FlowLayout the button is centered by default, but I want it to be located at x:10 and y:10.
Code Java:
package graphical;
import javax.swing.*;
import java.awt.*;
public class GUI extends JFrame
{
private JButton button;
public GUI()
{
super("JAVA");
setLayout(new FlowLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(640, 480);
this.setVisible(true);
button = new JButton("some button");
button.setLocation(10, 10); // ???
this.add(button);
}
}
I've googled out several ways to create custom layouts but they all seem to be too large.
As I'm mostly an AS3 coder, I'd normally do this by just setting the button's x and y values like so:
button.x = 10;
button.y = 10;
And this approach would work fine in ActionScript 3, but doesn't seem to work in Java.
Is there a simple way to do the same in Java?
If it's the same class, you don't have to this.methodName(), just call it like methodName().
Layouts are a royal pain! ~X(~X(~X(~X(
I've fiddled with them many times.
For JButton, there's also a setBounds() method that should setLocation(). I've no clue why it doesn't just move it to that location with the setLocation().
Re: Positioning elements. Is it possible without layouts?
Re: Positioning elements. Is it possible without layouts?
Yes, but how come setLocation(x,y) never worked quite how I wanted it to for me?
What's setLocation() do?
Do you have to use Insets, whatever those are, like in the example, or can you just plug in numbers?
I can find what in theory works, but it never got to work yet.
Re: Positioning elements. Is it possible without layouts?
Quote:
Originally Posted by
javapenguin
If it's the same class, you don't have to this.methodName(), just call it like methodName().
But using this.methodName() isn't hurting anything, and is often used to make the code more readable.
Quote:
Originally Posted by
javapenguin
Layouts are a royal pain! ~X(~X(~X(~X(
Not if you have a basic understanding of how they work, which a quick read-through of the tutorials will give you. They make otherwise complicated things, like resizing, repositioning, changing look and feel, etc very trivial. Steering an amateur programmer away from using layouts is not helpful.
Quote:
Originally Posted by
javapenguin
The JComponent class has a setAllignmentX() and setAllignmentY() methods that set allignment for that JComponent.
Okay. And how do you think that will help the OP? What are you actually suggesting?
Quote:
Originally Posted by
javapenguin
For JButton, there's also a setBounds() method that should setLocation(). I've no clue why it doesn't just move it to that location with the setLocation().
Because that's not how it works. If you have no clue, then why bother adding your input? Throw together a simple program that uses the method in question. Play around with it an test your assumptions before simply spewing out assumptions.
Re: Positioning elements. Is it possible without layouts?
Quote:
Originally Posted by
javapenguin
Yes, but how come setLocation(x,y) never worked quite how I wanted it to for me?
Then your assumptions are wrong. Write a basic program that plays with it.
Quote:
Originally Posted by
javapenguin
What's setLocation() do?
What does the API say it does? What do the tutorials say?
Quote:
Originally Posted by
javapenguin
Do you have to use Insets, whatever those are, like in the example, or can you just plug in numbers?
How hard would it have been to look up what insets are before posting this? How hard would it have been to write a simple program that tested them?
Quote:
Originally Posted by
javapenguin
I can find what in theory works, but it never got to work yet.
That's because you don't test your assumptions. Write some simple test programs that play with these things. Stop hijacking this thread.
I'm irritated. Can you tell?
2 Attachment(s)
Re: Positioning elements. Is it possible without layouts?
I tried to fix this wronging awhile ago.
I have attached a .txt file of an Object I made that lets you position objects based on Coordinate, centered, offsetting, pointers to other elements, arrays (1D and 2D) of components, ect.
I also attached a test program that I don't think uses all the methods (it is an old test program). Tell me if you need help using it, I'll be happy to help. It has made GUI building about a thousand times easier for me. You probably cannot use this with a GUI builder, I imagine things will blow up.
Save them as .java files and play with the layout manager. It is very simple once you get the hang of it.