1

我目前有读取用户在一行中输入的月份、日期和年份的代码(用空格分隔)。这是代码。

Scanner input = new Scanner(System.in);
int day = 0;
int month = 0;
int year = 0;

System.out.printf("enter the month, date, and year(a 2 numbered year). Put a space between the month, day, and year");
month = input.nextInt();
day = input.nextInt();
year = input.nextInt();

这很好,第二部分是显示一条消息,如果月*日==年,那么它是一个神奇的数字,如果不是,那么它不是一个神奇的数字。它必须显示在对话框中。这是我的代码,它工作得很好。

  if((day * month) == year)
  {
    String message = String.format("%s", "The date you entered is MAGIC!");//If the day * month equals the year, then it is a magic number
    JOptionPane.showMessageDialog(null, message);
  }
  if((day * month) != year)
  {  
    String message = String.format("%s", "The date you entered is NOT MAGIC!");//If the day * month does not equal the year, it is not a magic number
    JOptionPane.showMessageDialog(null, message);
  }

我的问题是!!如何获得一个对话框,以在控制台中的工作方式在一行中输入月份、日期和年份。我在 DrJava 中工作,而我所在的书中的章节对我的特定用途没有帮助。任何帮助都会很棒。谢谢大家!

4

3 回答 3

6

有多种方法可以解决问题,具体取决于您最终想要实现的目标。

JOptionPane允许您Object作为消息提供。如果此消息是 a String,它将按原样呈现,但是,如果它是Component某种类型的,它将被简单地添加到对话框中。这使得JOptionPane一个非常强大的小 API。

在此处输入图像描述

public class TestOptionPane07 {

    public static void main(String[] args) {
        new TestOptionPane07();
    }

    public TestOptionPane07() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTextField fldDay = new JTextField(3);
                JTextField fldMonth = new JTextField(3);
                JTextField fldYear = new JTextField(4);
                JPanel message = new JPanel();
                message.add(fldDay);
                message.add(new JLabel("/"));
                message.add(fldMonth);
                message.add(new JLabel("/"));
                message.add(fldYear);

                int result = JOptionPane.showConfirmDialog(null, message, "Enter Date", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (result == JOptionPane.OK_OPTION) {
                    String sDay = fldDay.getText();
                    String sMonth = fldMonth.getText();
                    String sYear = fldYear.getText();
                    JOptionPane.showMessageDialog(null, "You enetered " + sDay + "/" + sMonth + "/" + sYear);

                    try {
                        int day = Integer.parseInt(sDay);
                        int month = Integer.parseInt(sMonth);
                        int year = Integer.parseInt(sYear);
                        JOptionPane.showMessageDialog(null, "You enetered " + day + "/" + month + "/" + year);
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(null, "The values you entered are invalid");
                    }
                }
            }
        });
    }
}

更新

如果我要使用这样的东西,我还会使用DocumentFilter来确保用户只能输入有效值(此处的示例)

但你也可以使用JSpinners

在此处输入图像描述在此处输入图像描述

或者JComboBox

在此处输入图像描述

取决于你想要达到什么...

于 2013-02-09T01:47:49.633 回答
2

您可以利用以下内容获取用户输入

String word = JOptionPane.showInputDialog("Enter 3 int values");
String[] vals = word.split("\\s+"); // split the sting by whitespaces accepts regex. 
// vals[0] cast to int
// convert string representation of number into actual int value
int day = Integer.parseInt(vals[0]); // throws NumberFormatException
// vals[1] cast to int
// vals[2] cast to int

拆分 Java API

parseInt Java API

Java 正则表达式教程

于 2013-02-09T01:21:31.877 回答
2

这是一些代码,所有内容都在注释中描述:

// import statements
import javax.swing.JOptionPane;
// main class
public class Main {
    // main method
    public static void main(String[] args) {
        // get info
        try {
            Info info = new Info();
        } catch(Exception e) {
            System.err.println("Error! " + e.getMessage());
        }
        // do whatever with the info
    }
    // info class
    static class Info {
        // instance variables
        public int day, month, year;
        // constructor
        public Info() throws Exception {
            // get inputs
            String[] inputs = JOptionPane.showInputDialog(null, 
                "Enter day, month, year").split(" ");
            // not the right size
            if(inputs.length != 3) {   
                throw new Exception("Not enough infomation was given!");
            }
            // get values
            day = Integer.parseInt(inputs[0]);
            month = Integer.parseInt(inputs[1]);
            year = Integer.parseInt(inputs[2]);
        }
    }
}

如果出现问题,它会以一种优雅的方式通知您,并且您需要的所有内容都打包在一个方便的对象中。

于 2013-02-09T01:25:44.320 回答