【问题标题】:Why isn't my JOptionPane.showMessageDialog working?为什么我的 JOptionPane.showMessageDialog 不工作?
【发布时间】:2016-01-15 21:29:10
【问题描述】:

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

请帮忙!

【问题讨论】:

  • 定义“不起作用”(所以其他有同样问题的人可以找到这个问题)。您期望会发生什么以及会发生什么?
  • 我无法复制它。对话框为我出现。
  • 使用Scanner#next... 后尝试使用JOptionPane.showMessageDialog 时出现问题。为我重现问题的 SSCCE:pastebin.com/NAMcmuEh
  • 对我来说似乎工作得很好
  • 没关系!我几乎是个白痴。它出现在背景中。非常感谢!!!

标签: java user-interface dialog joptionpane


【解决方案1】:

当您尝试显示 Swing 组件时,您应该从事件分派线程中执行此操作。否则,您将无法获得可靠的结果。因此,在这种情况下,将所有代码封装在 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 组件,可能会发生什么情况:

忽略此规则的程序可能大部分时间都能正常运行,但会出现难以重现的不可预测的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-17
    • 2017-09-20
    • 2012-04-14
    • 2016-09-15
    • 2012-02-14
    • 2012-08-10
    • 2013-07-27
    • 1970-01-01
    相关资源
    最近更新 更多