|
Jlabel in Login Applet
How can I put a JLabel in my applet? I've tried different ways but cannot seem to get it to work. All I want to do is put "username" and "password" above the text and password fields.
<code>import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginApplet extends JApplet
implements ActionListener {
private JTextField login;
private JPasswordField password;
private JButton loginButton;
public void init() {
// Set the layout manager
Container c = getContentPane();
c.setLayout(new BorderLayout());
// Create the fields and button
login = new JTextField(10);
password = new JPasswordField("asecret");
loginButton = new JButton("Login");
// Add components to the container
c.add(login, BorderLayout.NORTH);
c.add(password, BorderLayout.CENTER);
c.add(loginButton, BorderLayout.SOUTH);
// Add event handlers
login.addActionListener(this);
password.addActionListener(this);
loginButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == login) {
password.requestFocus();
} else if ((e.getSource() == password)
|| (e.getSource() == loginButton)) {
JOptionPane.showMessageDialog(null,
"Login: " + login.getText()
+ "\nPassword: "
+ new String(password.getPassword()),
"Login Message",
JOptionPane.INFORMATION_MESSAGE);
}
}
}</code>
|