如何在单个joptionpane消息框中显示多条消息?

时间:2014-09-07 06:17:11

标签: java swing joptionpane

我是这些论坛的新手,所以我提前为我可能犯的任何混淆/错误道歉。

我正在尝试使用JOptionPane创建一个消息框,我希望它能显示许多内容,例如:

JOptionPane.showMessageDialog( null, string1, int1, string2, int2); 

有没有办法使用这些消息框在一个框内打印多个字符串和整数?

为了澄清,基本上我希望将底部的println转换为一个消息框。

我是java新手。

import javax.swing.*;

public class PayrollJoption {

    public static void main(String[] args) {

        String nameFirst;
        nameFirst = JOptionPane.showInputDialog("Enter Your First Name: ");

        String nameLast;
        nameLast = JOptionPane.showInputDialog("Enter Your Last Name: ");

        int hourlyRate;
        String hourlyRateString;
        hourlyRateString = JOptionPane.showInputDialog("Enter Your Hourly Rate: ");
        hourlyRate = Integer.parseInt(hourlyRateString);

        int hoursWorked;
        String hoursWorkedString;
        hoursWorkedString = JOptionPane.showInputDialog("Enter Your Hours Worked: ");
        hoursWorked = Integer.parseInt(hoursWorkedString);

        double grosspay = hourlyRate * hoursWorked; // calculating gross pay
        double taxWithholding = grosspay * 0.28; // calculating tax withholdings
        double netPay = grosspay - taxWithholding; // calculating net pay

        JOptionPane.showMessageDialog( null, "Name: ");  
                // This is the message box i was referring to

        System.out.println("Name: " + nameFirst + " " + nameLast); // printing full name
        System.out.println("Your Gross Pay Is:  " + "$" + grosspay); // printing gross pay
        System.out.println("Your Income Tax Is (28%) "); // showing tax percentage
        System.out.println("Your Tax WithHolding is:  " + "$" + taxWithholding); // showing tax withholdings
        System.out.println("Your Net Pay Is: " + "$" + netPay); // printing net pay
        System.out.println(" "); // skipping line for space
        System.out.println("You Need A New Job!"); // printing text

    }

}

2 个答案:

答案 0 :(得分:2)

你可以试试这个: -

String to_print="Name: " + nameFirst+ " "+ nameLast +"\n"+
"Your Gross Pay Is:  "+ "$" + grosspay+"\n"+
"Your Income Tax Is (28%) "+"\n"+
"Your Tax WithHolding is:  "+ "$"+ taxWithholding+"\n"+
"Your Net Pay Is: "+ "$"+ netPay+"\n"+" \n"+
"You Need A New Job!";   //printing text

删除所有底部System.out.println()行,然后调用,

JOptionPane.showMessageDialog( null,to_print);

我希望这适合你。如果没有,请在下面评论!

答案 1 :(得分:2)

欢迎来到论坛,在字符串中你可以使用\ n将消息分成几行:

"First line \n second line"

此外,您可以使用HTML并呈现字符串的内容:

"<html><b> a bold text </b></html>"