Re: Output to the frame
Ravi wrote:
import java.awt.*;
..
The frame added lasts covers the whole window. Please help by
correcting my code.
A bit of an expansion/refinement on what Liz
mentioned.
<sscce>
import java.awt.*;
class LogInAWT {
public static void main(String args[]) {
Frame f = new Frame("Private");
TextField t1 = new TextField(10);
TextField t2 = new TextField(10);
t2.setEchoChar('*');
Label l1 = new Label("Name");
Label l2 = new Label("Password");
Button bLogIn = new Button("Log In");
Panel pLabel = new Panel( new GridLayout(0,1) );
Panel pText = new Panel( new GridLayout(0,1) );
Panel pButton = new Panel();
pLabel.add(l1);
pLabel.add(l2);
pText.add(t1);
pText.add(t2);
pButton.add(bLogIn);
// not needed, not robust.
// see 'pack()' below.
// f.setSize(400,400);
f.add(pLabel, BorderLayout.WEST);
f.add(pText, BorderLayout.CENTER);
f.add(pButton, BorderLayout.SOUTH);
f.setLocationRelativeTo( null );
f.pack();
f.setVisible(true);
}
}
</sscce>
But why AWT? Here is a close Swing variant.
<sscce>
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
class LogInSwing {
public static void main(String args[]) {
JFrame f = new JFrame("Private");
JTextField t1 = new JTextField(10);
// JPasswordField automatically hides text
JPasswordField t2 = new JPasswordField(10);
JLabel l1 = new JLabel("Name");
JLabel l2 = new JLabel("Password");
JButton bLogIn = new JButton("Log In");
EmptyBorder border =
new EmptyBorder(3,10,1,10);
JPanel pLabel = new JPanel(
new GridLayout(0,1,10,10) );
pLabel.setBorder(border);
JPanel pText = new JPanel(
new GridLayout(0,1,10,10) );
pText.setBorder(border);
JPanel pButton = new JPanel();
pLabel.add(l1);
pLabel.add(l2);
pText.add(t1);
pText.add(t2);
pButton.add(bLogIn);
f.add(pLabel, BorderLayout.WEST);
f.add(pText, BorderLayout.CENTER);
f.add(pButton, BorderLayout.SOUTH);
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setLocationRelativeTo( null );
f.pack();
f.setVisible(true);
}
}
</sscce>
HTH
--
Andrew Thompson
http://www.athompson.info/andrew/
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-setup/200704/1