2

单击按钮时,有什么方法可以刷新 jLabel 中的计数器?我已经尝试过 repaint()、revalidate() 方法,但都不起作用。

当喜欢按钮点击时:

    jButton_like.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                int count = 1;
                eForumLikeCounter likeAmount = new eForumLikeCounter(
                        userName, topicId, count);
                if (likeAmount.checkLikeAmount() == true) {
                    JOptionPane
                            .showMessageDialog(null,
                                    "Unable to perform like on the same thread by the same user");
                } else if (likeAmount.checkDislikeExists() == true) {
                    JOptionPane
                            .showMessageDialog(null,
                                    "You can only either like or dislike this thread");
                } else {
                    likeAmount.likeCounter();
                }
            }
        });
    }

为数据库设置:

    public void SetUpLikeDislikeAmount() {
    int likes = 0;
    int dislike = 0;
    // Set Up Database Source
    db.setUp("IT Innovation Project");
    String sql = "Select likeDislike_likes,likeDislike_dislike from forumLikeDislike WHERE likeDislike_topics = "
            + topicId + "";
    ResultSet resultSet = null;
    // Call readRequest to get the result
    resultSet = db.readRequest(sql);
    try {
        while (resultSet.next()) {
            likes += resultSet.getInt("likeDislike_likes");
            dislike += resultSet.getInt("likeDislike_dislike");
        }
        resultSet.close();
    } catch (Exception e) {
        System.out.println(e);
    }
    jLabel_like.setText(Integer.toString(likes));
    jLabel_dislike.setText(Integer.toString(dislike));
}

提前致谢。

4

1 回答 1

2

只需setText()使用JLabel. 大概是这样的:

counter.setText("");

或者

counter.setText("0");
于 2013-02-02T18:05:41.353 回答