如何访问 JMenu 上的项目并使用 Action Listener?

我的 java 代码遇到了一个问题,其中动作侦听器不起作用。我应该创建一个 GUI,它有一个菜单栏,当点击这些按钮时,它会执行一些操作。例如,如果用户选择“Meals”菜单上的一个选项和“Dorms”菜单上的一个选项,它应该计算这些项目中的每一个分配的值,然后输出到总成本 JField。

http://img2.mukewang.com/61b2baa5000159f602710230.jpg

http://img1.mukewang.com/61b2baaa00016d8802730224.jpg

这就是我的代码的样子


private class MenuActionListener implements ActionListener {


//The user has chosen a new dorm or new meal plan

//Get the choices from the dormBox and mealBox and recalculate charges

//Display the new charges in the totalField


  public void actionPerformed(ActionEvent arg0) {

    //Get the choices from the dormBox and mealBox

    int totalCost;

    int dormIndex = menu.getComponentCount();

    int dormCost=dormCosts[dormIndex];

    int mealIndex=dorm.getComponentCount();

    int mealCost=mealCosts[mealIndex];

    //Calculate the charges

    totalCost=dormCost+mealCost;


    //Display the new charges 

    totalField.setText("$"+Integer.toString(totalCost));

  }

}

我应该如何让它运行良好..?


天涯尽头无女友
浏览 248回答 2
2回答

蝴蝶不菲

不要编写试图猜测要使用的正确值的侦听器。当您向特定组件注册侦听器时,您已经知道它是哪个组件,因此您可以注册一个侦听器,为该组件做正确的事情。在这种情况下,当成本存储在数组中时,还应该有一个相应的数组用于构建菜单项,它允许构建项和相应的侦听器。例如static String[] dorms&nbsp; = { "Allen Hall", "Pike Hall", "Farthing Hall" };static int[] dormCosts = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 20,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 40 };static String[] meals&nbsp; = { "7 / weak", "14 / week", "unlimited" };static int[] mealCosts = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 15 };JTextField totalField = new JTextField();int dormCost = dormCosts[0];int mealCost = mealCosts[0];void updateTotalCosts() {&nbsp; &nbsp; int totalCost = dormCost + mealCost; // really plus not multiply?&nbsp; &nbsp; totalField.setText("$" + totalCost);}JMenuBar buildMenu() {&nbsp; &nbsp; final JMenuBar mb = new JMenuBar();&nbsp; &nbsp; JMenu menu = mb.add(new JMenu("Meals"));&nbsp; &nbsp; for(int ix = 0; ix < meals.length; ix++) {&nbsp; &nbsp; &nbsp; &nbsp; int currMealCosts = mealCosts[ix];&nbsp; &nbsp; &nbsp; &nbsp; menu.add(meals[ix]).addActionListener(ev -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mealCost = currMealCosts;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateTotalCosts();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; menu = mb.add(new JMenu("Dorms"));&nbsp; &nbsp; for(int ix = 0; ix < dorms.length; ix++) {&nbsp; &nbsp; &nbsp; &nbsp; int currDormCosts = dormCosts[ix];&nbsp; &nbsp; &nbsp; &nbsp; menu.add(dorms[ix]).addActionListener(ev -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dormCost = currDormCosts;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateTotalCosts();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; return mb;}在每次循环迭代中,我们已经知道我们正在创建哪个项目以及与它相关的成本,因此我们使用这些成本注册一个监听器。如果您不能使用 lambda 表达式,则构造方法的前 Java 8 变体将如下所示JMenuBar buildMenu() {&nbsp; &nbsp; final JMenuBar mb = new JMenuBar();&nbsp; &nbsp; JMenu menu = mb.add(new JMenu("Meals"));&nbsp; &nbsp; for(int ix = 0; ix < meals.length; ix++) {&nbsp; &nbsp; &nbsp; &nbsp; final int currMealCosts = mealCosts[ix];&nbsp; &nbsp; &nbsp; &nbsp; menu.add(meals[ix]).addActionListener(new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent ev) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mealCost = currMealCosts;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateTotalCosts();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; menu = mb.add(new JMenu("Dorms"));&nbsp; &nbsp; for(int ix = 0; ix < dorms.length; ix++) {&nbsp; &nbsp; &nbsp; &nbsp; final int currDormCosts = dormCosts[ix];&nbsp; &nbsp; &nbsp; &nbsp; menu.add(dorms[ix]).addActionListener(new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent ev) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dormCost = currDormCosts;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateTotalCosts();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; return mb;}

ABOUTYOU

恐怕这种方法行不通。您无法从 ActionListener 内部访问您的 UI 组件。您可能想尝试向 JMenuItems 添加匿名侦听器,以更新类的属性以执行计算。编辑:检查 Holger 的答案以获得一个不错的解决方案:)通过这种方式,您可以访问外部组件或更好地将其委托给模型类。见这里:https&nbsp;:&nbsp;//www.codejava.net/java-core/the-java-language/java-8-lambda-listener-example
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python