Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an example of a Login Dialog. Closes entire program with close of Dialog.</p> <blockquote> <p>Username: <b><code>stackoverflow</code></b> Password: <b><code>stackoverflow</code></b></p> </blockquote> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestFrame extends JFrame { private PassWordDialog passDialog; public TestFrame() { passDialog = new PassWordDialog(this, true); passDialog.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new TestFrame(); frame.getContentPane().setBackground(Color.BLACK); frame.setTitle("Logged In"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setExtendedState(JFrame.MAXIMIZED_BOTH); } }); } } class PassWordDialog extends JDialog { private final JLabel jlblUsername = new JLabel("Username"); private final JLabel jlblPassword = new JLabel("Password"); private final JTextField jtfUsername = new JTextField(15); private final JPasswordField jpfPassword = new JPasswordField(); private final JButton jbtOk = new JButton("Login"); private final JButton jbtCancel = new JButton("Cancel"); private final JLabel jlblStatus = new JLabel(" "); public PassWordDialog() { this(null, true); } public PassWordDialog(final JFrame parent, boolean modal) { super(parent, modal); JPanel p3 = new JPanel(new GridLayout(2, 1)); p3.add(jlblUsername); p3.add(jlblPassword); JPanel p4 = new JPanel(new GridLayout(2, 1)); p4.add(jtfUsername); p4.add(jpfPassword); JPanel p1 = new JPanel(); p1.add(p3); p1.add(p4); JPanel p2 = new JPanel(); p2.add(jbtOk); p2.add(jbtCancel); JPanel p5 = new JPanel(new BorderLayout()); p5.add(p2, BorderLayout.CENTER); p5.add(jlblStatus, BorderLayout.NORTH); jlblStatus.setForeground(Color.RED); jlblStatus.setHorizontalAlignment(SwingConstants.CENTER); setLayout(new BorderLayout()); add(p1, BorderLayout.CENTER); add(p5, BorderLayout.SOUTH); pack(); setLocationRelativeTo(null); setDefaultCloseOperation(DISPOSE_ON_CLOSE); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); jbtOk.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if ("stackoverflow".equals(String.valueOf(jpfPassword.getPassword())) &amp;&amp; "stackoverflow".equals(jtfUsername.getText())) { parent.setVisible(true); setVisible(false); } else { jlblStatus.setText("Invalid username or password"); } } }); jbtCancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); parent.dispose(); System.exit(0); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload