方法不起作用

时间:2012-08-22 21:53:13

标签: java methods arraylist

我有一种方法,我试图从另一个类接收一个arraylist并将其存储在一个新的arraylist中。当我点击按钮时,该方法应该运行,但我什么都没得到。相反,我的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 HashMap<String, ArrayList<String>> drinkar = null;//Controller
private ServerHandler serverH = new ServerHandler();

public GUI() {


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



    jtf.addKeyListener(new KeyListener() {

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {

                if (jtf.getText().length() > 0) {
                    serverH.writeMsg(jtf.getText());
                }
                else {
                    String[] empty = new String[]{""};
                    jl.setListData(empty);
                }
        }
    });


    jl.addListSelectionListener(new ListSelectionListener() {

        public void valueChanged(ListSelectionEvent e) {
             if (jl.getSelectedIndex() != -1) {
                 String item = (String) jl.getSelectedValue();
                 jl1.setText("");
                 for (String ingrediens : drinkar.get(item)) {
                     jl1.append(ingrediens + "\n");
                 }
             }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);
}


public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(b)){
        getArrayList();
    }

}

// tar emot arrayen, lagrar i ny array och visar i JList
public void getArrayList() {

    DefaultListModel model = new DefaultListModel();
    ArrayList<String> drinks = new ArrayList<String>();

    drinks = serverH.receiveArrayList();
    System.out.println("the contents of the list" + drinks);

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

}


public static void main(String[] args) {
    GUI g = new GUI();

}

}

这是我的serverHandler类,我试图接收Arraylist。但它似乎没有进入我的GUI。

public class ServerHandler  {

private Socket socket;// ServerHandler
private DataInputStream dis;// ServerHandler
private ObjectInputStream ois = null;// ServerHandler
private DataOutputStream dos;// ServerHandler
private HashMap<String, ArrayList<String>> drinkar;

public ServerHandler(){

    socket = new Socket();
    InetSocketAddress ipPort = new InetSocketAddress("127.0.0.1", 4444);
    try {
        socket.connect(ipPort);
        dis = new DataInputStream(socket.getInputStream());
        dos = new DataOutputStream(socket.getOutputStream());
    } catch (Exception e) {
    }
//        new Thread(this).start();
}


public ArrayList<String> receiveArrayList() {
    String fromServer;
    try {
        while ((fromServer = dis.readUTF()) != null) {
            if (fromServer.equals("read")) {
                getArrayList();
            }

        }
    } catch (Exception e) {
    }
    return getArrayList();

}

public void writeMsg(String jtf){

    if (dos != null) {
        if (jtf.length() > 0) {
            try {
                dos = new DataOutputStream(socket.getOutputStream());
                dos.writeUTF(jtf);
            } catch (IOException ex) {
                System.out.print(ex);
            }
        }
    }
}

public ArrayList<String> getArrayList(){

ArrayList<String> arr = new ArrayList<String>();

    try {
        ois = new ObjectInputStream(socket.getInputStream());
        drinkar = (HashMap<String, ArrayList<String>>) (ois.readObject());

        Iterator it = drinkar.keySet().iterator();
        while(it.hasNext()){
            String temp = (String) it.next();
            arr.add(temp);
        }
        System.out.println(arr);


    } catch (ClassNotFoundException ex) {
        System.out.println(ex);
    } catch (IOException ex) {
        System.out.println(ex);
    }

    return arr;

}

public ArrayList<String> drinks(){

    ArrayList<String>arr = new ArrayList<String>();
    return null;

}

public static void main(String[] args) {
    ServerHandler sv = new ServerHandler();
}

}

2 个答案:

答案 0 :(得分:1)

您永远不会将动作监听器添加到您的JButton b。因此,if(e.getSource().equals(b))始终为false,因为b按钮从不启动操作。因为这永远不会评估为true,所以永远不会调用该方法。

答案 1 :(得分:1)

好的,总结一下。首先,调用getArrrayList两次并不是一个好主意,因为第二次套接字中没有任何东西你只是吞下饮料...... :)从while循环中返回getArrayList。关于你在上次评论中的问题,这是一个设计问题。也从地图返回值。我认为,你不应该剥离密钥集并将其作为列表返回(你可以通过new ArrayList(map.keySet())更容易地做到这一点)。返回整个地图,并将其拆分为GUI的getArrayList