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

时间:2012-08-24 14:41:51

标签: java user-interface client-server

我在启动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);
    }
}

2 个答案:

答案 0 :(得分:1)

我认为必须是ServerHandler代码中的问题。你可以发布这个课程的代码吗?在ServerHandler之前放置GUI初始化会发生什么?

答案 1 :(得分:0)

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