|
|
@ -1,11 +1,69 @@ |
|
|
|
import javax.swing.*; |
|
|
|
import javax.swing.border.EmptyBorder; |
|
|
|
import java.awt.*; |
|
|
|
import java.awt.event.ActionEvent; |
|
|
|
import java.awt.event.ActionListener; |
|
|
|
import java.awt.event.KeyEvent; |
|
|
|
import java.awt.event.KeyListener; |
|
|
|
import java.io.*; |
|
|
|
import java.net.Socket; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.util.Date; |
|
|
|
|
|
|
|
public class ChatClient extends JFrame implements KeyListener { |
|
|
|
public static class TR extends JFrame { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private JScrollPane jScrollPane; |
|
|
|
private JPanel container; |
|
|
|
|
|
|
|
public TR() throws HeadlessException { |
|
|
|
super("Notizen"); |
|
|
|
this.jScrollPane = jScrollPane; |
|
|
|
|
|
|
|
initComponents(); |
|
|
|
} |
|
|
|
|
|
|
|
private void initComponents() { |
|
|
|
setVisible(true); |
|
|
|
setBounds(1180, 170, 350, 550); |
|
|
|
setPreferredSize(new Dimension(300, 500)); |
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|
|
|
setResizable(false); |
|
|
|
|
|
|
|
JPanel root = new JPanel(); |
|
|
|
root.setPreferredSize(new Dimension(300, 500)); |
|
|
|
|
|
|
|
setContentPane(root); |
|
|
|
|
|
|
|
jScrollPane = new JScrollPane(container, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); |
|
|
|
jScrollPane.setPreferredSize(new Dimension(300, 500)); |
|
|
|
root.add(jScrollPane); |
|
|
|
|
|
|
|
container = new JPanel(); |
|
|
|
container.setBorder(new EmptyBorder(3, 3, 3, 3)); |
|
|
|
container.setBackground(new Color(0x839683)); |
|
|
|
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS)); |
|
|
|
} |
|
|
|
|
|
|
|
private JPanel setupPanel(String header) { |
|
|
|
JPanel panel = new JPanel(); |
|
|
|
panel.setPreferredSize(new Dimension(270, 150)); |
|
|
|
panel.setLayout(new BorderLayout()); |
|
|
|
|
|
|
|
container.add(panel); |
|
|
|
container.add(Box.createVerticalStrut(5)); |
|
|
|
|
|
|
|
JLabel jLabel = new JLabel(header); |
|
|
|
jLabel.setHorizontalAlignment(JLabel.LEFT); |
|
|
|
panel.add(jLabel, BorderLayout.NORTH); |
|
|
|
|
|
|
|
return panel; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
private String address; |
|
|
|
private String connectionFailedMessage; |
|
|
|
private Socket connectionToServer; |
|
|
@ -16,6 +74,8 @@ public class ChatClient extends JFrame implements KeyListener { |
|
|
|
private JTextArea outputTextArea; |
|
|
|
private JTextField inputTextField; |
|
|
|
private JScrollPane outputScrollPane; |
|
|
|
private Timer timer; |
|
|
|
private JLabel timeLabel; |
|
|
|
|
|
|
|
public ChatClient() { |
|
|
|
super("Chat"); |
|
|
@ -26,13 +86,34 @@ public class ChatClient extends JFrame implements KeyListener { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void initTimer() { |
|
|
|
timer = new Timer(1000, new ActionListener() { |
|
|
|
@Override |
|
|
|
public void actionPerformed(ActionEvent e) { |
|
|
|
updateTime(); |
|
|
|
} |
|
|
|
}); |
|
|
|
timer.start(); |
|
|
|
} |
|
|
|
|
|
|
|
private void updateTime() { |
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); |
|
|
|
String currentTime = sdf.format(new Date()); |
|
|
|
timeLabel.setText("Current Time: " + currentTime); |
|
|
|
} |
|
|
|
|
|
|
|
private void initGui() { |
|
|
|
outputTextArea = new JTextArea(); |
|
|
|
outputTextArea.setEditable(false); |
|
|
|
outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); |
|
|
|
outputTextArea.setBackground(Color.lightGray); |
|
|
|
|
|
|
|
outputScrollPane = new JScrollPane(outputTextArea); |
|
|
|
|
|
|
|
timeLabel = new JLabel(); |
|
|
|
updateTime(); |
|
|
|
add(timeLabel, BorderLayout.NORTH); |
|
|
|
|
|
|
|
inputTextField = new JTextField(); |
|
|
|
inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); |
|
|
|
inputTextField.addKeyListener(this); |
|
|
@ -52,6 +133,7 @@ public class ChatClient extends JFrame implements KeyListener { |
|
|
|
toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); |
|
|
|
|
|
|
|
initGui(); |
|
|
|
initTimer(); |
|
|
|
|
|
|
|
while (true) { |
|
|
|
String message = fromServerReader.readLine(); |
|
|
@ -107,6 +189,8 @@ public class ChatClient extends JFrame implements KeyListener { |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
SwingUtilities.invokeLater(() -> new TR()); |
|
|
|
new ChatClient(); |
|
|
|
} |
|
|
|
} |
|
|
|
|