Results 1 to 7 of 7
Thread: How to make it so it fit window
- 02-17-2012, 10:26 PM #1
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 14
- Rep Power
- 0
How to make it so it fit window
Hey!
What I'm trying to do is do a program where you can write your name, date and post in a text field. I have done that but
they are in horizontal (row) so it wont fit in my window, who has to be this.setSize(400, 400);
So I'm trying to get it in column (vertical), anyone can help me with the code? Would be thankful :)
Like this :
Name : Textfield
Date : Textfield
Post: Textfield
Here is my code (its shorten)
Java Code:import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class Vindu extends JFrame{ JTextArea jta = new JTextArea(); JTextField jtf = new JTextField(20); String alt =""; JTextField jtaInnlegg = new JTextField();//lag tekstområde ArrayList<String> al; public Vindu(){ al= new ArrayList<String>(); Container cp = this.getContentPane();//Hent innholdsfortegnelese cp.add(jta,BorderLayout.SOUTH); JPanel jpWest = new JPanel();//lag panel GridLayout gl = new GridLayout(3,1); jpWest.setLayout(gl); JLabel jlNavn = new JLabel("Name"); JLabel jlDato = new JLabel("Date"); JLabel jlInnlegg = new JLabel("Post"); JTextField jtfNavn = new JTextField(20); JTextField jtfDato = new JTextField(20); JTextField jtfInlegg = new JTextField(20); final JButton jb = new JButton("Lagre innlegg");//lag knapp jpWest.add(jlName); jpWest.add(jlDate); jpWest.add(jlIPost); jpWest.add(jtfName); jpWest.add(jtfDate); //Component jtfDate; jpWest.add(jtaPost); jpWest.add(jb); //legger panel west i et nytt panel som legge si west JPanel jpInner = new JPanel(); jpInner.add(jpWest); cp.add(jpInner,BorderLayout.WEST);//legg panel i west this.setTitle("Blogg program"); this.setLocation(150, 150); this.setSize(400, 400); this.setVisible(true); } public static void main(String[] args) { new Vindu(); } }Last edited by pbrockway2; 02-17-2012 at 10:32 PM. Reason: code tags added
- 02-17-2012, 10:34 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: How to make it so it fit window
Please use "code" tags when posting code. Put [code] at the start of the and [/code] at the end: that way the formatting is preserved when it appears here. It is also a good idea to use spaces rather than tabs to indent code as tabs can tend to get rendered rather wide.
- 02-17-2012, 10:47 PM #3
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 14
- Rep Power
- 0
Re: How to make it so it fit window
Ok will do next time (Y)
- 02-17-2012, 10:51 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: How to make it so it fit window
That's lots of rows and two columns, so the layout constructor would be "GridLayout gl = new GridLayout(0,2);"Like this :
Name : Textfield
Date : Textfield
Post: Textfield
The labels and other components get laid out (left to right, top to bottom) in the order you add them. So it's quite important to add them label/component, label/component, etc. Something like:
There are lots of details at Laying Out Components Within a Container lesson in Oracle's Tutorial.Java Code:JPanel jpWest = new JPanel();//lag panel GridLayout gl = new GridLayout(0,2); jpWest.setLayout(gl); JLabel jlNavn = new JLabel("Name"); JTextField jtfNavn = new JTextField(20); jpWest.add(jlNavn); jpWest.add(jtfNavn); JLabel jlDato = new JLabel("Date"); JTextField jtfDato = new JTextField(20); jpWest.add(jlDato); jpWest.add(jtfDato); JLabel jlInnlegg = new JLabel("Post"); jpWest.add(jlInnlegg); /* ??? */JTextField jtfInlegg = new JTextField(20); jpWest.add(jtaInnlegg); final JButton jb = new JButton("Lagre innlegg");//lag knapp jpWest.add(jb);
-----
I'll move this whole thread to the Awt/Swing forum where it belongs.
- 02-18-2012, 10:58 AM #5
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 14
- Rep Power
- 0
Re: How to make it so it fit window
Thank you it looks much better! But the text field don't still fit the window, there is too much space between the labels and text field.
It looks like this :
Name -Space- text field
Date -Space- text field
Post -Space- text field
Also the text area is not showing completely in the window, when I press the button "Lagre innlegg" the information I put on Post text field get printed out and the text area is enlarging, but I would like to have so it showing the entire text area from the beginning.
I have had this problem with other assignments too and I never figured out how to do it
- 02-18-2012, 11:15 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: How to make it so it fit window
I can't add much to what's explained in the Tutorial lesson already linked to. You can do a lot by nesting layouts: especially BorderLayout and BoxLayout which are both rather simple. Nesting them gives you the flexibility that GridLayout lacks.
- 02-18-2012, 11:33 AM #7
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Noob question: How to make smooth window changes?
By fr33d4n in forum AWT / SwingReplies: 1Last Post: 08-19-2011, 08:55 PM -
Hide taskbar and make window full screen
By tmparisi in forum New To JavaReplies: 6Last Post: 05-18-2011, 04:48 PM -
Need to Understand code to make window centralized
By vicky15 in forum New To JavaReplies: 3Last Post: 05-14-2011, 12:49 PM -
How do you make another window appear
By robertbob in forum AWT / SwingReplies: 5Last Post: 05-22-2010, 07:05 PM -
how to make window active and textfield focused
By whwillisiv in forum New To JavaReplies: 0Last Post: 04-28-2009, 04:06 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks