Pojawił się kolejny problem. Otóż gdy próbuję uruchomić klasę server, uruchamia się ona normalnie, jednak potem chcę uruchomić klasę klient i tu pojawia się problem, ponieważ nie pojawia się gui. Gdy uruchamiam klasy w odwrotną stronę, wszystko działa poprawnie.
Client
package com.jarek.chat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.net.Socket;
/**
* Created by Jarek on 09.11.16.
*/
public class Client extends JFrame implements ActionListener{
private JTextArea msgArea = new JTextArea(10, 32);
private JTextField msgText = new JTextField();
private JPanel btnPanel = new JPanel();
private JButton msgSend = new JButton("Send message!");
private JButton clearArea = new JButton("Clear message area!");
private JButton fileSend = new JButton("Send file!");
static Socket s;
static DataInputStream dataInputStream;
static DataOutputStream dataOutputStream;
public Client() {
msgArea.setEditable(false);
msgArea.setWrapStyleWord(true);
msgArea.setLineWrap(true);
msgSend.addActionListener(this);
clearArea.addActionListener(this);
fileSend.addActionListener(this);
Container container = getContentPane();
container.add(new JScrollPane(msgArea), BorderLayout.CENTER);
btnPanel.setLayout(new FlowLayout());
btnPanel.setPreferredSize(new Dimension(200,200));
btnPanel.add(msgSend, BorderLayout.NORTH);
btnPanel.add(clearArea, BorderLayout.SOUTH);
btnPanel.add(fileSend, BorderLayout.WEST);
container.add(btnPanel, BorderLayout.EAST);
container.add(msgText, BorderLayout.SOUTH);
setTitle("Client");
// panelMain.add(container);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int choice = JOptionPane.showConfirmDialog(container, "Exit?", "Close CHAT",
JOptionPane.OK_OPTION);
if (choice == JOptionPane.OK_OPTION)
System.exit(0); // do dopracowania
}
});
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == msgSend) {
try {
String msgOut;
msgOut = msgText.getText().trim();
dataOutputStream.writeUTF(msgOut);
msgText.setText("");
} catch (Exception i) {
JOptionPane.showMessageDialog(this, "Send message error! Check your " +
"connection or if server is closed!", "Send error!", JOptionPane.ERROR_MESSAGE);
}
}
else if(e.getSource() == clearArea) {
msgArea.setText("");
}
else if(e.getSource() == fileSend) {
SendFile sendFile = new SendFile(this);
}
}
private void listen() {
String msgIn = "";
try {
s = new Socket("127.0.0.1" , 1220);
dataInputStream = new DataInputStream(s.getInputStream());
dataOutputStream = new DataOutputStream(s.getOutputStream());
while(msgIn!="exit") {
msgIn = dataInputStream.readUTF();
System.out.println(msgIn);
msgArea.setText(msgArea.getText() + "\nServer: " + msgIn);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Receive message error! Check your " +
"connection!", "Receive error!", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Client clientFrame = new Client();
clientFrame.listen();
});
}
public void setFile(File file) {
File file1 = file;
msgArea.append(file1.getName() + "\n");
}
}
server:
package com.jarek.chat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by Jarek on 09.11.16.
*/
public class Server extends JFrame implements ActionListener{
private JTextField msgText = new JTextField();
private JButton msgSend = new JButton("Send message!");
private JPanel btnPanel = new JPanel();
private JButton clearArea = new JButton("Clear message area!");
private JTextArea msgArea = new JTextArea(10,32);
private JButton fileSend = new JButton("Send file!");
private File file;
private static ServerSocket ss;
private static Socket s;
private static DataInputStream dataInputStream;
private static DataOutputStream dataOutputStream;
public Server() {
msgArea.setEditable(false);
msgArea.setWrapStyleWord(true);
msgArea.setLineWrap(true);
msgSend.addActionListener(this);
clearArea.addActionListener(this);
fileSend.addActionListener(this);
Container container = getContentPane();
container.add(new JScrollPane(msgArea), BorderLayout.CENTER);
btnPanel.setLayout(new FlowLayout());
btnPanel.setPreferredSize(new Dimension(200,200));
btnPanel.add(msgSend, BorderLayout.NORTH);
btnPanel.add(clearArea, BorderLayout.SOUTH);
btnPanel.add(fileSend, BorderLayout.WEST);
container.add(btnPanel, BorderLayout.EAST);
container.add(msgText, BorderLayout.SOUTH);
setTitle("Server");
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int choice = JOptionPane.showConfirmDialog(container, "Exit?", "Close CHAT",
JOptionPane.OK_OPTION);
if (choice == JOptionPane.OK_OPTION)
System.exit(0); // do dopracowania
}
});
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == msgSend) {
try {
String msgOut;
msgOut = msgText.getText().trim();
dataOutputStream.writeUTF(msgOut);
msgText.setText("");
} catch (Exception i) {
JOptionPane.showMessageDialog(this, "Send message error! Check your " +
"connection or if server is closed!", "Send error!", JOptionPane.ERROR_MESSAGE);
}
}
else if(e.getSource() == clearArea) {
msgArea.setText("");
}
else if(e.getSource() == fileSend) {
SendFile sendFile = new SendFile(this);
}
}
private void listen() {
String msgIn = "";
try {
ss = new ServerSocket(1220); //number of server starting port
s = ss.accept(); //accepting connections to server
dataInputStream = new DataInputStream(s.getInputStream());
dataOutputStream = new DataOutputStream(s.getOutputStream());
while(msgIn != "exit") {
msgIn = dataInputStream.readUTF();
System.out.println(msgIn);
if(msgArea.getText()!=null)
msgArea.setText(msgArea.getText() + "\n" + msgIn);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Receive message error! Check your " +
"connection!", "Receive error!", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
Server serverFrame = new Server();
serverFrame.listen();
}
public void setFile(File file) {
this.file = file;
}
}
Pomocy :)