试图将JOptionPane插入程序

时间:2014-09-25 23:49:50

标签: java swing

这个程序几乎是由一个很棒的人在这个网站上重写的。我试图涉足它并且我迈出了最后一步。我试图让我的消息成为JoptionPane,我需要好的消息才能让PLAIN_MESSAGE图标和错误消息出错。到目前为止,我可以使框出现,但我不知道如何让它提示用户在框中输入答案,并在弹出框中弹出结果。截至目前,它打开了选项窗格,但让用户在日食底部的框中​​输入答案。

  import javax.swing.JOptionPane;
import java.util.Scanner;
public class Easter
{
    public static void main(String[] args)
    {

        JOptionPane.showInputDialog(null, "Please enter a year to calculate Easter Sunday", null, JOptionPane.PLAIN_MESSAGE);
        Scanner s = new Scanner(System.in);
        int inputted = getResult(s);
        while(inputted <= 0)
        {
            String result = JOptionPane.showInputDialog(null, "Expected a positive year. Please try again:", "Error", JOptionPane.ERROR_MESSAGE);
            System.out.println("You entered " + result);
            inputted = getResult(s);
        }
        System.out.println(getEasterSundayDate(inputted));
    }

    private static int getResult(Scanner s)
    {
        while(!s.hasNextInt())
        {
            JOptionPane.showMessageDialog(null, "Expected a valid year. Please try again:\n>");
            s.nextLine();
        }
        return s.nextInt();
    }

    public static String getEasterSundayDate(int year)
    {
        int a = year % 19,
            b = year / 100,
            c = year % 100,
            d = b / 4,
            e = b % 4,
            g = (8 * b + 13) / 25,
            h = (19 * a + b - d - g + 15) % 30,
            j = c / 4,
            k = c % 4,
            m = (a + 11 * h) / 319,
            r = (2 * e + 2 * j - k - h + m + 32) % 7,
            n = (h - m + r + 90) / 25,
            p = (h - m + r + n + 19) % 32;

        String result;
        switch(n)
        {
            case 1:
                result = "January ";
                break;
            case 2:
                result = "February ";
                break;
            case 3:
                result = "March ";
                break;
            case 4:
                result = "April ";
                break;
            case 5:
                result = "May ";
                break;
            case 6:
                result = "June ";
                break;
            case 7:
                result = "July ";
                break;
            case 8:
                result = "August ";
                break;
            case 9:
                result = "September ";
                break;
            case 10:
                result = "October ";
                break;
            case 11:
                result = "November ";
                break;
            case 12:
                result = "December ";
                break;
            default:
                result = "error";
        }

        return result + p;
    }
}

1 个答案:

答案 0 :(得分:3)

首先应该看看JOptionPane.showInputDialog。有一些变量,但链接版本允许您指定消息类型

您还应该查看How to Make Dialogs了解更多详情

作为一个例子......

String result = JOptionPane.showInputDialog(null, "Expected a positive year. Please try again:", "Error", JOptionPane.ERROR_MESSAGE);
System.out.println("You entered " + result);
result = JOptionPane.showInputDialog(null, "Please enter a year to calculate Easter Sunday", "Info", JOptionPane.PLAIN_MESSAGE);
System.out.println("You entered " + result);
result = JOptionPane.showInputDialog(null, "Please enter a year to calculate Easter Sunday");
System.out.println("You entered " + result);

<强>更新

为简单起见,我已经在main方法中维护了核心部分,您可以将值的验证卸载到另一个方法,但这会得到基本的想法...

public static void main(String[] args) {
    String value = JOptionPane.showInputDialog(null, "Please enter a year to calculate Easter Sunday", null, JOptionPane.PLAIN_MESSAGE);
    int inputted = 0;
    do {
        try {
            inputted = Integer.parseInt(value);
            if (inputted <= 0) {
                inputted = 0;
                value = JOptionPane.showInputDialog(null, "Expected a positive year. Please try again:", "Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (NumberFormatException exp) {
            inputted = 0;
            value = JOptionPane.showInputDialog(null, value + " is not a valid number. Please try again:", "Error", JOptionPane.ERROR_MESSAGE);
        }
    } while (inputted == 0);
}