|
@ -17,7 +17,7 @@ import javax.swing.JTextArea; |
|
|
import javax.swing.JTextField; |
|
|
import javax.swing.JTextField; |
|
|
|
|
|
|
|
|
public class ChatGUI implements ActionListener { |
|
|
public class ChatGUI implements ActionListener { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Menu items for font sizes |
|
|
// Menu items for font sizes |
|
|
JMenuItem small = new JMenuItem("small-font"); |
|
|
JMenuItem small = new JMenuItem("small-font"); |
|
|
JMenuItem medium = new JMenuItem("medium-font"); |
|
|
JMenuItem medium = new JMenuItem("medium-font"); |
|
@ -28,9 +28,9 @@ public class ChatGUI implements ActionListener { |
|
|
JMenuItem red = new JMenuItem("red"); |
|
|
JMenuItem red = new JMenuItem("red"); |
|
|
JMenuItem blue = new JMenuItem("blue"); |
|
|
JMenuItem blue = new JMenuItem("blue"); |
|
|
JMenuItem green = new JMenuItem("green"); |
|
|
JMenuItem green = new JMenuItem("green"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JMenuItem exit = new JMenuItem("Exit"); |
|
|
JMenuItem exit = new JMenuItem("Exit"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JTextField inputTextField = new JTextField(); |
|
|
JTextField inputTextField = new JTextField(); |
|
|
JTextArea outputTextArea = new JTextArea(); |
|
|
JTextArea outputTextArea = new JTextArea(); |
|
|
JButton sendButton = new JButton("Senden"); |
|
|
JButton sendButton = new JButton("Senden"); |
|
@ -44,11 +44,11 @@ public class ChatGUI implements ActionListener { |
|
|
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()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set up the menu bar |
|
|
// Set up the menu bar |
|
|
JMenuBar bar = new JMenuBar(); |
|
|
JMenuBar bar = new JMenuBar(); |
|
|
gui.setJMenuBar(bar); |
|
|
gui.setJMenuBar(bar); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create menu items and menus |
|
|
// Create menu items and menus |
|
|
JMenu options = new JMenu("options"); |
|
|
JMenu options = new JMenu("options"); |
|
|
JMenu colors = new JMenu("font-colors"); |
|
|
JMenu colors = new JMenu("font-colors"); |
|
@ -65,11 +65,11 @@ public class ChatGUI implements ActionListener { |
|
|
colors.add(red); |
|
|
colors.add(red); |
|
|
colors.add(blue); |
|
|
colors.add(blue); |
|
|
colors.add(green); |
|
|
colors.add(green); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size.add(small); |
|
|
size.add(small); |
|
|
size.add(medium); |
|
|
size.add(medium); |
|
|
size.add(large); |
|
|
size.add(large); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Register action listeners for menu items |
|
|
// Register action listeners for menu items |
|
|
red.addActionListener(this); |
|
|
red.addActionListener(this); |
|
|
blue.addActionListener(this); |
|
|
blue.addActionListener(this); |
|
@ -82,19 +82,20 @@ public class ChatGUI implements ActionListener { |
|
|
exit.addActionListener(this); |
|
|
exit.addActionListener(this); |
|
|
|
|
|
|
|
|
bar.add(menu); |
|
|
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 panel with text field and send button |
|
|
JPanel inputPanel = new JPanel(new BorderLayout()); |
|
|
JPanel inputPanel = new JPanel(new BorderLayout()); |
|
|
inputPanel.add(inputTextField, BorderLayout.CENTER); |
|
|
|
|
|
inputPanel.add(sendButton, BorderLayout.EAST); |
|
|
|
|
|
gui.add(inputPanel, BorderLayout.SOUTH); |
|
|
|
|
|
|
|
|
inputPanel.add(inputTextField, BorderLayout.CENTER); |
|
|
|
|
|
inputPanel.add(sendButton, BorderLayout.EAST); |
|
|
|
|
|
gui.add(inputPanel, BorderLayout.SOUTH); |
|
|
|
|
|
|
|
|
inputTextField.addActionListener(this); |
|
|
inputTextField.addActionListener(this); |
|
|
sendButton.addActionListener(this); |
|
|
sendButton.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; |
|
@ -102,6 +103,7 @@ public class ChatGUI implements ActionListener { |
|
|
gui.setSize(quarterWidth, quarterHeight); |
|
|
gui.setSize(quarterWidth, quarterHeight); |
|
|
gui.setVisible(true); |
|
|
gui.setVisible(true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// main methode zum Testen, müsste bei Implementation entfernt werden |
|
|
// main methode zum Testen, müsste bei Implementation entfernt werden |
|
|
public static void main(String[] args) { |
|
|
public static void main(String[] args) { |
|
|
|
|
|
|
|
@ -114,13 +116,13 @@ public class ChatGUI implements ActionListener { |
|
|
System.exit(0); |
|
|
System.exit(0); |
|
|
} |
|
|
} |
|
|
if (e.getSource() == inputTextField || e.getSource() == sendButton) { |
|
|
if (e.getSource() == inputTextField || e.getSource() == sendButton) { |
|
|
String inputText = inputTextField.getText(); |
|
|
|
|
|
outputTextArea.append(inputText + "\n"); |
|
|
|
|
|
inputTextField.setText(""); |
|
|
|
|
|
|
|
|
String inputText = inputTextField.getText(); |
|
|
|
|
|
outputTextArea.append(inputText + "\n"); |
|
|
|
|
|
inputTextField.setText(""); |
|
|
} |
|
|
} |
|
|
if (e.getSource() == red) { |
|
|
if (e.getSource() == red) { |
|
|
outputTextArea.setForeground(Color.RED); |
|
|
outputTextArea.setForeground(Color.RED); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else if (e.getSource() == blue) { |
|
|
} else if (e.getSource() == blue) { |
|
|
outputTextArea.setForeground(Color.BLUE); |
|
|
outputTextArea.setForeground(Color.BLUE); |
|
|
} else if (e.getSource() == black) { |
|
|
} else if (e.getSource() == black) { |
|
@ -128,17 +130,15 @@ public class ChatGUI implements ActionListener { |
|
|
} else if (e.getSource() == green) { |
|
|
} else if (e.getSource() == green) { |
|
|
outputTextArea.setForeground(Color.GREEN); |
|
|
outputTextArea.setForeground(Color.GREEN); |
|
|
} |
|
|
} |
|
|
if(e.getSource() == small) { |
|
|
|
|
|
outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 12)); |
|
|
|
|
|
} |
|
|
|
|
|
if(e.getSource() == medium) { |
|
|
|
|
|
outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 16)); |
|
|
|
|
|
} |
|
|
|
|
|
if(e.getSource() == large) { |
|
|
|
|
|
outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 20)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (e.getSource() == small) { |
|
|
|
|
|
outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 12)); |
|
|
|
|
|
} |
|
|
|
|
|
if (e.getSource() == medium) { |
|
|
|
|
|
outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 16)); |
|
|
|
|
|
} |
|
|
|
|
|
if (e.getSource() == large) { |
|
|
|
|
|
outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 20)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |