我怎样才能让我的小部件查看器中的 Jbutton 实现正确的事件

How can i make the Jbutton in my widgetviewer to implement the proper event

说明:

Write a WidgetViewer GUI that has the following widgets:

a button labeled "go up/down"
a label initialized to 0 (we'll call this the left label)
a label initialized to 0 (we'll call this the right label)
a button labeled "go down/up"

When the "go up/down" button is pushed, a random number between 1 and 10 (inclusive)
is generated and added to the left label, and another random number between 1 and 10 (inclusive)
is generated and subtracted from the right label.

When the "go down/up" button is pushed, a random number between 1 and 10 (inclusive) is
generated and subtracted from the left label, and another random number between 1 and 10
(inclusive) is generated and added to the right label.

错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Go up/down"

代码:

我似乎无法理解此错误消息,可能是我添加了一个字符串但我不确定,这可能是我的逻辑。但我想弄清楚如何让名为 goupdown 和 godownup 的 Jbutton 显示随机数,然后在需要时加或减 1。

public class UpAndDown {


    public UpAndDown() {

    WidgetViewer wv = new WidgetViewer();
    JButton goUpDown = new JButton("Go up/down");
    JLabel leftLabel = new JLabel("0");
    wv.add(goUpDown, 10, 30, 150, 20);
    wv.add(leftLabel, 10, 60, 150, 25);

    JButton goDownUp = new JButton("Go down/up");
    JLabel rightLabel = new JLabel("0");
    wv.add(goDownUp, 10, 120, 150, 20);
    wv.add(rightLabel, 10, 160, 150, 25);

    ButtonIncrement action = new ButtonIncrement(goUpDown);
    goUpDown.addActionListener(action);

    ButtonIncrement action2 = new ButtonIncrement(goDownUp);
    goDownUp.addActionListener(action2);
}

static class ButtonIncrement implements ActionListener {

    private final JButton goUpDownBtn;
    private final JButton goDownUpBtn;

    public ButtonIncrement(JButton buttonToModify) {
        goUpDownBtn = buttonToModify;
        goDownUpBtn = buttonToModify;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int rand = (int) (Math.random() * 10);
        String val = goUpDownBtn.getText();
        //JButton jc = (JButton) e.getSource();
        int newVal = Integer.parseInt(val) + rand;
        goDownUpBtn.setText(String.valueOf(newVal));
        //jc.setText(goDownUpBtn.getText() + " " + val);// get string, convert to int, add rand value. then convert int back to string and set text to string

        /*String val2 = goDownUpBtn.getText();
        int newVal2 = Integer.parseInt(val2) + rand;
        goDownUpBtn.setText(String.valueOf(newVal2));*/


    }
}

Write a WidgetViewer GUI that has the following widgets:

a button labeled "go up/down" a label initialized to 0 (we'll call this the left label) a label initialized to 0 (we'll call this the right label) a button labeled "go down/up"

When the "go up/down" button is pushed, a random number between 1 and 10 (inclusive) is generated and added to the left label, and another random number between 1 and 10 (inclusive) is generated and subtracted from the right label.

When the "go down/up" button is pushed, a random number between 1 and 10 (inclusive) is generated and subtracted from the left label, and another random number between 1 and 10 (inclusive) is generated and added to the right label.

所以,我们需要从中删除一些重要的东西。

  • 这些值最初初始化为 0
  • 您需要在触发按钮时更新标签
  • 根据哪个按钮被触发,将决定哪些值被添加或减去

所以,我们可以简单地从

开始
class IncrementDecrementAction implements ActionListener {

    private final JLabel leftLabel;
    private final JLabel rightLabel;

    private int leftValue = 0;
    private int rightValue = 0;

    public IncrementDecrementAction(JLabel leftLabel, JLabel rightLabel) {
        this.leftLabel = leftLabel;
        this.rightLabel = rightLabel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int rand = (int) (Math.random() * 10) + 1;
        leftValue += rand;
        rand = (int) (Math.random() * 10) + 1;
        rightValue += rand;
        this.leftLabel.setText(Integer.toString(leftValue));
        this.rightLabel.setText(Integer.toString(rightValue));
    }
}

好的,但这只能解决一个方向的问题,那down/up呢?

好吧,我们可以写第二个 ActionListener 来处理这个问题,但是您需要某种模型来维护左右值,以便两个听众都知道当前值是什么。不是一个不合理的想法,但它需要额外的两个 类 才能让它发挥作用。

相反,我们可以利用 ActionEventactionCommand 支持并为每个按钮指定一个特定的名称,我们可以在触发操作时查找...

class IncrementDecrementAction implements ActionListener {

    private final JLabel leftLabel;
    private final JLabel rightLabel;

    private int leftValue = 0;
    private int rightValue = 0;

    public IncrementDecrementAction(JLabel leftLabel, JLabel rightLabel) {
        this.leftLabel = leftLabel;
        this.rightLabel = rightLabel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        int leftRandom = (int) (Math.random() * 10) + 1;
        int rightRandom = (int) (Math.random() * 10) + 1;
        if (e.getActionCommand().equals("upDown")) {
            leftValue += leftRandom;
            rightValue -= rightRandom;
        } else if (e.getActionCommand().equals("downUp")) {
            leftValue -= leftRandom;
            rightValue += rightRandom;
        }

        this.leftLabel.setText(Integer.toString(leftValue));
        this.rightLabel.setText(Integer.toString(rightValue));
    }
}

这基本上改变了基于事件源的计算方向 - 而且它不需要知道事件源实际上是什么(按钮、菜单项、键绑定),它只是不在乎。

您只需将适当的 actionCommand 应用到适当的 Jbutton 即可进行设置,例如...

public class TestPane extends JPanel {

    private JLabel leftLabel;
    private JLabel rightLabel;

    private JButton upDownButton;
    private JButton downUpButton;

    public TestPane() {
        leftLabel = new JLabel("0");
        rightLabel = new JLabel("0");

        upDownButton = new JButton("Up/down");
        downUpButton = new JButton("Down/up");

        upDownButton.setActionCommand("upDown");
        downUpButton.setActionCommand("downUp");

        ActionListener actionListener = new IncrementDecrementAction(leftLabel, rightLabel);

        upDownButton.addActionListener(actionListener);
        downUpButton.addActionListener(actionListener);

        add(leftLabel);
        add(rightLabel);

        add(upDownButton);
        add(downUpButton);
    }

}

现在,在达到这一点之前,我经历了十几个想法 - 我的观点是尽量保持简单,而不是试图引入一大堆可能不需要添加的复杂性- 小心 - 我不知道练习的目的是什么,所以我可能从你的教练试图让你做的方向走错了方向

修改后的代码:

我根据 WV 对象的说明修改了您给出的答案以显示 GUI,这是正确的版本。

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;



public class UpAndDown extends JPanel {

private JLabel leftLabel;
private JLabel rightLabel;

private JButton upDownButton;
private JButton downUpButton;


public UpAndDown() {


    WidgetViewer wv = new WidgetViewer();

    JButton upDownButton  = new JButton("Up/down");
    wv.add(upDownButton, 10, 30, 150, 20);
    JButton downUpButton = new JButton("Down/up");
    wv.add(downUpButton, 10, 120, 150, 20);

    JLabel leftLabel = new JLabel("0");
    wv.add(leftLabel, 10, 60, 150, 25);
    JLabel rightLabel = new JLabel("0");
    wv.add(rightLabel, 10, 140, 150, 25);

    upDownButton.setActionCommand("upDown");
    downUpButton.setActionCommand("downUp");

    ActionListener actionListener = new IncrementDecrementAction(leftLabel, rightLabel);

    upDownButton.addActionListener(actionListener);
    downUpButton.addActionListener(actionListener);

}

static class IncrementDecrementAction implements ActionListener {

    private final JLabel leftLabel;
    private final JLabel rightLabel;

    private int leftValue = 0;
    private int rightValue = 0;

    public IncrementDecrementAction(JLabel leftLabel, JLabel rightLabel) {
        this.leftLabel = leftLabel;
        this.rightLabel = rightLabel;
    }

    public void actionPerformed(ActionEvent e) {

        int leftRandom = (int) (Math.random() * 10) + 1;
        int rightRandom = (int) (Math.random() * 10) + 1;
        if (e.getActionCommand().equals("upDown")) {
            leftValue += leftRandom;
            rightValue -= rightRandom;
        } else if (e.getActionCommand().equals("downUp")) {
            leftValue -= leftRandom;
            rightValue += rightRandom;
        }

        this.leftLabel.setText(Integer.toString(leftValue));
        this.rightLabel.setText(Integer.toString(rightValue));
    }

}
}