-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGui.java
More file actions
163 lines (146 loc) · 5.73 KB
/
Copy pathGui.java
File metadata and controls
163 lines (146 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.lang.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gui {
// Connect status constants
final static int DISCONNECTED = 0;
final static int BEGIN_CONNECT = 1;
final static int CONNECTED = 2;
// Various GUI components and info
public static JFrame mainFrame = null;
public static JTextArea chatText = null;
public static JTextField chatLine = null;
public static JLabel statusBar = null;
public static JTextField ipField = null;
public static JTextField portField = null;
public static JRadioButton hostOption = null;
public static JRadioButton guestOption = null;
public static JButton connectButton = null;
public static JButton disconnectButton = null;
// Connection info
public static String hostIP = "localhost";
public static int port = 1234;
public static int connectionStatus = DISCONNECTED;
public static boolean isHost = true;
private static JPanel initOptionsPane() {
JPanel pane = null;
ActionAdapter buttonListener = null;
// Create an options pane
JPanel optionsPane = new JPanel(new GridLayout(4, 1));
// IP address input
pane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
pane.add(new JLabel("Host IP:"));
ipField = new JTextField(10); ipField.setText(hostIP);
ipField.setEditable(true);
pane.add(ipField);
optionsPane.add(pane);
// Port input
pane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
pane.add(new JLabel("Port:"));
portField = new JTextField(10); portField.setEditable(true);
portField.setText((new Integer(port)).toString());
pane.add(portField);
optionsPane.add(pane);
// Host/guest option
ButtonGroup bg = new ButtonGroup();
hostOption = new JRadioButton("Host", true);
hostOption.setMnemonic(KeyEvent.VK_H);
guestOption = new JRadioButton("Guest", false);
guestOption.setMnemonic(KeyEvent.VK_G);
bg.add(hostOption);
bg.add(guestOption);
pane = new JPanel(new GridLayout(1, 2));
pane.add(hostOption);
pane.add(guestOption);
optionsPane.add(pane);
// Connect/disconnect buttons
JPanel buttonPane = new JPanel(new GridLayout(1, 2));
buttonListener = new ActionAdapter() {
public void actionPerformed(ActionEvent e) {
// Request a connection initiation
if (e.getActionCommand().equals("connect")) {
connectButton.setEnabled(false);
disconnectButton.setEnabled(true);
connectionStatus = BEGIN_CONNECT;
ipField.setEnabled(false);
portField.setEnabled(false);
hostOption.setEnabled(false);
guestOption.setEnabled(false);
chatLine.setEnabled(true);
statusBar.setText("Online");
mainFrame.repaint();
}
// Disconnect
else {
connectButton.setEnabled(true);
disconnectButton.setEnabled(false);
connectionStatus = DISCONNECTED;
ipField.setEnabled(true);
portField.setEnabled(true);
hostOption.setEnabled(true);
guestOption.setEnabled(true);
chatLine.setText(""); chatLine.setEnabled(false);
statusBar.setText("Offline");
mainFrame.repaint();
}
}
};
connectButton = new JButton("Connect");
connectButton.setMnemonic(KeyEvent.VK_C);
connectButton.setActionCommand("connect");
connectButton.addActionListener(buttonListener);
connectButton.setEnabled(true);
disconnectButton = new JButton("Disconnect");
disconnectButton.setMnemonic(KeyEvent.VK_D);
disconnectButton.setActionCommand("disconnect");
disconnectButton.addActionListener(buttonListener);
disconnectButton.setEnabled(false);
buttonPane.add(connectButton);
buttonPane.add(disconnectButton);
optionsPane.add(buttonPane);
return optionsPane;
}
private static void initGUI() {
// Set up the status bar
statusBar = new JLabel();
statusBar.setText("Offline");
// 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);
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);
}
public static void main(String args[]) {
initGUI();
}
}
// Action adapter for easy event-listener coding
class ActionAdapter implements ActionListener {
public void actionPerformed(ActionEvent e) {}
}