gui在服务器停止之前不会启动

cfh9epnr  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(366)

我启动gui有困难。它与我的 ClientController . 启动服务器后,我也会启动 ClientController ,之后,我的gui应该会随之运行。但直到我停止服务器,它才出现,然后它终于出现了。

public class ClientController {
    private HashMap<String, ArrayList<String>> drinkar = null;//Controller
    private ServerHandler serverH = new ServerHandler();
    private GUI gui = new GUI(this);

    public void searchDrinks(String jtf) {
        drinkar = serverH.drinksStartsWith(jtf);
        if(drinkar!=null) {
            gui.setDrinks(new ArrayList(drinkar.entrySet()));
            gui.setIngredients(new ArrayList());
        }
        System.out.println(drinkar);
    }

    public void drinkInformation(String drink) {
        ArrayList<String> result = drinkar.get(drink);
        if(result!=null) {
            gui.setIngredients(result);
        }
    }

    public static void main(String[] args) {
        ClientController controller = new ClientController();
    }
}

这是我的gui课程

public class GUI implements ActionListener {

private JFrame frame = new JFrame("Dryck & Ingrediens"); // GUI
private JTextField jtf = new JTextField();// GUI
private JTextArea jl1 = new JTextArea();// GUI
private JList jl = new JList();// GUI
private JButton b = new JButton("Sök");
private JScrollPane js = new JScrollPane(jl);// GUI
private JLabel lab = new JLabel("Ange dryck");//GUI
private JLabel lab1 = new JLabel("Walid Shams");
private JLabel lab2 = new JLabel("Kushtrim Brahimi");
private ClientController controller;
//    private HashMap<String, ArrayList<String>> drinkar = null;//Controller
//    private ServerHandler serverH = new ServerHandler();

public GUI(ClientController controller) {
    this.controller = controller;

    frame.getContentPane().setLayout(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(50, 300, 420, 400);
    frame.setResizable(false);
    frame.setVisible(true);
    js.add(jl);
    js.add(jl1);
    jl1.setEditable(false);
    lab.setBounds(90, 20, 130, 20);
    lab1.setBounds(300, 310, 130, 20);
    lab2.setBounds(300, 330, 130,20);
    jtf.setBounds(50, 40, 150, 40);
    b.setBounds(230, 40, 100, 40);
    jl.setBounds(50, 90, 150, 200);
    jl1.setBounds(210, 90, 150, 200);
    Container con = frame.getContentPane();
    con.setBackground(Color.cyan);

    jl.addListSelectionListener(new ListSelectionListener() {

        // sorterar ingredienserna för vald drink och skriver ut i textArea
        public void valueChanged(ListSelectionEvent e) {
             if (jl.getSelectedIndex() != -1) {
                 String item = (String) jl.getSelectedValue();
                 jl1.setText("");
//                     controller.drinkInformation(item);
             }else{
                 jl1.setText("");
             }
        }
    });
    frame.add(jtf);
    frame.add(jl);
    frame.add(jl1);
    frame.add(lab);
    frame.add(lab1);
    frame.add(lab2);
    frame.add(js);
    frame.add(b);

    b.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(b)){
        if (jtf.getText().length() > 0) {
            controller.searchDrinks(jtf.getText());
        }
    }

}

public void setDrinks(ArrayList<String> drinks) {
    DefaultListModel model = new DefaultListModel();

    for(int i = 0; i < drinks.size(); i++){
        model.addElement(drinks.get(i));
    }
    jl.setModel(model);

}

public void setIngredients(ArrayList<String> ingredients) {

    for(String ingredient : ingredients){
        jl1.append(ingredient + "\n");
    }
}

}

最后是我的服务器

public class Server {

    private ServerSocket server;
    private Socket socket;
    private ObjectOutputStream oos;
    private DataOutputStream dos;
    private DataInputStream dis;
    private HashMap<String, ArrayList<String>> drinkar = new HashMap<String, ArrayList<String>>();
    private int port;

    public Server(int port){
        this.port = port;
        Thread connectThread = new Thread( new Connect() );
        connectThread.start();
    }

    // låter en klient koppla upp sig till servern
    private class Connect implements Runnable {
        public void run() {
            try {
                server = new ServerSocket(port);
                System.out.println("Server running");
                while (true) {
                    socket = server.accept();
                    Thread clientThread = new Thread( new TalkToClient( socket ) );
            clientThread.start();
                    System.out.println("someone connected");
                }
            } catch (IOException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                server.close();
            } catch( Exception e ) {}
        }
    }

    // sköter kommunikationen med klienter
    private class TalkToClient implements Runnable{
        private Socket socket;
        public TalkToClient( Socket socket ) {
            this.socket = socket;
        }
        public void run() {
            String clientMsg;
            try {
                dis = new DataInputStream(socket.getInputStream());
                while ((clientMsg = dis.readUTF()) != null) {
                    if (clientMsg.length() > 0) {
                        dos = new DataOutputStream(socket.getOutputStream());
                        dos.writeUTF("read");
                        drinkar = Mysql.getDrinks(clientMsg);
                        sendNames();
                    }
                }
            } catch (Exception e) {
                System.out.println(e);
            }

        }

    }

    // skickar med hashmapen med writeObject
    public void sendNames() {

        try {
            oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(drinkar);
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) {
        new Server(4444);
    }
}
dba5bblo

dba5bblo1#

当服务器启动时,它会写入输出流。它锁定试图连接到服务器的客户端。

nnsrf1az

nnsrf1az2#

我想一定是serverhandler代码有问题。你能给这个班贴代码吗?当您将gui的初始化放在serverhandler之前时会发生什么?

相关问题