gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:17:27 26 4
gpt4 key购买 nike

我是这些论坛的新手,所以对于我可能造成的任何困惑/错误,我提前表示歉意。

我正在尝试使用 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

}

}

最佳答案

你可以试试这个:-

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

我希望这对你有用。如果没有,请在下面发表评论!

关于java - 如何在单个 joptionpane 消息框中显示多条消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25707625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com