如何拆分 getActionCommand() 以获得字符串中的第一个元素?
How do I split the getActionCommand() in order to get the first element in the string?
我的程序中有一组JRadioButton
。
public class SalePanel extends JPanel implements View {
private JTextField sell = new JTextField(5);
private ButtonGroup buttons = new ButtonGroup();
private Stadium stadium;
我在这里添加了按钮:
private void build(Stadium stadium){
add(buttonBox(stadium));
}
这就是我创建按钮的方式:
private Box buttonBox(Stadium stadium)
{ Box box = Box.createVerticalBox();
SaleListener listener = new SaleListener();
for (Group group: stadium.groups()) {
box.add(button(group, listener));
}
return box;
}
private JRadioButton button(Group group, SaleListener listener){
JRadioButton button = new JRadioButton();
button.addActionListener(listener);
buttons.add(button);
button.add(Box.createHorizontalStrut(35));
button.add(new JLabel(group.name() + " @ $"+ formatted(group.price())));
return button;
}
按钮的标签在这里:button.add(new JLabel(group.name()
<--- 这是另一个 class 中的标签,其名称类似于 "Front"、"Middle" 或其他什么。
我现在的任务是:
- 从事件中获取单选按钮标签(我使用
getActionCommand()
获取标签)
- 从标签中获取组名(我需要在space上拆分字符串,并获取返回数组中的第一个字符串)。
- 从名称中找到组:
查找给定名称的组
所以,我是按以下方式做的:
private class SaleListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String[] words = e.getActionCommand().split(" ");
String groupName = words[0];
for (Group group: stadium.groups()) {
if (group.matches(groupName)){
group.sell(sale());
update();
}
}
}
}
不幸的是,它不起作用,我找不到错误在哪里。
你能给我关于这个任务的任何建议吗?我做错了什么?
p.s。这行代码 String[] words = e.getActionCommand().split(" ");
没有按照我的意愿行事。我试了System.out.println(words[0])
,结果是空的,不过应该有组名:(
这个...
button.add(new JLabel(group.name() + " @ $"+ formatted(group.price())));
没有意义。你应该使用...
button.setText(group.name() + " @ $"+ formatted(group.price()));
还有...
button.setActionCommand(group.name());
如果您不关心剩下的文字...
我的程序中有一组JRadioButton
。
public class SalePanel extends JPanel implements View {
private JTextField sell = new JTextField(5);
private ButtonGroup buttons = new ButtonGroup();
private Stadium stadium;
我在这里添加了按钮:
private void build(Stadium stadium){
add(buttonBox(stadium));
}
这就是我创建按钮的方式:
private Box buttonBox(Stadium stadium)
{ Box box = Box.createVerticalBox();
SaleListener listener = new SaleListener();
for (Group group: stadium.groups()) {
box.add(button(group, listener));
}
return box;
}
private JRadioButton button(Group group, SaleListener listener){
JRadioButton button = new JRadioButton();
button.addActionListener(listener);
buttons.add(button);
button.add(Box.createHorizontalStrut(35));
button.add(new JLabel(group.name() + " @ $"+ formatted(group.price())));
return button;
}
按钮的标签在这里:button.add(new JLabel(group.name()
<--- 这是另一个 class 中的标签,其名称类似于 "Front"、"Middle" 或其他什么。
我现在的任务是:
- 从事件中获取单选按钮标签(我使用
getActionCommand()
获取标签) - 从标签中获取组名(我需要在space上拆分字符串,并获取返回数组中的第一个字符串)。
- 从名称中找到组: 查找给定名称的组
所以,我是按以下方式做的:
private class SaleListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String[] words = e.getActionCommand().split(" ");
String groupName = words[0];
for (Group group: stadium.groups()) {
if (group.matches(groupName)){
group.sell(sale());
update();
}
}
}
}
不幸的是,它不起作用,我找不到错误在哪里。 你能给我关于这个任务的任何建议吗?我做错了什么?
p.s。这行代码 String[] words = e.getActionCommand().split(" ");
没有按照我的意愿行事。我试了System.out.println(words[0])
,结果是空的,不过应该有组名:(
这个...
button.add(new JLabel(group.name() + " @ $"+ formatted(group.price())));
没有意义。你应该使用...
button.setText(group.name() + " @ $"+ formatted(group.price()));
还有...
button.setActionCommand(group.name());
如果您不关心剩下的文字...