|
@ -14,12 +14,12 @@ import javax.swing.JTextArea; |
|
|
import javax.swing.JTextField; |
|
|
import javax.swing.JTextField; |
|
|
|
|
|
|
|
|
public class ChatGUI implements ActionListener { |
|
|
public class ChatGUI implements ActionListener { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JMenuItem exit = new JMenuItem("Exit"); |
|
|
JMenuItem exit = new JMenuItem("Exit"); |
|
|
JMenuItem black = new JMenuItem("black"); |
|
|
|
|
|
JMenuItem red = new JMenuItem("red"); |
|
|
|
|
|
JMenuItem blue = new JMenuItem("blue"); |
|
|
|
|
|
JMenuItem green = new JMenuItem("green"); |
|
|
|
|
|
|
|
|
JMenuItem black = new JMenuItem("black"); |
|
|
|
|
|
JMenuItem red = new JMenuItem("red"); |
|
|
|
|
|
JMenuItem blue = new JMenuItem("blue"); |
|
|
|
|
|
JMenuItem green = new JMenuItem("green"); |
|
|
JTextField inputTextField = new JTextField(); |
|
|
JTextField inputTextField = new JTextField(); |
|
|
JTextArea outputTextArea = new JTextArea(); |
|
|
JTextArea outputTextArea = new JTextArea(); |
|
|
|
|
|
|
|
@ -27,49 +27,49 @@ public class ChatGUI implements ActionListener { |
|
|
|
|
|
|
|
|
public ChatGUI() { |
|
|
public ChatGUI() { |
|
|
gui = new JFrame(); |
|
|
gui = new JFrame(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set up the main frame |
|
|
// Set up the main frame |
|
|
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|
|
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|
|
gui.setTitle("java-chat"); |
|
|
gui.setTitle("java-chat"); |
|
|
|
|
|
|
|
|
gui.setLayout(new BorderLayout()); |
|
|
gui.setLayout(new BorderLayout()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JMenuBar bar = new JMenuBar(); |
|
|
JMenuBar bar = new JMenuBar(); |
|
|
gui.setJMenuBar(bar); |
|
|
|
|
|
|
|
|
|
|
|
JMenu options = new JMenu("options"); |
|
|
|
|
|
JMenu colors = new JMenu("font-colors"); |
|
|
|
|
|
|
|
|
|
|
|
JMenu menu = new JMenu("File"); |
|
|
|
|
|
menu.add(exit); |
|
|
|
|
|
menu.add(options); |
|
|
|
|
|
bar.add(menu); |
|
|
|
|
|
options.add(colors); |
|
|
|
|
|
|
|
|
|
|
|
colors.add(black); |
|
|
|
|
|
colors.add(red); |
|
|
|
|
|
colors.add(blue); |
|
|
|
|
|
colors.add(green); |
|
|
|
|
|
|
|
|
|
|
|
red.addActionListener(this); |
|
|
|
|
|
blue.addActionListener(this); |
|
|
|
|
|
black.addActionListener(this); |
|
|
|
|
|
green.addActionListener(this); |
|
|
|
|
|
|
|
|
|
|
|
exit.addActionListener(this); |
|
|
|
|
|
|
|
|
|
|
|
bar.add(menu); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gui.setJMenuBar(bar); |
|
|
|
|
|
|
|
|
|
|
|
JMenu options = new JMenu("options"); |
|
|
|
|
|
JMenu colors = new JMenu("font-colors"); |
|
|
|
|
|
|
|
|
|
|
|
JMenu menu = new JMenu("File"); |
|
|
|
|
|
menu.add(exit); |
|
|
|
|
|
menu.add(options); |
|
|
|
|
|
bar.add(menu); |
|
|
|
|
|
options.add(colors); |
|
|
|
|
|
|
|
|
|
|
|
colors.add(black); |
|
|
|
|
|
colors.add(red); |
|
|
|
|
|
colors.add(blue); |
|
|
|
|
|
colors.add(green); |
|
|
|
|
|
|
|
|
|
|
|
red.addActionListener(this); |
|
|
|
|
|
blue.addActionListener(this); |
|
|
|
|
|
black.addActionListener(this); |
|
|
|
|
|
green.addActionListener(this); |
|
|
|
|
|
|
|
|
|
|
|
exit.addActionListener(this); |
|
|
|
|
|
|
|
|
|
|
|
bar.add(menu); |
|
|
|
|
|
|
|
|
// Set up the output text area with scrolling |
|
|
// Set up the output text area with scrolling |
|
|
JScrollPane outputScrollPane = new JScrollPane(outputTextArea); |
|
|
JScrollPane outputScrollPane = new JScrollPane(outputTextArea); |
|
|
gui.add(outputScrollPane, BorderLayout.CENTER); |
|
|
gui.add(outputScrollPane, BorderLayout.CENTER); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set up the input text field |
|
|
// Set up the input text field |
|
|
inputTextField.setPreferredSize(new Dimension(0, 30)); |
|
|
inputTextField.setPreferredSize(new Dimension(0, 30)); |
|
|
gui.add(inputTextField, BorderLayout.SOUTH); |
|
|
gui.add(inputTextField, BorderLayout.SOUTH); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inputTextField.addActionListener(this); |
|
|
inputTextField.addActionListener(this); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set the size of the GUI to be a quarter of the screen size |
|
|
// Set the size of the GUI to be a quarter of the screen size |
|
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
|
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
|
|
int quarterWidth = screenSize.width / 2; |
|
|
int quarterWidth = screenSize.width / 2; |
|
@ -89,21 +89,21 @@ public class ChatGUI implements ActionListener { |
|
|
System.exit(0); |
|
|
System.exit(0); |
|
|
} |
|
|
} |
|
|
if (e.getSource() == inputTextField) { |
|
|
if (e.getSource() == inputTextField) { |
|
|
String inputText = inputTextField.getText(); |
|
|
|
|
|
outputTextArea.append(inputText + "\n"); |
|
|
|
|
|
inputTextField.setText(""); |
|
|
|
|
|
|
|
|
String inputText = inputTextField.getText(); |
|
|
|
|
|
outputTextArea.append(inputText + "\n"); |
|
|
|
|
|
inputTextField.setText(""); |
|
|
|
|
|
} |
|
|
|
|
|
if (e.getSource() == red) { |
|
|
|
|
|
outputTextArea.setForeground(Color.RED); |
|
|
} |
|
|
} |
|
|
if (e.getSource() == red) { |
|
|
|
|
|
outputTextArea.setForeground(Color.RED); |
|
|
|
|
|
} |
|
|
|
|
|
if (e.getSource() == blue) { |
|
|
|
|
|
outputTextArea.setForeground(Color.BLUE); |
|
|
|
|
|
}else if (e.getSource() == black) { |
|
|
|
|
|
outputTextArea.setForeground(Color.BLACK); |
|
|
|
|
|
} else if (e.getSource() == green) { |
|
|
|
|
|
outputTextArea.setForeground(Color.GREEN); |
|
|
|
|
|
|
|
|
if (e.getSource() == blue) { |
|
|
|
|
|
outputTextArea.setForeground(Color.BLUE); |
|
|
|
|
|
} else if (e.getSource() == black) { |
|
|
|
|
|
outputTextArea.setForeground(Color.BLACK); |
|
|
|
|
|
} else if (e.getSource() == green) { |
|
|
|
|
|
outputTextArea.setForeground(Color.GREEN); |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |