0

i still learning about java and i have simple code

import javax.swing.*;

Public class Test extends JFrame{
JLabel name = new JLabel("Name");
JTextArea ta = new JTextArea();

Test(){
    this.setSize(435, 400);
    this.setTitle("Test");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLayout(null);
    name.setBounds(10, 10, 50, 20);
    this.add(name);
    ta.setBounds(90, 10, 300, 200); 
    ta.setLineWrap(true);
    JScrollPane scroll = new JScrollPane(ta);
    this.add(scroll);
}

public static void main(String [] args){
    new Test().setVisible(true);
}
}

What layout should i choose ? i want the textarea show beside the name label

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    What options are you presenting? You should probably look into a `LayoutManager` instead of hardcoding the layout. – Jeroen Vannevel Oct 20 '13 at 14:17
  • i'm sorry , i just started today ^^ ... – user2848429 Oct 20 '13 at 14:39
  • `setLayout(null);` Since you just started today, it is a good time to **not** pick up any bad habits. That is one of the worst. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Oct 20 '13 at 15:14

1 Answers1

0

If it's just a Label and a TextArea, just use a simple GridLayout

Test(){

    setLayout(new GridLayout(1, 2));   // sets a Grid Layout with 1 row and 2 columns
    add(name);

    JScrollPane scroll = new JScrollPane(ta);
    ta.setLineWrap(true);
    add(scroll);


}

public static void main(String[] args) {
    JFrame frame = new Test();
    frame.setSize(435, 400);
    frame.setTitle("Test");
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setVisible(true);
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • how about the size ? my textarea and label is bigger than i thought – user2848429 Oct 20 '13 at 14:36
  • Change the size of the frame `frame.setSize(x, y)`. You should go through the Java GUI tutorials if you're new to everything http://docs.oracle.com/javase/tutorial/uiswing/index.html – Paul Samsotha Oct 20 '13 at 14:43
  • 1
    The size of a `JTextArea` can be suggested in the number of rows & columns as well as the font size. Given the `GridLayout` will then size the other component, setting the size of the frame is unnecessary (and sub-optimal). – Andrew Thompson Oct 20 '13 at 15:17
  • 1
    And don't forget to `pack()`. – trashgod Oct 20 '13 at 16:06