【问题标题】:Using Calc button and radiobutton listeners together一起使用 Calc 按钮和单选按钮侦听器
【发布时间】:2016-04-06 04:21:04
【问题描述】:

我正在开展一个项目,该项目根据通话时间和通话时间来计算通话费用。除计算按钮外,一切正常。我不知道如何做到这一点,所以当单击计算按钮时,它会查找选择了哪个单选按钮并执行适当的等式。我尝试过使用循环,但它不起作用。

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;


public class RateChargeGUI extends JFrame
{
private JPanel panel;  
private JLabel messageLabel; //message to the  user
private JTextField timeTextField; //user inputs time of call in minutes
private JPanel Buttons;
private JRadioButton DayTimeButton;  //declares a new radio button called DayTimeButton
private JRadioButton EveningButton; //declares a new radio button called EveningButton
private JRadioButton Off_PeakButton; //declares a new radio button called Off_PeakButton
private ButtonGroup radioButtonGroup; //places the buttons in a group
private JButton exitButton; 
private JButton calcButton;
private final int WINDOW_WIDTH = 300; //window width
private final int WINDOW_HEIGHT = 300; //window height

/**
Constructor 
*/

public RateChargeGUI ()
{
    //sets the text for the title bar
    setTitle("Call Prices");
    //sets the size of the window
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //build a panel that will be added to the frame
    buildPanel();
    buildButtons();

    //add the panel to the frame
    add(panel);
    add(Buttons, BorderLayout.SOUTH);

    //display the window
    setVisible(true);
}


//The buildPanel method adds a label, text field, and the buttons to the panel.

private void buildPanel()
{
messageLabel = new JLabel(" Enter Number of minutes "); //label left of the text box telling the user what to do.
timeTextField = new JTextField(10); //text field that accepts the duration of the call
DayTimeButton = new JRadioButton("Day Time"); // radio buttons for the time of day that the call is made
EveningButton = new JRadioButton("Evening");
Off_PeakButton = new JRadioButton("Off_Peak");


//add and group the radio buttons

radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(DayTimeButton);
radioButtonGroup.add(EveningButton);
radioButtonGroup.add(Off_PeakButton);

//action listeners for the radio buttons that make it possible to select one of them
//DayTimeButton.addActionListener(new RadioButtonListener ());
//EveningButton.addActionListener(new RadioButtonListener ());
//Off_PeakButton.addActionListener(new RadioButtonListener ());

//create a panel and add the components to it. such as the label telling the user to enter the number of minutes
//or the radio buttons
panel = new JPanel();
panel.add(messageLabel);
panel.add(timeTextField);
panel.add(DayTimeButton);
panel.add(EveningButton);
panel.add(Off_PeakButton);

Buttons = new JPanel();
add(Buttons, BorderLayout.SOUTH);
}
private void buildButtons()
{
    exitButton = new JButton("Exit");
    calcButton = new JButton("Calculate");

    exitButton.addActionListener(new ExitButtonListener ());
    calcButton.addActionListener(new CalcButtonListener ());

    Buttons.add(calcButton);
    Buttons.add(exitButton);
}
/**
Private inner class that handles the event when the user clicks one of the radio buttons.
*/

private class CalcButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        String input; //holds user input
        double result = 0.0; //holds the conversion
        input = timeTextField.getText(); //enables the text field to accept text

        //Determine which radio button was selected and perform a mathmatical equation based on which button was selected.
        //6AM-6PM
        if (e.getSource() == DayTimeButton)
        {
            result = Double.parseDouble(input) * 0.20;
        }
        //6PM-12AM
        else if (e.getSource() == EveningButton)
        {
            result = Double.parseDouble(input) * 0.12;
        }
        //12AM-6AM
        else if (e.getSource() == Off_PeakButton)
        {
            result = Double.parseDouble(input) * 0.04;
        }
        if (DaytimeButton.is
        //display the ammount that it will cost
        JOptionPane.showMessageDialog(null, " The call price is: $ " + result);
    }
}
private class ExitButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        System.exit(0);
    }
}
//calls the ratechargegui class. The RateChargeGUI class was build above and is now being called.
public static void main(String[] args)// stringargs[] allows the program to accept arguments.
    {
    new RateChargeGUI();
    }
}

【问题讨论】:

  • 你的代码能编译吗? if (DaytimeButton.is 是语法错误。另外,但它不起作用什么也没告诉我们。你目前的结果是什么?您的预期结果是什么?您当前的结果与预期结果有何不同?
  • 您的代码不完整。你想在 if (DaytimeButton.is 附近做什么
  • 变量名不应以大写字符开头。有些变量是正确的,有些则不是。保持一致!!!

标签: java swing button radio-button listeners


【解决方案1】:

如果您将此侦听器仅应用于 calcButton,则它始终是事件源。您必须检查在执行操作时选择了哪个单选按钮。

private class CalcButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
    String input; //holds user input
    double result = 0.0; //holds the conversion
    input = timeTextField.getText(); //enables the text field to accept text

    // Determine which radio button was selected and perform a mathmatical equation based on which button was selected.
    //6AM-6PM
    if (DayTimeButton.isSelected()) {
        result = Double.parseDouble(input) * 0.20;
    }
    //6PM-12AM
    else if (EveningButton.isSelected()) {
        result = Double.parseDouble(input) * 0.12;
    }
    //12AM-6AM
    else if (Off_PeakButton.isSelected()) {
        result = Double.parseDouble(input) * 0.04;
    }

    //display the ammount that it will cost
    JOptionPane.showMessageDialog(null, " The call price is: $ " + result);
}
}

【讨论】:

    【解决方案2】:

    我在您的代码中没有看到与一天中的几个小时相关的不同按钮的任何 addActionListener() 方法。如果您希望事件处理程序工作,每个单选按钮都必须附加一个 actionListener。所以,你可以在某个地方这样做:

    DayTimeButton.addActionListener(CalcButtonListener);
    EveningButton.addActionListener(CalcButtonListener);
    Off_PeakButton.addActionListener(CalcButtonListener);
    

    另外,在 Java 中,变量名必须是驼峰式。类/接口名称可以以大写字母开头,但不能以变量开头。

    如果您不需要处理单击其中一个单选按钮时发生的事件,而只需要单选按钮的状态,请尝试使用方法 isSelected()。您在 actionPerformed 中的测试可能如下所示:

    if (DaytimeButton.isSelected()) {
        // do stuff for this button
    }
    else if (EveningButton.isSelected()) {
        // do stuff for this button
    }
    

    【讨论】:

      【解决方案3】:

      您不需要为此设置循环。正如其他人所指示的,您可以使用

         isSelected()
      

      检查单选按钮选择时的方法。

      注意 但是请注意这些。

      1. 每当您期望用户输入时,在调用逻辑中的方法之前,请执行文本字段验证。现在,对于您的实例,您应该在单击 Caculate 按钮时立即检查输入有效性(空输入、字母输入等)。
      2. 使用双精度时,您的输出可能有很多小数位。因此,最好先格式化您的输出,然后再将它们展示给您的用途。
      3. 遵循 java 命名约定。正如@Daniel.Schroeder 强调的那样,对变量使用“eveningButton”、“dayTimeButton”。同时对组件使用有意义的变量。例如,您可以使用“dayTimeRadioBtn”,这样您就知道稍后这是关于您的单选按钮而不是按钮。

      【讨论】:

        猜你喜欢
        • 2011-07-16
        • 1970-01-01
        • 2015-07-31
        • 1970-01-01
        • 2020-12-23
        • 2012-02-11
        • 2012-11-11
        • 2018-08-05
        • 1970-01-01
        相关资源
        最近更新 更多