Java 如何使widgetviewer中的Jbutton实现正确的事件
说明:Java 如何使widgetviewer中的Jbutton实现正确的事件,java,swing,widget,jbutton,numberformatexception,Java,Swing,Widget,Jbutton,Numberformatexception,说明: 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 "
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或减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));*/
}
}
编写具有以下小部件的WidgetViewer GUI:
标签为“向上/向下”的按钮初始化为0的标签(我们将调用
这是左标签)初始化为0的标签(我们称之为
右标签)标签为“向下/向上”的按钮
按下“向上/向下”按钮时,会出现一个介于1和之间的随机数
生成10(包括),并将其添加到左侧标签,以及另一个
生成并减去介于1和10(含)之间的随机数
从右边的标签
按下“向下/向上”按钮时,会出现一个介于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
来处理这个问题,但是你需要某种模型来维护左右值,这样两个监听器都知道当前值是什么。这不是一个不合理的想法,但它需要额外的两个类来实现
相反,我们可以利用ActionEvent
的actionCommand
支持,为每个按钮指定一个特定的名称,我们可以在触发操作时查找该名称
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);
}
}
现在,在说到这一点之前,我经历了十几个想法-我的目的是尽量让它简单,而不试图引入一大堆可能不需要添加的复杂性-小心-我不知道这个练习的目的是什么,所以我可能走错了方向,而不是你的导师想让你做的
编写具有以下小部件的WidgetViewer GUI:
标签为“向上/向下”的按钮初始化为0的标签(我们将调用
这是左标签)初始化为0的标签(我们称之为
右标签)标签为“向下/向上”的按钮
按下“向上/向下”按钮时,会出现一个介于1和之间的随机数
生成10(包括),并将其添加到左侧标签,以及另一个
生成并减去介于1和10(含)之间的随机数
从右边的标签
按下“向下/向上”按钮时,会出现一个介于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
来处理这个问题,但是你需要某种模型来维护左右值,这样两个监听器都知道当前值是什么。这不是一个不合理的想法,但它需要额外的两个类来实现
相反,我们可以利用ActionEvent
的actionCommand
支持,为每个按钮指定一个特定的名称,我们可以在触发操作时查找该名称
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));
}
}
}
修改代码:
我根据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));
}
}
}
该错误是自我消除的-您无法将文本
“向上/向下”
转换为数字。您“应该”做的是维护“左值”和“右值”作为独立属性,您可以根据触发的事件与之交互*解释性错误是自解释性的-您不能将文本“向上/向下”
转换为数字。