【发布时间】: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