From 14c221315bb4233553831551a62ed4a69070f3e3 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 03:40:32 +0100 Subject: [PATCH] =?UTF-8?q?hinzuf=C3=BCgen=20senden-button?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatGUI.java | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index de20bd9..d5efb58 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -6,10 +6,12 @@ import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; +import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; @@ -31,6 +33,7 @@ public class ChatGUI implements ActionListener { JTextField inputTextField = new JTextField(); JTextArea outputTextArea = new JTextArea(); + JButton sendButton = new JButton("Senden"); JFrame gui; @@ -79,17 +82,19 @@ public class ChatGUI implements ActionListener { exit.addActionListener(this); bar.add(menu); - + // Set up the output text area with scrolling JScrollPane outputScrollPane = new JScrollPane(outputTextArea); gui.add(outputScrollPane, BorderLayout.CENTER); - - // Set up the input text field - inputTextField.setPreferredSize(new Dimension(0, 30)); - gui.add(inputTextField, BorderLayout.SOUTH); + + JPanel inputPanel = new JPanel(new BorderLayout()); + inputPanel.add(inputTextField, BorderLayout.CENTER); + inputPanel.add(sendButton, BorderLayout.EAST); + gui.add(inputPanel, BorderLayout.SOUTH); inputTextField.addActionListener(this); - + sendButton.addActionListener(this); + // Set the size of the GUI to be a quarter of the screen size Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int quarterWidth = screenSize.width / 2; @@ -108,10 +113,10 @@ public class ChatGUI implements ActionListener { if (e.getSource() == exit) { System.exit(0); } - if (e.getSource() == inputTextField) { - String inputText = inputTextField.getText(); - outputTextArea.append(inputText + "\n"); - inputTextField.setText(""); + if (e.getSource() == inputTextField || e.getSource() == sendButton) { + String inputText = inputTextField.getText(); + outputTextArea.append(inputText + "\n"); + inputTextField.setText(""); } if (e.getSource() == red) { outputTextArea.setForeground(Color.RED);