java - 如何在 java swing 中为我的程序添加整数输入?

标签 java swing user-interface input

所以我有一个 swing 程序,我应该计算在酒店房间里呆“x”周的费用。

我希望能够让用户输入“x”,但我不确定我会使用什么代码。到目前为止,这是我的代码。

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

最佳答案

两个最简单的解决方案是...

JFormattedTextField

enter image description here

JSpinner

enter image description here

看...

有关更多详细信息(对于我的钱,JSpinner 将是我的选择)

关于java - 如何在 java swing 中为我的程序添加整数输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18007121/

相关文章:

Java Map 从值中获取键

java - Android GridView 间距和居中问题

eclipse - 没有错误,但代码无法运行,应用程序启动方法 JavaFX 中出现异常

java - 在 Windows 操作系统中使用命令行查找与文件(.pdf)相关的默认程序的命令

.net - WPF UI 更新细节

c# - 创建多表单 GUI

java - Maven/IntelliJ/Yosemite 安装问题

java - Spring Security 自定义登录表单重定向到资源不可用

java - 为 JTable 构建并发 TableColumnAdjuster

java - 调用 SwingWorker 方法 get() 时 gui 卡住