示例#1
0
  public NewUser() {
    super("Adding New User");
    label1 = new JLabel("Name");
    label2 = new JLabel("Category");
    username = new JLabel("Username");
    password = new JLabel("Password");
    confirm = new JLabel("Re-enter Password");
    pass1 = new JPasswordField();
    pass2 = new JPasswordField();
    txtusername = new JTextField();
    name = new JTextField();
    combo1 = new JComboBox();
    button1 = new JButton("Ok", new ImageIcon("Icon/i16x16/ok.png"));
    button2 = new JButton("Cancel", new ImageIcon("Icon/i16x16/exit.png"));

    panel1 = new JPanel(new GridLayout(6, 2));
    panel1.add(label1);
    panel1.add(name);
    panel1.add(label2);
    panel1.add(combo1);
    panel1.add(username);
    panel1.add(txtusername);
    panel1.add(password);
    panel1.add(pass1);
    panel1.add(confirm);
    panel1.add(pass2);
    panel1.add(button1);
    panel1.add(button2);
    combo1.addItem("Manager");
    combo1.addItem("Booking Clerk");
    combo1.addItem("Supervisor");
    panel2 = new JPanel();
    panel2.add(panel1);
    getContentPane().add(panel2);
    setSize(350, 195);
    setVisible(true);
    setLocation((screen.width - 500) / 2, ((screen.height - 350) / 2));
    setResizable(false);
    name.addKeyListener(
        new KeyAdapter() {
          public void keyTyped(KeyEvent e) {
            char c = e.getKeyChar();
            if (!(Character.isLetter(c)
                || (c == KeyEvent.VK_BACK_SPACE)
                || (c == KeyEvent.VK_SPACE)
                || (c == KeyEvent.VK_DELETE))) {

              getToolkit().beep();
              JOptionPane.showMessageDialog(
                  null, "Invalid Character", "ERROR", JOptionPane.DEFAULT_OPTION);
              e.consume();
            }
          }
        });
    txtusername.addKeyListener(
        new KeyAdapter() {
          public void keyTyped(KeyEvent e) {
            char c = e.getKeyChar();
            if (!(Character.isLetter(c)
                || (c == KeyEvent.VK_BACK_SPACE)
                || (c == KeyEvent.VK_SPACE)
                || (c == KeyEvent.VK_DELETE))) {

              getToolkit().beep();
              JOptionPane.showMessageDialog(
                  null, "Invalid Character", "ERROR", JOptionPane.DEFAULT_OPTION);
              e.consume();
            }
          }
        });
    button1.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent e) {

            if (name.getText() == null || name.getText().equals("")) {
              JOptionPane.showMessageDialog(
                  null, "Enter name", "ERROR", JOptionPane.DEFAULT_OPTION);
              name.requestFocus();
              return;
            }
            if (txtusername.getText() == null || txtusername.getText().equals("")) {
              JOptionPane.showMessageDialog(
                  null, "Enter username", "ERROR", JOptionPane.DEFAULT_OPTION);
              txtusername.requestFocus();
              return;
            }
            if (pass1.getText() == null || pass1.getText().equals("")) {
              JOptionPane.showMessageDialog(
                  null, "Enter password", "ERROR", JOptionPane.DEFAULT_OPTION);
              pass1.requestFocus();
              return;
            }
            if (pass2.getText() == null || pass2.getText().equals("")) {
              JOptionPane.showMessageDialog(
                  null, "Confirm your password", "ERROR", JOptionPane.DEFAULT_OPTION);
              pass2.requestFocus();
              return;
            }
            if (!pass1.getText().equals(pass2.getText())) {
              JOptionPane.showMessageDialog(
                  null, "passwords do not match.", "ERROR", JOptionPane.DEFAULT_OPTION);
              pass2.requestFocus();
              return;
            }
            try {
              Statement statement = Connect.getConnection().createStatement();
              {
                String temp =
                    "INSERT INTO users (Name,Category,username, password) VALUES ('"
                        + name.getText()
                        + "', '"
                        + combo1.getSelectedItem()
                        + "', '"
                        + txtusername.getText()
                        + "', '"
                        + pass1.getText()
                        + "')";

                int result = statement.executeUpdate(temp);
                JOptionPane.showMessageDialog(
                    null, "User is succesfully added", "SUCCESS", JOptionPane.DEFAULT_OPTION);
                dispose();
              }

            } catch (Exception in) {
              in.printStackTrace();
            }
          }
        });
    button2.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            dispose();
          }
        });
  }
  ChatClient(String login, int id, int grpId) throws IOException {
    super(login); // call the super constructor to name the JFrame.
    loginName = login;
    userId = id;
    groupId = grpId;

    ta = new JTextArea(18, 50);
    tf = new JTextField(50);

    send = new JButton("Send");
    logout = new JButton("Log Out");
    refresh = new JButton("Refresh");

    addWindowListener(
        new WindowAdapter() {
          @Override
          public void windowClosing(WindowEvent e) {
            try {
              dout.writeUTF(loginName + " " + "LOGOUT");
              System.exit(1);
            } catch (IOException e1) {
              e1.printStackTrace();
            }
          }
        });

    tf.addKeyListener(
        new KeyListener() {
          @Override
          public void keyTyped(KeyEvent e) {}

          @Override
          public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
              try {
                if (tf.getText().length() > 0) {
                  dout.writeUTF(loginName + " DATA " + tf.getText().toString());
                  tf.setText("");
                }
              } catch (IOException e1) {
                e1.printStackTrace();
              }
            }
          }

          @Override
          public void keyReleased(KeyEvent e) {}
        });

    send.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            try {
              if (tf.getText().length() > 0) {
                dout.writeUTF(loginName + " DATA " + tf.getText().toString());
                tf.setText("");
              }
            } catch (IOException e1) {
              e1.printStackTrace();
            }
          }
        });

    logout.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            try {
              dout.writeUTF(loginName + " LOGOUT ");
              System.exit(1);
            } catch (IOException e1) {
              e1.printStackTrace();
            }
          }
        });

    refresh.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            DatabaseOperations.executeAvailability();
          }
        });

    socket = new Socket("localhost", SOCKETNUMBER);

    din = new DataInputStream(socket.getInputStream());
    dout = new DataOutputStream(socket.getOutputStream());

    dout.writeUTF(loginName);
    dout.writeUTF(loginName + " LOGIN");

    thread = new Thread(this);
    thread.start();
    setup();
  }