Java 为什么不是';我的JOptionPane.showMessageDialog不工作吗?

Java 为什么不是';我的JOptionPane.showMessageDialog不工作吗?,java,user-interface,dialog,joptionpane,Java,User Interface,Dialog,Joptionpane,我是一名初级程序员,我为我的学校课程写了这篇文章。但不知何故,即使是displaypay()下的我的JOptionPane.showMessageDialog也无法工作!!!我希望会弹出一个消息框,但在我输入每天的小时数后,什么也没有发生!它甚至没有跳过部分,整个程序只是暂停!我真糊涂!相反,如果我使用System.out.println(),它就可以正常工作 另外,我不想对displaypay()执行System.out.println,我必须使用showMessageDialog pack

我是一名初级程序员,我为我的学校课程写了这篇文章。但不知何故,即使是displaypay()下的我的JOptionPane.showMessageDialog也无法工作!!!我希望会弹出一个消息框,但在我输入每天的小时数后,什么也没有发生!它甚至没有跳过部分,整个程序只是暂停!我真糊涂!相反,如果我使用System.out.println(),它就可以正常工作

另外,我不想对displaypay()执行System.out.println,我必须使用showMessageDialog

 package payroll.program;

 import java.util.Scanner;
 import javax.swing.JOptionPane; //imports

 class Employee
{
int hourlypay;
int totalhours = 0;
String name; //variables

void getname()
{
    Scanner scan = new Scanner(System.in);

    System.out.println("What is this employee's name?");
    name = scan.next(); //gets name
}

void calculatepay()
{
    int[] hours = new int[5]; //creates array for hours

    Scanner scan = new Scanner(System.in);

    System.out.println("How much is " + name + " paid per hour?");
    hourlypay = scan.nextInt(); //gets hourly pay

    for (int i = 0; i < 5; i++)
    {
        System.out.println("How many hours did " + name + " work on day " + (i + 1) + "?");
        hours[i] = scan.nextInt(); //gets hour on each day

        totalhours = totalhours + hours[i]; //adds hours up
    }
}
void displaypay()
{
    JOptionPane.showMessageDialog(null, "You have to pay " + " $" + totalhours * hourlypay + " to " + name + "!"); //displays total pay
}
 }

 public class PayrollProgram {

    public static void main(String[] args) {

    int numberofemployees; //variable for # of employees

    System.out.println("Welcome to the Payroll Program!"); //welcomes user

    Scanner scan = new Scanner(System.in);
    System.out.println("How many employees do you have?");
    numberofemployees = scan.nextInt(); //gets input for # of employees

    Employee[] ArrayofEmployees = new Employee[numberofemployees]; //creates array of employees with the same size as the number of employees

    for (int i = 0; i < numberofemployees; i++)
    {
        ArrayofEmployees[i] = new Employee(); //creates an Employee for each space in the array
    }

    for (int i = 0; i < numberofemployees; i++)
    {
        ArrayofEmployees[i].getname();
        ArrayofEmployees[i].calculatepay();
        ArrayofEmployees[i].displaypay(); //runs functions in class Employee for each employee
    }
}
package payroll.program;
导入java.util.Scanner;
导入javax.swing.JOptionPane//进口
班级员工
{
小时内支付;
整数总小时=0;
字符串名称;//变量
void getname()
{
扫描仪扫描=新扫描仪(System.in);
System.out.println(“这个员工叫什么名字?”);
name=scan.next();//获取名称
}
void calculatepay()
{
int[]小时=新的int[5];//为小时创建数组
扫描仪扫描=新扫描仪(System.in);
System.out.println(“每小时“+name+”支付多少钱?”);
hourlypay=scan.nextInt();//获取小时工资
对于(int i=0;i<5;i++)
{
System.out.println(“一天“+(i+1)+”中“+name+”工作了多少小时?”;
小时数[i]=scan.nextInt();//获取每天的小时数
totalhours=totalhours+hours[i];//将小时相加
}
}
无效支付()
{
JOptionPane.showMessageDialog(null,“您必须支付”+“$”+totalhours*hourlypay+“到”+name+“!”;//显示总支付
}
}
公共类工资计划{
公共静态void main(字符串[]args){
int numberofemployees;//用于#个员工的变量
System.out.println(“欢迎使用工资单程序!”;//欢迎用户
扫描仪扫描=新扫描仪(System.in);
System.out.println(“您有多少员工?”);
numberofemployees=scan.nextInt();//获取#个员工的输入
Employee[]ArrayofEmployees=新员工[numberofemployees];//创建与员工数量相同大小的员工数组
对于(int i=0;i
}

这是我的程序,JOptionPane.showMessageDialog无法工作


请帮忙

当您尝试显示swing组件时,您需要从事件调度线程执行此操作。否则,您将无法获得可靠的结果。因此,在本例中,将所有代码封装在main中,并调用:

publicstaticvoidmain(字符串[]args){
javax.swing.SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
int numberofemployees;//用于#个员工的变量
System.out.println(“欢迎使用工资单程序!”;//欢迎用户
扫描仪扫描=新扫描仪(System.in);
System.out.println(“您有多少员工?”);
numberofemployees=scan.nextInt();//获取#个员工的输入
Employee[]ArrayofEmployees=新员工[numberofemployees];//创建与员工数量相同大小的员工数组
对于(int i=0;i
更多信息可在此处找到:

特别是,请注意,如果无法从事件调度器线程运行Swing组件,可能会发生什么情况:

忽略此规则的程序可能在大多数情况下都能正常运行,但会出现难以重现的不可预测错误


定义“不起作用”(这样其他有同样问题的人也会发现这个问题)。你预期会发生什么,会发生什么?我无法重现。该对话框为我显示。在使用
Scanner\next…
后,当我们尝试使用
JOptionPane.showMessageDialog
时,似乎出现了问题。SSCCE为我重现了这个问题:对我来说似乎工作得很好!我简直是个白痴。它出现在背景中。非常感谢!!!
public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            int numberofemployees; //variable for # of employees

            System.out.println("Welcome to the Payroll Program!"); //welcomes user

            Scanner scan = new Scanner(System.in);
            System.out.println("How many employees do you have?");
            numberofemployees = scan.nextInt(); //gets input for # of employees

            Employee[] ArrayofEmployees = new Employee[numberofemployees]; //creates array of employees with the same size as the number of employees

            for (int i = 0; i < numberofemployees; i++) {
                ArrayofEmployees[i] = new Employee(); //creates an Employee for each space in the array
            }

            for (int i = 0; i < numberofemployees; i++) {
                ArrayofEmployees[i].getname();
                ArrayofEmployees[i].calculatepay();
                ArrayofEmployees[i].displaypay(); //runs functions in class Employee for each employee
            }
        }
    });
}