Главная страница Случайная страница КАТЕГОРИИ: АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника |
FrmServer.java
/* * 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 jChatServer; import java.awt.Dimension; import java.awt.Toolkit; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; /** * * @author VasVas */ public class frmServer extends javax.swing.JFrame {
private netServer server; private Thread ServerThread; /** * Creates new form frmServer */ public frmServer() { initComponents(); InetAddress addr; try { addr = InetAddress.getLocalHost(); String myLANIP = addr.getHostAddress(); lblIP.setText(" IP: " +myLANIP); } catch (UnknownHostException ex) { Logger.getLogger(frmServer.class.getName()).log(Level.SEVERE, null, ex); } }
/** * 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" > //GEN-BEGIN: initComponents private void initComponents() {
jButton1 = new javax.swing.JButton(); lblIP = new javax.swing.JLabel(); txtPort = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); lblStatus = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle(" Сервер чату");
jButton1.setText(" Запустити сервер"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } });
lblIP.setText(" IP: ");
txtPort.setText(" 11777");
jLabel1.setText(" Port: ");
lblStatus.setText(" Сатутс: не активний");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(lblIP, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(lblStatus)) .addGap(59, 59, 59)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblIP)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(lblStatus)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) );
pack(); }// < /editor-fold> //GEN-END: initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST: event_jButton1ActionPerformed // TODO add your handling code here: if(server==null){ server=new netServer(Integer.parseInt(txtPort.getText()), this); ServerThread=server; ServerThread.start();
lblStatus.setText(" Сатутс: активний"); jButton1.setText(" Зупинити "); }else{ ServerThread.interrupt(); stopThread z = new stopThread(server); //server.stopServer(); z.start(); jButton1.setText(" Запустити сервер"); lblStatus.setText(" Сатутс: вимкнено"); server=null; } }//GEN-LAST: event_jButton1ActionPerformed public void resetServerStart(String msg){ lblStatus.setText(" Сатутс: не активний"); jButton1.setText(" Запустити сервер"); server=null; JOptionPane.showMessageDialog(this, " Сервер не може приймати з’єднання! \n" +" Певно він уже запущений, або зайнятий порт.\n" +" ERROR message: " + msg); } /** * @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 https://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(frmServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(frmServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(frmServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(frmServer.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() { frmServer z=new frmServer(); z.setVisible(true); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); z.setLocation((screenSize.width - z.getWidth()) / 2, (screenSize.height - z.getHeight()) / 2);
} }); }
// Variables declaration - do not modify//GEN-BEGIN: variables public javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel lblIP; private javax.swing.JLabel lblStatus; private javax.swing.JTextField txtPort; // End of variables declaration//GEN-END: variables }
|