Swing JFrame Quiz - Java Swing

Java examples for Swing:JFrame

Introduction

Create a frame that opens a smaller frame with fields asking for a username and password.

Demo Code

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Main extends JFrame {
  PasswordFrame pass = new PasswordFrame();

  public Main() {
    super("Ask Password");
    setSize(540, 80);//from  w  ww.j a v  a 2s .  com
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    pass.setVisible(true);
  }

  public static void main(String[] arguments) {
    Main ask = new Main();
  }
}

class PasswordFrame extends JFrame {
  public PasswordFrame() {
    super("Password");
    setSize(210, 130);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JPanel pane = new JPanel();
    JLabel usernameLabel = new JLabel("Username: ");
    JTextField username = new JTextField(8);
    JLabel passwordLabel = new JLabel("Password: ");
    JPasswordField password = new JPasswordField(8);
    pane.add(usernameLabel);
    pane.add(username);
    pane.add(passwordLabel);
    pane.add(password);
    setContentPane(pane);
    setVisible(false);
  }
}

Related Tutorials