我是初学程序员,我为学校班级写了这篇文章。但不知怎的,即使我在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不起作用。
请帮忙!
答案 0 :(得分:1)
当您尝试显示swing组件时,您需要从事件调度线程执行此操作。否则,您将无法获得可靠的结果。因此,在这种情况下,在调用SwingUtilities.invokeLater:
的调用中将所有代码包装在main中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组件,它会发生什么:
忽略此规则的程序可能在大多数情况下正常运行,但会遇到难以重现的不可预测的错误。