Results 1 to 4 of 4
  1. #1
    nokomis is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Default 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>

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You'll likely need to nest JPanels for this to work. Put your textfields in their own jpanel, perhaps boxlayout, oriented in a page axis direction, and add your JLabel first then the JTextField, then add these to the main JPanel BorderLayout whatever.

    Again, the key here is to nest JPanels, each using whatever layout would best solve your problem.

  3. #3
    nokomis is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Default

    Okay, I modified an existing one, but after compiling, it still shows the old values in the html page, but on the appletviewer, the new modifications show up. Why?

  4. #4
    nokomis is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

Similar Threads

  1. GUI - JLabel
    By Azndaddy in forum New To Java
    Replies: 8
    Last Post: 05-02-2008, 08:03 AM
  2. JLabel append?
    By Jononomous in forum New To Java
    Replies: 0
    Last Post: 04-07-2008, 08:41 PM
  3. Applet Login System Tutorial
    By Free-JavaTutorials in forum Java Applets
    Replies: 0
    Last Post: 03-15-2008, 03:51 AM
  4. JLabel
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 02:55 PM
  5. JLabel
    By Freddie in forum AWT / Swing
    Replies: 2
    Last Post: 05-29-2007, 03:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •