// Initialize all the GUI components and display the frame private static void initGUI() { // Set up the status bar statusField = new JLabel(); statusField.setText(statusMessages[DISCONNECTED]); statusColor = new JTextField(1); statusColor.setBackground(Color.red); statusColor.setEditable(false); statusBar = new JPanel(new BorderLayout()); statusBar.add(statusColor, BorderLayout.WEST); statusBar.add(statusField, BorderLayout.CENTER); // Set up the options pane JPanel optionsPane = initOptionsPane(); // Set up the chat pane JPanel chatPane = new JPanel(new BorderLayout()); chatText = new JTextArea(10, 20); chatText.setLineWrap(true); chatText.setEditable(false); chatText.setForeground(Color.blue); JScrollPane chatTextPane = new JScrollPane( chatText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); chatLine = new JTextField(); chatLine.setEnabled(false); chatLine.addActionListener( new ActionAdapter() { public void actionPerformed(ActionEvent e) { String s = chatLine.getText(); if (!s.equals("")) { appendToChatBox("OUTGOING: " + s + "\n"); chatLine.selectAll(); // Send the string sendString(s); } } }); chatPane.add(chatLine, BorderLayout.SOUTH); chatPane.add(chatTextPane, BorderLayout.CENTER); chatPane.setPreferredSize(new Dimension(200, 200)); // Set up the main pane JPanel mainPane = new JPanel(new BorderLayout()); mainPane.add(statusBar, BorderLayout.SOUTH); mainPane.add(optionsPane, BorderLayout.WEST); mainPane.add(chatPane, BorderLayout.CENTER); // Set up the main frame mainFrame = new JFrame("Simple TCP Chat"); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setContentPane(mainPane); mainFrame.setSize(mainFrame.getPreferredSize()); mainFrame.setLocation(200, 200); mainFrame.pack(); mainFrame.setVisible(true); }
// Checks the current state and sets the enables/disables // accordingly public void run() { switch (connectionStatus) { case DISCONNECTED: connectButton.setEnabled(true); disconnectButton.setEnabled(false); ipField.setEnabled(true); portField.setEnabled(true); hostOption.setEnabled(true); guestOption.setEnabled(true); chatLine.setText(""); chatLine.setEnabled(false); statusColor.setBackground(Color.red); break; case DISCONNECTING: connectButton.setEnabled(false); disconnectButton.setEnabled(false); ipField.setEnabled(false); portField.setEnabled(false); hostOption.setEnabled(false); guestOption.setEnabled(false); chatLine.setEnabled(false); statusColor.setBackground(Color.orange); break; case CONNECTED: connectButton.setEnabled(false); disconnectButton.setEnabled(true); ipField.setEnabled(false); portField.setEnabled(false); hostOption.setEnabled(false); guestOption.setEnabled(false); chatLine.setEnabled(true); statusColor.setBackground(Color.green); break; case BEGIN_CONNECT: connectButton.setEnabled(false); disconnectButton.setEnabled(false); ipField.setEnabled(false); portField.setEnabled(false); hostOption.setEnabled(false); guestOption.setEnabled(false); chatLine.setEnabled(false); chatLine.grabFocus(); statusColor.setBackground(Color.orange); break; } // Make sure that the button/text field states are consistent // with the internal states ipField.setText(hostIP); portField.setText((new Integer(port)).toString()); hostOption.setSelected(isHost); guestOption.setSelected(!isHost); statusField.setText(statusString); chatText.append(toAppend.toString()); toAppend.setLength(0); mainFrame.repaint(); }