我有一个程序,其中应该选择JMenuButtonItem并且ActionListener使用所选的任何内容更新JTextField。但是,它只显示buildPhonePackageMenu()中预先选择的true
。如果true
不在参数中,则单选按钮被识别为已选中。
buildPhoneModelMenu()似乎工作正常。
任何人都可以帮我弄清楚为什么一个菜单可以工作而不是另一个菜单?我已经花了6个小时试图搞清楚。 谢谢!
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;
public class CellPhoneCalculator extends JFrame{
private JMenuBar menuBar; //Menu bar to hold drop down menus.
private JMenu fileMenu; //file menu
private JMenu phonePackage; //menu containing phone packages and options
private JMenu phoneModels; //menu containing cell phone models.
private JMenuItem exit;//Exit button
private JRadioButtonMenuItem package300; //300 minute cell phone package
private JRadioButtonMenuItem package800; //800 minute cell phone package
private JRadioButtonMenuItem package1500; //1500 minute cell phone package.
private JRadioButtonMenuItem phone100; //phone model 100
private JRadioButtonMenuItem phone110; //phone model 110
private JRadioButtonMenuItem phone200; //phone model 200
private JCheckBoxMenuItem vmail; //voicemail option
private JCheckBoxMenuItem text; //text messaging option
private JLabel packageSelected;//Displays the text "Cell Phone Package:"
private JLabel modelSelected; //Displays the text "Cell Phone Model:"
private JLabel optionsSelected; //Displays the text "Options Selected:"
private JLabel subtotal; //Displays the text "Subtotal."
private JLabel tax; //Displays the text "Sales Tax (6%) :"
private JLabel total; //Displays the text "Total:"
private JTextField pSelected; //displays the selected cell phone package.
private JTextField mSelected; //displays the selected cell phone model.
private JTextField oSelectedV; //Displays the Voice Mail option selected.
private JTextField oSelectedT; //Displays the Text option selected.
private JTextField dspSub; //displays the subtotal
private JTextField dspTax; //displays the sales tax amount on subtotal
private JTextField dspTotal; //displays the total after tax.
public CellPhoneCalculator()
{
//call the superclass constructor to instantiate a JFrame
//with a String argument to be title.
super("Cell Phone Package Pricing Calculator");
//Exit on window close
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPricePane();//builds the price panel to go into content pane
buildMenuBar();//Builds the menu bar.
buildPhonePackageMenu();//Builds phone package menu
buildFileMenu();//Builds the File Menu
//pack method to resize JFrame.
pack();
//Set visibility of panel.
setVisible(true);
}
private void buildMenuBar()
{
menuBar = new JMenuBar();//Create the menu bar
buildFileMenu(); //build the file menu
buildPhonePackageMenu();//build the phone package menu
buildPhoneModelMenu();//build the phone model menu
//Add the menus to the menuBar.
menuBar.add(fileMenu);
menuBar.add(phonePackage);
menuBar.add(phoneModels);
setJMenuBar(menuBar);//sets the menu bar for the window.
}
private void buildPhonePackageMenu()
{
phonePackage = new JMenu("Cell Phone Packages");//Drop down containing phone plan
//packages and options.
//Make the radio buttons for cell phone packages.
package300 = new JRadioButtonMenuItem("300 minute package", true);
package800 = new JRadioButtonMenuItem("800 minute package");
package1500 = new JRadioButtonMenuItem("1500 minute package");
//Register the action listener PackageListener with the radio buttons.
package300.addActionListener(new PackageListener());
package800.addActionListener(new PackageListener());
package1500.addActionListener(new PackageListener());
//Make new button group for packages so buttons are mutually exclusive.
ButtonGroup packages = new ButtonGroup();
//Add radio buttons to packages button group.
packages.add(package300);
packages.add(package800);
packages.add(package1500);
//Create phone option check boxes.
vmail = new JCheckBoxMenuItem("Voice Mail");
text = new JCheckBoxMenuItem("Text Mail");
//Register Item Listener with check box options.
vmail.addItemListener(new OptionsListener());
text.addItemListener(new OptionsListener());
//Add components to menu
phonePackage.add(package300);
phonePackage.add(package800);
phonePackage.add(package1500);
phonePackage.addSeparator();//Add a separator before phone options
phonePackage.add(vmail);
phonePackage.add(text);
}
private void buildFileMenu()
{
//Instantiate file drop down with text "File."
fileMenu = new JMenu("File");
//Create exit button
exit = new JMenuItem("Exit");
//register action listener
exit.addActionListener(new ExitButtonListener());
//add exit menu item to menu
fileMenu.add(exit);
}
/*
* Build the menu to select the phone models.
*/
private void buildPhoneModelMenu()
{
//Create Menu drop down with text "Phone Models."
phoneModels = new JMenu("Phone Models");
//Create new radio buttons in menu, with model 100 preselected.
phone100 = new JRadioButtonMenuItem("Phone Model 100", true);
phone110 = new JRadioButtonMenuItem("Phone Model 110");
phone200 = new JRadioButtonMenuItem("Phone Model 200");
//Register the ActionListener modelListener with the buttons.
phone100.addActionListener(new ModelListener());
phone110.addActionListener(new ModelListener());
phone200.addActionListener(new ModelListener());
//Create new phone model button group.
ButtonGroup phoneModel = new ButtonGroup();
//Add phone model radio buttons to button group, so they're selection is mutually exclusive.
phoneModel.add(phone100);
phoneModel.add(phone110);
phoneModel.add(phone200);
//Add the buttons to the menu.
phoneModels.add(phone100);
phoneModels.add(phone110);
phoneModels.add(phone200);
}
private DecimalFormat dollar = new DecimalFormat("###.##");
/*
* Construct the PricePane with all components.
*/
public void buildPricePane()
{
//Create the JPanel object and assign to pricePanel.
JPanel pricePanel = new JPanel();
setLayout(new GridLayout(13,1));
//Create the text labels for left hand column
packageSelected = new JLabel("Cell Phone Package: ");
modelSelected = new JLabel("Cell Phone Model: ");
optionsSelected = new JLabel("Options Selected: ");
subtotal = new JLabel("Subtotal: ");
tax = new JLabel("Sales Tax (6%): ");
total = new JLabel("Total: ");
//Create the Un-Editable text fields to display selected radio button items
pSelected = new JTextField("", 10);
pSelected.setEditable(false);
mSelected = new JTextField("", 10);
mSelected.setEditable(false);
//Create the Un-Editable text fields to display selected check box items.
oSelectedV = new JTextField(10);
oSelectedV.setEditable(false);
oSelectedT = new JTextField(10);
oSelectedT.setEditable(false);
//Create the un-editable text fields to display subtotal, tax and total.
dspSub = new JTextField("", 10);
dspSub.setEditable(false);
dspTax = new JTextField("", 10);
dspTax.setEditable(false);
dspTotal = new JTextField("", 10);
dspTotal.setEditable(false);
//Add the items and labels to JPanel.
add(packageSelected);
add(pSelected);
add(modelSelected);
add(mSelected);
add(optionsSelected);
add(oSelectedV);
add(oSelectedT);
add(subtotal);
add(dspSub);
add(tax);
add(dspTax);
add(total);
add(dspTotal);
}
/*
* ActionListener interface for the cell phone package radio buttons.
* Sends text and prices to PricePane, to display and sum respectively.
*/
private class PackageListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(package300.isSelected())
pSelected.setText("300 minutes per month: $45.00 per month");//Sends display text
if(package800.isSelected())
pSelected.setText("800 minutes per month: $65.00 per month");
if(package1500.isSelected())
pSelected.setText("1500 minutes per month: $99.00 per month");
}
}
/*
* ItemListener interface for the cell phone options check boxes.
*/
private class OptionsListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
if(vmail.isSelected())
oSelectedV.setText("Voice mail: $5.00 per month");
if(text.isSelected())
oSelectedT.setText("Text messaging: $10.00");
}
}
/*
* ActionListener for the cell phone model radio buttons.
*/
private class ModelListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(phone100.isSelected())
mSelected.setText("Model 100: $29.95");
if(phone110.isSelected())
mSelected.setText("Model 110: $49.95");
if(phone200.isSelected())
mSelected.setText("Model 200: $99.95");
}
}
/*
* ActionListener interface for exit button to close application.
*/
private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
/*
* Main function to launch GUI.
*/
public static void main(String[] args)
{
new CellPhoneCalculator();
}
}
答案 0 :(得分:0)
buildPhonePackageMenu();//Builds phone package menu
这一行 public CellPhoneCalculator()
需要删除。我搞砸了。无视。