gpt4 book ai didi

java - 为什么我的 JOptionPane.showMessageDialog 不工作?

转载 作者:行者123 更新时间:2023-11-30 10:51:06 24 4
gpt4 key购买 nike

我是一名初级程序员,我为我的学校类(class)写了这篇文章。但不知何故,即使是我在 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
}
}

这是我的程序,JOptionPane.showMessageDialog 不工作。

请帮忙!

最佳答案

当您尝试显示 swing 组件时,您应该从事件分派(dispatch)线程执行此操作。否则,您将无法获得可靠的结果。因此,在这种情况下,将所有代码包装在 main 中,调用 SwingUtilities.invokeLater。 :

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

可在此处找到更多信息:The Event Dispatch Thread .

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

Programs that ignore this rule may function correctly most of the time, but are subject to unpredictable errors that are difficult to reproduce.

关于java - 为什么我的 JOptionPane.showMessageDialog 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34820154/

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