我如何在我的控件查看器中创建Jbutton来实现适当的事件

问题描述 投票:0回答:1

说明:

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));*/


    }
}
java widget jbutton
1个回答
0
投票

编写具有以下小部件的WidgetViewer GUI:

标记为“向上/向下”的按钮,其标签初始化为0(我们称为这是左侧标签)初始化为0的标签(我们将其称为右侧标签)标为“向下/向上”的按钮]

按下“向上/向下”按钮时,将在1到1之间的一个随机数生成10(含)并添加到左侧标签,另一个生成并减去1到10(含)之间的随机数从右边的标签。

按下“向下/向上”按钮时,将在1到1之间的一个随机数生成10(含)并从左侧标签中减去,并且另一个介于1和10(含)之间的随机数会生成,并且添加到正确的标签。

所以,有很多重要的事情需要我们解决。

  • 这些值最初被初始化为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));
    }
}

好的,但这只能从一个方向解决问题,向下/向上怎么办?

好吧,我们可以编写第二个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);
    }

}

现在,在理解这一点之前,我经历了十几个想法-我的观点是尝试使它尽可能简单,而又不尝试引入可能不需要添加的一堆复杂性-提防-我不知道练习的目的是什么,所以我可能已经从您的老师试图让您做的那一个方向走错了]

© www.soinside.com 2019 - 2024. All rights reserved.