简体   繁体   中英

How do I add an integer input for my program in java swing?

So I have a program in swing where I am supposed to calculate the cost of staying in a hotel room for "x" amount of weeks.

I want to be able to have the user input "x" but I am not sure what code I would use. Also Here is my code so far.

public class Hotel extends JFrame
{
    int room1 = 0;
    int room2 = 0;
    int weeks;
    int boat = 0;
    JPanel p, p1;
    JLabel l, l1;
    JTextField t, t1;
    JButton b;
    JCheckBox b1, b2, b3;

    Hotel()
    {
        setTitle("Hotel Room Selection");
        setSize(800, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        add(p);
        setVisible(true);
    }

    void buildPanel()
    {
        l = new JLabel("Select a room type");
        l1 = new JLabel("How many weeks will you be staying?");
        b = new JButton("Confirm");
        b1 = new JCheckBox("1 bedroom ($600 per week)");
        b2 = new JCheckBox("2 Bedroom ($850 per week)");
        b3 = new JCheckBox("Rowboat rental ($60 per week)");

        p = new JPanel();
        p1 = new JPanel();
        add(p);
        p.add(l);
        p.add(b1);
        p.add(b2);
        p.add(b3);
        p.add(l1);
        p.add(b);
        add(p1);

        b.addActionListener(new buttonlistener());
        b1.addItemListener(new cbListener());
        b2.addItemListener(new cbListener());
        b3.addItemListener(new cbListener());
        setVisible(true);

    }

    class buttonlistener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String i = t.getText();
            int val = Integer.parseInt(i);
            val = (room1 + room2 + boat) * weeks;
            JOptionPane.showMessageDialog(null, "Total is " + val);
        }
    }

    class cbListener implements ItemListener
    {
        public void itemStateChanged(ItemEvent b)
        {
            if (b.getSource() == b1)
            {
                if (b1.isSelected())
                {
                    room1 = 600;
                }
                if (b.getSource() == b3)
                {
                    if (b3.isSelected())
                    {
                        boat = 60;
                    }
                }
            }
            else if (b.getSource() == b2)
            {
                if (b2.isSelected())
                {
                    room2 = 850;
                }
                if (b.getSource() == b3)
                {
                    if (b3.isSelected())
                    {
                        boat = 60;
                    }
                }
            }
        }
    }

    public static void main(String[] args)
    {
        Hotel a1 = new Hotel();
    }
}

The two easiest solutions are...

JFormattedTextField

在此处输入图片说明

and

JSpinner

在此处输入图片说明

See...

For more details (for my money, JSpinner would be my choice)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM