Beginner question about setHorisontalAlignment!
I´m new at java, and world like to place a label on a panels upper left corner.
Should i use a different LayoutManager?
Hers a simple examle i can't get to work.
Probably a simple fix, but i´m bad at this:D
Thanks for your time !
Best Regards
Oskar
Code Java:
import javax.swing.*;
import java.awt.*;
public class test {
public static void main(String[] args) {
JFrame frame = new JFrame("TrippTrapp");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 300));
panel.setBackground(Color.red);
JLabel label = new JLabel("hej");
label.setHorizontalAlignment(SwingConstants.LEFT);
panel.add(label);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
Re: Beginner question about setHorisontalAlignment!
setHorizontalAlignment will place your JLabel's text in the for left portion of the JLabel, but this won't have any visible effect on your GUI since your JLabel is only large enough to hold the text it contains.
I would suggest that yes, you use a different layout manager for the JPanel since its default FlowLayout will place the JLabel at the top-center position. Consider a GridBagLayout for this. With an appropriate GridBagConstraint, you should be able to place your JLabel where you want it. Another option is to nest JPanels that use simpler layouts such as both using BorderLayout, and placing one JPanel in the BorderLayout.NORTH position of the main JPanel, and then placing the JLabel in the BorderLayout.WEST position of the north JPanel.
Re: Beginner question about setHorisontalAlignment!
You can use "null" layout....then use setBounds() method to specify exactly where you want to display your component...
like....
setLayout(null);
component.setBounds(a,b,c,d);
here--a= starting x coordinate,b= starting y coordinate,c=length of component along x axis,d=height of component along y axis.
hope its helpful :)
Re: Beginner question about setHorisontalAlignment!
Quote:
Originally Posted by
rougeking
You can use "null" layout....then use setBounds() method to specify exactly where you want to display your component...
like....
setLayout(null);
component.setBounds(a,b,c,d);
here--a= starting x coordinate,b= starting y coordinate,c=length of component along x axis,d=height of component along y axis.
hope its helpful :)
I urge you to ignore rougeking's well-meaning but misleading help. null layout should be avoided unless absolutely necessary (such as for some animations and for dragging components). If you use null layout, you will make your GUI *very* hard to update and expand.
Re: Beginner question about setHorisontalAlignment!
ya I too agree with curmudgen.....every layout has got it drawbacks....
I just gave it so that u'll have an idea about null also.....
use it only there are less components,frame is set to nonresizable....and more over null layout doesnt guarantee same amount of pixel count on different platforms.
Re: Beginner question about setHorisontalAlignment!
Thanks everyone! You guys have been a big help! I solved the problem using GridBagLayout! Thanks for the hint!
Best Regards
Oskar
Re: Beginner question about setHorisontalAlignment!
Great -- glad you've got it working!