Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package Client;
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import java.nio.ByteBuffer;
- import java.nio.channels.SocketChannel;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- /**
- *
- * @author osm70
- */
- public class RegisterWindow extends javax.swing.JFrame {
- /**
- * Creates new form RegisterWindow
- */
- public RegisterWindow() {
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- name = new javax.swing.JTextField();
- password = new javax.swing.JPasswordField();
- repeat = new javax.swing.JPasswordField();
- jButton1 = new javax.swing.JButton();
- jButton2 = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jLabel1.setText("Name:");
- jLabel2.setText("Password:");
- jLabel3.setText("Password:");
- jButton1.setText("Register");
- jButton1.setToolTipText("");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jButton2.setText("back");
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jButton1))
- .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
- .addGap(29, 29, 29)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel3)
- .addGap(18, 18, 18)
- .addComponent(repeat))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jButton2)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel1)
- .addGap(31, 31, 31)
- .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel2)
- .addGap(18, 18, 18)
- .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGap(0, 0, Short.MAX_VALUE)))))
- .addGap(223, 223, 223))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(28, 28, 28)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel1)
- .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel2)
- .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel3)
- .addComponent(repeat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(36, 36, 36)
- .addComponent(jButton1)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
- .addComponent(jButton2)
- .addGap(33, 33, 33))
- );
- pack();
- }// </editor-fold>
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- this.setVisible(false);
- Client.lw.setVisible(true);
- // TODO add your handling code here:
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- if (name.getText().contains("§")||password.getText().contains("§")||repeat.getText().contains("§"))
- {
- JOptionPane.showMessageDialog(rootPane, "Invalid entry. Do not use §");
- }
- else {
- if(password.getText().equals(repeat.getText())) {
- try {
- char tmp='O';
- SocketChannel socketChannel = SocketChannel.open();
- InetSocketAddress address = new InetSocketAddress("127.0.0.1", 1253);
- socketChannel.connect(address);
- ByteBuffer buf = ByteBuffer.allocate(9999);
- buf.clear();
- String message = name.getText() + "§" + password.getText();
- buf.put(message.getBytes());
- buf.flip();
- while (buf.hasRemaining()) {
- socketChannel.write(buf);
- }
- buf.clear();
- buf.clear();
- buf.clear();
- socketChannel.close();
- try {
- buf.clear();
- int cnt;
- cnt = socketChannel.read(buf);
- socketChannel.configureBlocking(false);
- while (cnt > 0){
- try {
- buf.flip();
- while (buf.hasRemaining()){
- tmp=((char) buf.get());
- System.out.println(tmp);
- }
- buf.clear();
- cnt = socketChannel.read(buf);
- } catch (IOException ex) {
- Logger.getLogger(ChatWindow.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- socketChannel.close();
- } catch (IOException ex) {
- Logger.getLogger(ChatWindow.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- buf.clear();
- int cnt;
- cnt = socketChannel.read(buf);
- socketChannel.configureBlocking(false);
- while (cnt > 0){
- try {
- buf.flip();
- while (buf.hasRemaining()){
- tmp=((char) buf.get());
- System.out.println(tmp);
- }
- buf.clear();
- cnt = socketChannel.read(buf);
- } catch (IOException ex) {
- Logger.getLogger(ChatWindow.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- socketChannel.close();
- } catch (IOException ex) {
- Logger.getLogger(ChatWindow.class.getName()).log(Level.SEVERE, null, ex);
- }
- if (tmp=='O')
- {
- JOptionPane.showMessageDialog(rootPane, "Account created");
- }
- else {
- JOptionPane.showMessageDialog(rootPane, "Name taken");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- else { JOptionPane.showMessageDialog(rootPane, "Password mismatch");
- }
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(RegisterWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(RegisterWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(RegisterWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(RegisterWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new RegisterWindow().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JTextField name;
- private javax.swing.JPasswordField password;
- private javax.swing.JPasswordField repeat;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement