JOptionPane and reading integers - Beginner Java

前端 未结 3 1526
深忆病人
深忆病人 2020-12-07 05:10

I currently have code that reads the month, date, and year a user enters in one line (separated by spaces). Here is the code.

Scanner input = new Scanner(Sys         


        
3条回答
  •  醉话见心
    2020-12-07 05:41

    There are a number of ways to approach the problem depending on ultimately what it is you want to achieve.

    JOptionPane allows you to supply Object as the message. If this message is a String it will rendered as is, however, if it is a Component of some kind, it will be simply added to the dialog. This makes JOptionPane a very powerful little API.

    enter image description here

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

    Updated

    If I was going to use something like this, I would also use a DocumentFilter to ensure that the user could only enter valid values (examples here)

    But you could also use JSpinners

    enter image description hereenter image description here

    Or JComboBox

    enter image description here

    Depending on what it is you want to achieve...

提交回复
热议问题