- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个程序,其中应该选择 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();
}
}
最佳答案
buildPhonePackageMenu();//Builds phone package menu
这行在 公共(public) CellPhoneCalculator()
需要删除。我搞砸了。无视。
关于java - JTextField 卡在 ActionListener 的特定文本上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45514282/
在 Android 的 API > 19 中是否有任何方法可以获取可移动 SD 卡的路径? 与外部 SD 卡一样,我们有 Environment.getExternalStorageDirectory
一些 Android 设备有 microSD(或其他存储卡)插槽,通常安装为 /storage/sdcard1 据我所知,自 Android 4.4 起 Google 限制了对此内存的访问,并在 An
我使用 Java Card 2.1.2 SDK 和 GPShell 作为与设备通信的方式在 Java Card 上构建一个项目。我从 GpShell 测试了 helloworld 示例,并成功发送了
我开发了一个应用程序,它有一个来电接收器,它适用于所有手机。一位用户有一部双 SIM 卡安卓手机。该应用程序适用于第一张 SIM 卡。但是当有人调用他的第二张 SIM 卡时,我们的应用程序不会被调用。
我有一个带预览的文件输入。 这是笔 Codepen 我想强制高度,我无法理解我该怎么做。我想将此组件的高度固定为 300px(示例),我还需要保持加载图像的正确纵横比,用灰色背景填充空白。现在我保持宽
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 6年前关闭。 Improve this qu
我正在使用此代码访问 SD card : import os from os.path import join from jnius import autoclass #from android.pe
我正在为数据记录设备编写固件。它以 20 Hz 的频率从传感器读取数据并将数据写入 SD 卡。但是,向SD卡写入数据的时间并不一致(大约200-300 ms)。因此,一种解决方案是以一致的速率将数据写
我正在使用以下代码将视频放到网站上,但是在垂直方向上,手机屏幕上只能看到视频的左半部分 我不是网络开发人员。有人可以告诉我确切的内容吗,如何使其正确放置在手机屏幕上? 是在youtube iframe
我正在使用 Vuetify 1.5 和 Vuetify 网格系统来设置我的布局。现在我有一个组件 HelloWorld我将其导入到我的 Parent 中成分。我已经在我的 HelloWorld 中设置
我使用 python 制作了一个简单的二十一点游戏。我制作了游戏的其余部分,但我正在努力放入 ASCII 卡,所以这只是代码的一小部分。我尝试将 * len(phand) 放在附加行的末尾。虽然这确实
我正在使用玩家卡设置 Twitter 卡。它可以在预览工具中运行,但文档说它需要在“twitter.com 现代桌面浏览器? native iOs 和 Android Twitter 应用程序?mob
任何旧的 GSM 兼容 SIM 卡(3G USIM 的奖励)。 我想我需要一些硬件?谁能为业余爱好者推荐一些便宜的东西,以及一些更专业的东西? 我认为会有一个带有硬件的 API 的完整文档,所以也许这
我使用 python 制作了一个简单的二十一点游戏。我制作了游戏的其余部分,但我正在努力放入 ASCII 卡,所以这只是代码的一小部分。我尝试将 * len(phand) 放在附加行的末尾。虽然这确实
我记得前一段时间读到有 cpu 卡供系统添加额外的处理能力来进行大规模并行化。任何人都有这方面的经验和任何资源来研究项目的硬件和软件方面吗?这项技术是否不如传统集群?它更注重功率吗? 最佳答案 有两个
我检查外部存储是否已安装并且可用于读/写,然后从中读取。我使用的是确切的官方 Android 示例代码 ( from here )。 它说外部存储未安装。 getExternalFilesDir(nu
在 Android 2.1 及更低版本中,Android 应用程序可以请求下载到 SD 卡上吗?另外我想知道应用程序是否可以请求一些包含视频的文件夹下载到 SD 卡上?以及如何做到这一点? 提前致谢。
我们编写了一个 Windows 设备驱动程序来访问我们的自定义 PCI 卡。驱动程序使用 CreateFile 获取卡的句柄。 我们最近在一次安装中遇到了问题,卡似乎停止工作了。我们尝试更换卡(更换似
有些新设备(例如 Samsung Galaxy)带有两个 SD 卡。我想知道是否有任何方法可以确定设备是否有两张 SD 卡或一张 SD 卡。谢谢 最佳答案 我认为唯一的方法是使用 检查可用根的列表 F
我正在尝试将文件读/写到 SD 卡。我已经尝试在我的真实手机和 Eclipse 中的模拟器上执行此操作。在这两种设备上,对/mnt/sdcard/或/sdcard 的权限仅为“d--------”,我
我是一名优秀的程序员,十分优秀!