Java按钮问题

时间:2010-08-15 19:23:00

标签: java button

我试图让用户输入2个字段。一个是游泳池的体积,一个是小屋浴缸的体积。然后计算每个的价格,我遇到的问题是,如果用户输入池的容量,那么他们不能为热水浴缸输入任何东西,反之亦然。这就是我到目前为止所拥有的。我是否需要为此分别设置2个字段,或者如何完成?

几乎字符串错误=“”;一旦我弄清楚如何只允许他们输入一组数字就可以删除。这是计算部分,代码的另一部分只是3个标签。

pricePanel = new JPanel(new FlowLayout());

        final JRadioButton poolPrice= new JRadioButton("Pool");
        final JRadioButton tubPrice = new JRadioButton("Hot Tub");

        poolPrice.setSelected(true);

        ButtonGroup group = new ButtonGroup();
        group.add(poolPrice);
        group.add(tubPrice);

        pricePanel.add(poolPrice);
        pricePanel.add(tubPrice);

        pricePanel.add(new JLabel("Enter the pool's volume: "));
        final JTextField poolField = new JTextField(10);
        pricePanel.add(poolField);

        pricePanel.add(new JLabel("Enter the tub's volume: "));
        final JTextField tubField = new JTextField(10);
        pricePanel.add(tubField);


        JButton calculatePrice = new JButton("Calculate Price");
        calculatePrice.setMnemonic('C');
        pricePanel.add(calculatePrice);
        pricePanel.add(createExitButton());

        pricePanel.add(new JLabel("The price is:$ "));
        final JTextField priceField = new JTextField(10);
        priceField.setEditable(false);
        pricePanel.add(priceField);


        calculatePrice.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                    double pool = Double.parseDouble (poolField.getText());
                    double tub = Double.parseDouble(tubField.getText());

                    double price;
                    if (poolPrice.isSelected()) {
                        price = pool * 100;
                    } else {
                        price = tub * 75;
                    }
                    priceField.setText(df.format(price));
                }
        });
    };


        ActionListener priceListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == poolPrice) {
                    tubLabel.setEnabled(false);
                    tubField.setEnabled(false);
                    messageArea.setVisible(true);
                } else if (e.getSource() == tubPrice) {
                    poolLabel.setEnabled(false);
                    poolField.setEnabled(false);
                    messageArea.setVisible(true);
                }
            }
        };


        poolPrice.addActionListener(priceListener);
        tubPrice.addActionListener(priceListener);

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

hotTubsPanel = new JPanel(new FlowLayout());

    final JRadioButton roundTub = new JRadioButton("Round Tub");
    final JRadioButton ovalTub = new JRadioButton("Oval Tub");

    roundTub.setSelected(true);

    ButtonGroup group = new ButtonGroup();
    group.add(roundTub);
    group.add(ovalTub);

    hotTubsPanel.add(roundTub);
    hotTubsPanel.add(ovalTub);

    hotTubsPanel.add(new JLabel("Enter the tub's length:       "));
    final JTextField lengthField = new JTextField(10);
    hotTubsPanel.add(lengthField);

    final JLabel widthLabel = new JLabel("Enter the tub's width*: ");
    widthLabel.setEnabled(false);
    hotTubsPanel.add(widthLabel);

    final JTextField widthField = new JTextField(10);
    widthField.setEnabled(false);
    hotTubsPanel.add(widthField);

    hotTubsPanel.add(new JLabel("Enter the tub's depth: "));
    final JTextField depthField = new JTextField(10);
    hotTubsPanel.add(depthField);

    JButton calculateVolume = new JButton("Calculate Volume");
    calculateVolume.setMnemonic('C');
    hotTubsPanel.add(calculateVolume);
    hotTubsPanel.add(createExitButton());

    hotTubsPanel.add(new JLabel("The tub's volume is: "));
    final JTextField volumeField = new JTextField(10);
    volumeField.setEditable(false);
    hotTubsPanel.add(volumeField);

    final JTextArea messageArea = createMessageArea(1, 25,
            "*Width will be set to the same value as length");
    hotTubsPanel.add(messageArea);

    calculateVolume.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (roundTub.isSelected()) {
                widthField.setText(lengthField.getText());
            }

            ValidationResult result = validateFields(new JTextField[] {
                    lengthField, widthField, depthField });

            String errors = "";
            if (result.filled != 3) {
                errors += "Please fill out all fields! ";
            }
            if (result.valid != 3 && result.filled != result.valid) {
                errors += "Please enter valid numbers!";
            }

            if (errors != "") {
                messageArea.setText(errors);
                messageArea.setVisible(true);
            } else {
                messageArea.setVisible(false);

                double length = Double.parseDouble(lengthField.getText());
                double width = Double.parseDouble(widthField.getText());
                double depth = Double.parseDouble(depthField.getText());

                double volume;
                if (roundTub.isSelected()) {
                    volume = Math.PI * Math.pow(length / 2.0, 2) * depth;
                } else {
                    volume = Math.PI * Math.pow(length * width, 2) * depth;
                }
                volumeField.setText(df.format(volume));
            }
        }
    });

    ActionListener tubsListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == roundTub) {
                widthLabel.setEnabled(false);
                widthField.setEnabled(false);
                widthField.setText(lengthField.getText());
                messageArea.setText("Tub's width set to length");
                messageArea.setVisible(true);
            } else if (e.getSource() == ovalTub) {
                widthLabel.setEnabled(true);
                widthField.setEnabled(true);
                messageArea.setVisible(false);
            }
        }
    };
    roundTub.addActionListener(tubsListener);
    ovalTub.addActionListener(tubsListener);
}

1 个答案:

答案 0 :(得分:2)

两个文本字段和一个结果字段可能会让用户感到困惑。如果只有一个结果字段,那么应该只有一个文本字段。 Stian建议的单选按钮可以工作,甚至有两个按钮,一个用于游泳池,一个用于热水浴缸(这只需要一次点击即可获得另一个价格)。你应该在结果字段中有一些东西,表明已经计算了哪一个,比如“Pool price:$ XX.XX”或“Hot tub price:$ XX.XX”。如果你按下了按钮的想法并标记了按钮“Pool”和“Hot Tub”,你甚至可以对结果做些喜欢的事情,比如setText(e.getActionCommand()+" price:"+price);

此外,if (errors != "")始终为true。您正在测试您的errors对象是否与您正在创建的新String对象相同;它永远不会。您应该测试if (!errors.equals(""))if (errors.length()!=0)