Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Arrays;
- 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 (Arrays.equals("stackoverflow".toCharArray(), jpfPassword.getPassword())
- && "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);
- }
- });
- }
- }
- JFrame f = new JFrame();
- JTextField text = new JTextField(15); //the 15 sets the size of the text field
- JPanel p = new JPanel();
- JButton b = new JButton("Login");
- f.add(p); //so you can add more stuff to the JFrame
- f.setSize(250,150);
- f.setVisible(true);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- p.add(text);
- p.add(b);
- b.addActionListener(this);
- public void actionPerforemed(ActionEvent e)
- {
- //Get the text of the JTextField
- String TEXT = text.getText();
- }
- import java.awt.event*;
- import java.awt.*; //Just in case we need it
- import java.x.swing.*;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement