Monday May 19, 2008
Monday May 19, 2008
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel l = new JLabel("label:");
JTextField f = new JTextField(10);
JButton b = new JButton("Calc");
panel.add(l);
panel.add(f);
panel.add(b);
frame.getContentPane().add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
public class Test2 {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4,2));
JLabel l1 = new JLabel("label:");
JTextField f1 = new JTextField(10);
panel.add(l1);
panel.add(f1);
JLabel l2 = new JLabel("label:");
JTextField f2 = new JTextField(10);
panel.add(l2);
panel.add(f2);
JLabel l3 = new JLabel("label:");
JTextField f3 = new JTextField(10);
panel.add(l3);
panel.add(f3);
JButton b = new JButton("Calc");
panel.add(b);
frame.getContentPane().add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
public class Test3 {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4,1));
JLabel l1 = new JLabel("label:");
JTextField f1 = new JTextField(10);
JPanel panel1 = new JPanel();
panel1.add(l1);
panel1.add(f1);
panel.add(panel1);
JLabel l2 = new JLabel("label:");
JTextField f2 = new JTextField(10);
JPanel panel2 = new JPanel();
panel2.add(l2);
panel2.add(f2);
panel.add(panel2);
JLabel l3 = new JLabel("label:");
JTextField f3 = new JTextField(10);
JPanel panel3 = new JPanel();
panel3.add(l3);
panel3.add(f3);
panel.add(panel3);
JButton b = new JButton("Calc");
JPanel panel4 = new JPanel();
panel4.add(b);
panel.add(panel4);
frame.getContentPane().add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
Don't teach him layout problems, you should really care about layout managers when you are going to create a software that will be executing in a multi-enviroment, OS, fonts, L&F, which sure it's not your case, for the moment keep it simple and with an explanation of why and the reason behind layout managers, for now use a null or absolute layout. that will keep the real problems focused. And by the time he needs to write a real app he will be able to handle LayoutManagers, after some problems like all of us.
Posted by 201.244.248.42 on May 19, 2008 at 08:43 PM PDT #
Normally, I would disagree, but in this particular instance (teaching a young beginner), I'll have to agree with the previous commenter. Actually, I would recommend you use NetBeans Matisse. It's absolutely *perfect* for this sort of thing (when you want to just forget about the layout manager), especially since it makes the auto-generated code immutable. In short, it lets you focus on more interesting problems and deal with the subtleties of Swing later on.
Posted by Daniel Spiewak on May 21, 2008 at 03:46 PM PDT #