如何使用if / else语句在JText字段中打印多个文本?

时间:2016-10-17 02:25:20

标签: java if-statement jtextfield

    private void resultActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
    if(pick1 > pick2 ){
    resultstf.setText("The New President is Koon!");
    }
    else if(pick2 > pick1){
    resultstf.setText("The New President is Baam!");
    }
    else if(pick1 == pick2){
    resultstf.setText("The Result for the new President is a Tie! Please Vote Again.");
    }
    if(pick3 > pick4){
    resultstf.setText("The New VP is Sachi!");
    }
    else if(pick4 > pick3){
    resultstf.setText("The New VP is Faker!");
    }
}

每当按结果按钮时,如何打印多个文本?就像我想打印出“The New President is Koon”,同时打印出“The New VP is Sachi”。

1 个答案:

答案 0 :(得分:0)

使用StringBuilder并随时构建消息:

private void resultActionPerformed(java.awt.event.ActionEvent evt) {
    StringBuilder message = new StringBuilder();                                   
    // TODO add your handling code here:
    if (pick1 > pick2) {
        message.append("The New President is Koon!\n");
    }
    else if (pick2 > pick1) {
        message.append("The New President is Baam!\n");
    }
    else if (pick1 == pick2) {
        message.append("The Result for the new President is a Tie! Please Vote Again.\n");
    }
    if (pick3 > pick4) {
        message.append("The New VP is Sachi!\n");
    }
    else if (pick4 > pick3) {
        message.append("The New VP is Faker!\n");
    }

    resultstf.setText(message.toString());
}