-
Hi to all. I am having a problem regarding layouts. I have been reading about layouts but I cannot find the way to do this. I mean, is pretty simple. I want something like this:
Name: (textbox)
Lastname: (textbox)
(button)
Of course, textbox means that I want a textbox and the button a button. I mean, I want the two text boxes to be in the same amount of space from the left margin and the button in the middle of the labels and text boxes two lines below. Also, I have another doubt, how can I change the font of the labels, textboxs and buttons?
Thanks a lot!
Nicolas
-
Are you using the AWT or Swing? It makes a difference.
-
When I first tried Java, I just used a 3 X 3 GridLayout and settled for the button being either in the left or the right column (and played with the size of the container).
If you must have the button being centered between the "columns" of label and TextField, here is a start using AWT.
Code:
import java.awt.*;
public class L extends Frame{
public static void main(String arg[]){
L myLayout = new L();
}
L(){
Panel pOuter = new Panel(new GridLayout(2,1));
Panel pTop = new Panel();//Default Layout is FlowLayout
Panel pBottom = new Panel();
Panel p = new Panel(new GridLayout(2,2));
Label n = new Label("Name:");
Label l = new Label("Lastname:");
TextField t1 = new TextField();
TextField t2 = new TextField();
Button b = new Button("Your Button");
b.setFont(new Font("Serif", Font.BOLD, 18));//Can Set Fonts OR OMIT using object name "p,n,l,t1,t2, or b".
p.add(n);
p.add(t1);
p.add(l);
p.add(t2);
pTop.add(p);
pBottom.add(b);
pOuter.add(pTop);
pOuter.add(pBottom);
add(pOuter);
setSize(200,140);//Trial and error (not fun)
setVisible(true);
}
}
Using 3X3
Code:
import java.awt.*;
public class L extends Frame{
public static void main(String arg[]){
L myLayout = new L();
}
L(){
Panel p = new Panel(new GridLayout(3,3));
Label n = new Label("Name:");
Label l = new Label("Lastname:");
TextField t1 = new TextField();
TextField t2 = new TextField();
Button b = new Button("Your Button");
p.add(n);
p.add(t1);
p.add(l);
p.add(t2);
p.add(b);
add(p);//Defaults to center of BorderLayout for frame
setSize(200,100);//Notice change
setVisible(true);
}
}
I'd like to know if there are easier ways too (Swing or not).