• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

question-closed java uruchamianie jFrame podczas działania innego jFrame

Object Storage Arubacloud
0 głosów
443 wizyt
pytanie zadane 6 grudnia 2016 w Programowanie przez jaroslaw.slaby Początkujący (410 p.)
zamknięte 6 grudnia 2016 przez jaroslaw.slaby

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 :)

komentarz zamknięcia: Rozwiązano problem przed otrzymaniem odpowiedzi

Podobne pytania

0 głosów
2 odpowiedzi 321 wizyt
pytanie zadane 5 grudnia 2016 w Java przez jaroslaw.slaby Początkujący (410 p.)
0 głosów
2 odpowiedzi 2,672 wizyt
pytanie zadane 22 listopada 2016 w Java przez itcloud Gaduła (3,380 p.)
+1 głos
1 odpowiedź 250 wizyt
pytanie zadane 10 kwietnia 2022 w Java przez coriver Nowicjusz (170 p.)

92,552 zapytań

141,399 odpowiedzi

319,534 komentarzy

61,938 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...