// Create the GUI and show it. private static void createAndShowGUI() { // Create and set up the window. frame = new JFrame("Capture Replay"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create and set up the content pane. JComponent newContentPane = new CaptureReplay(); newContentPane.setOpaque(true); // content panes must be opaque frame.setContentPane(newContentPane); frame.setPreferredSize(new Dimension(400, 400)); // Display the window. frame.pack(); frame.setVisible(true); }
public static void main(String[] args) throws IOException { ServerSocket s = new ServerSocket(PORT); System.out.println("Started: " + s); frame.setPreferredSize(new Dimension(400, 400)); frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); JMenuBar one = new JMenuBar(); JMenu second = new JMenu("OPTIONS"); // JMenuItem third = new JMenuItem("NEW GAME"); JMenuItem fourth = new JMenuItem("EXIT"); fourth.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { try { exit(evt); } catch (IOException e) { } } }); // second.add(third); second.add(fourth); one.add(second); frame.setJMenuBar(one); frame.setLayout(new java.awt.GridLayout(3, 3)); button = new JButton[10]; for (int i = 1; i < 10; i++) { button[i] = new JButton(); button[i].setBackground(Color.lightGray); button[i].setActionCommand(Integer.toString(i)); button[i].addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { print(evt.getActionCommand()); try { func(socket, evt.getActionCommand()); String check = win(); finalresult(check); if (check != "") { func(socket, check); } else { func(socket, evt.getActionCommand()); } ; } catch (IOException e) { } } }); frame.add(button[i]); } try { socket = s.accept(); frame.pack(); frame.setVisible(true); try { System.out.println("Connection accepted: " + socket); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); /* while(true) { String str=in.readLine(); print2(str); }*/ while (true) { String str = in.readLine(); if (str.equals("exit")) { System.exit(0); } if (str.equals("tie")) { JOptionPane.showMessageDialog(null, "It is a tie between the two"); System.exit(0); } else { if (str.equals("winserver")) { System.out.println("server has won"); JOptionPane.showMessageDialog(null, "the server has won the game"); System.exit(0); } else { if (str.equals("winclient")) { JOptionPane.showMessageDialog(null, "the client has won the game"); System.exit(0); } else { System.out.println(str); print2(str); } } } } } catch (Exception e) { } // finally {socket.close(); // } } catch (Exception e) { } // finally {s.close(); // } }