有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用户输入后java JTextArea未更新

我通过telnet将用户输入输入到HashMap中,然后尝试将该信息显示到JTextArea中

当我点击run时,GUI启动,初始HashMap被发布到GUI。但当我更新HashMap值时,它们不会显示在文本区域中。我有一个时间文本字段,它会自动更新并显示当前时间

我哪里做错了?问题是在GUI类中还是在我的用户输入类中

以下是我的GUI课程:

public class GUI extends JFrame {

private JTextArea theArea1;
private JTextField theField1;
private Timer theTimer;
private static final int TIMER_DELAY = 1;
private Scores scores;
private SimpleDateFormat formatter;
private long startTimeMillis;


public GUI(Scores score) {
    this.scores = score;
    this.theArea1 = theArea1;
    this.theField1 = theField1;
}

public void init() {
    Container c = getContentPane();
    setTitle("Leadrboard");
    setSize(400, 400);

    formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss");
    startTimeMillis = System.currentTimeMillis();

    JPanel aPanel = new JPanel( );
    aPanel.setLayout(new GridLayout(2,1));

    theField1 = new JTextField("", 20);
    aPanel.add(theField1);
    theField1.setEditable(false);

    theArea1 = new JTextArea("", 5, 20);
    aPanel.add(theArea1);
    theArea1.setEditable(false);
    c.add(new JScrollPane(theArea1), BorderLayout.CENTER) ;
    c.add(aPanel, BorderLayout.NORTH);

    String str = "";

    theTimer = new Timer(TIMER_DELAY, new ActionListener() {
        @Override

        public void actionPerformed(ActionEvent e) {
    // do whatever theTimer requires

           Date todaysDate = new java.util.Date();
           String formattedDate = formatter.format(todaysDate);
           theField1.setText(formattedDate);
           theArea1.setText(scores.toString());


        }

    });
    theTimer.start();

  }

}

下面是我输入用户输入的课程。输入score(HashMap值)后,我会在cmd窗口中打印HashMap,它会按预期显示HashMap,但GUI文本字段不会更改:

public class ClientRequest extends Thread {

private Scores scores = new Scores();
private Socket incoming;
//HashMap<String, Integer> name = new HashMap<String, Integer>();

public ClientRequest(Socket incoming) {
    this.incoming = incoming;
}

@Override
public void run() {
    try {
        // set up streams for bidirectional transfer
        // across connection socket 
        BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
        PrintWriter out = new PrintWriter(incoming.getOutputStream(), true /* auto flush */);
        out.println("You are connected to "
                + incoming.getLocalAddress().getHostName()
                + " on port " + incoming.getLocalPort()
        );
        out.println("to manually enter players scores for each hole enter 'MANUAL.");
        String input = in.readLine();

        if (input.trim().equals("MANUAL")) {
            boolean isDone = false;
            while (!isDone) {

                //scores.fillHashmap(scores.name);
                out.println("Enter hole number");
                int holeNumber = Integer.parseInt(in.readLine());

                out.println("Enter player name");
                boolean check = false;
                String playerName = in.readLine();

                out.println("Enter player name");

                boolean nameExists = scores.name.containsKey(playerName);
                if (nameExists == false) {
                    out.println("That is not a valid player, try again");
                    playerName = in.readLine();
                }

                if (nameExists == true) {
                    out.println("Valid player");

                }


                out.println("Enter score for that hole");
                int holeScore = Integer.parseInt(in.readLine());
                scores.name.put(playerName, (scores.name.get(playerName) + holeScore));
                out.println(scores.toString());

            }

        }


        incoming.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

@Override
public String toString() {
    String str = "";
    for (String key : scores.name.keySet()) {

        //int holeScore = Integer.parseInt(in.readLine());
        scores.name.put(key, (scores.name.get(key)));

        str += "yeeeh" + scores.name.put(key, (scores.name.get(key)));
    }

    return str;
  }
}

这里是Scores,HashMap存储在这里:

public class Scores {

HashMap<String, Integer> name = new HashMap<String, Integer>();
Boolean[] garciaArray;
Boolean[] furykArray;
Boolean[] donaldArray;
Boolean[] westwoodArray;
Boolean[] mcilroyArray;

public String test() {
    return "test";
}

public Scores() {
    name.put("Sergio Garcia", 0);
    name.put("Jim Furyk", 0);
    name.put("Luke Donald", 0);
    name.put("Lee Westwood", 0);
    name.put("Rory McIlroy", 0);

}

public HashMap<String, Integer> getName() {
    return name;
}

public String toString() {
    StringBuilder sb = new StringBuilder();
    Iterator<Map.Entry<String, Integer>> iter = name.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry<String, Integer> entry = iter.next();
        sb.append(entry.getKey());
        sb.append('=').append('"');
        sb.append(entry.getValue());
        sb.append('"');
        if (iter.hasNext()) {
            sb.append(',').append(' ' + "\n");
        }
    }
    return sb.toString();
  }
}

共 (0) 个答案