我正在看mybringback的youtube教程系列。 (该系列的第38个视频)。在代码的最底部,我有e.getactioncommand。当我单击单选按钮时,我希望它在文本字段中说些什么。例如,当我按下单选按钮1时,在文本字段中应该说“您选择了单选按钮1”。我完全按照本教程进行操作,但无法弄清楚出了什么问题。
我也有司机课。我没有在这里发布。
这是视频https://www.youtube.com/watch?v=pjS_IiNp008&list=SPDAA5DE54FB5215EC
package ActionCommandnActionListeners;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class FirstWindow extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
String s;
JCheckBox cb, cb2;
JTextField textField;
JLabel label;
JRadioButton b1, b2, b3, b4;
ButtonGroup group;
JTextArea tb;
public FirstWindow() {
super("Your Computer is very special");
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel p = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel(new GridBagLayout());
JPanel p4 = new JPanel();
b1 = new JRadioButton("Choice 1");
b1.setActionCommand("you selected num1");
b1.addActionListener(this);
b2 = new JRadioButton("Choice 2");
b2.setActionCommand("you selected num2");
b2.addActionListener(this);
b3 = new JRadioButton("Choice 3");
b3.setActionCommand("you selected num3");
b3.addActionListener(this);
b4 = new JRadioButton("Choice 4");
b4.setActionCommand("you selected num4");
b4.addActionListener(this);
group = new ButtonGroup();
group.add(b1);
group.add(b2);
group.add(b3);
group.add(b4);
p4.add(b1);
p4.add(b2);
p4.add(b3);
p4.add(b4);
JButton b = new JButton("Button 1");
JButton c = new JButton("Button 2");
p.add(b);
p.add(c);
cb = new JCheckBox("Do you LOVE bacon?");
cb2 = new JCheckBox("Do you LOVE cheese?");
p2.add(cb);
p2.add(cb2);
label = new JLabel("This is a label");
JTextArea tb = new JTextArea("This is a text area");
textField = new JTextField("text field");
c.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String s2 = textField.getText();
label.setText(s2);
}
});
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(15, 15, 15, 15);
gbc.gridx = 0;
gbc.gridy = 0;
p3.add(label, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
p3.add(tb, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
p3.add(textField, gbc);
add(p, BorderLayout.SOUTH);
add(p2, BorderLayout.NORTH);
add(p3, BorderLayout.CENTER);
add(p4, BorderLayout.WEST);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
s = "Good job kid, you harvested your corn! \n";
if (cb.isSelected()) {
s += " And of course you love bacon! \n";
}
if (cb2.isSelected()) {
s += " Most naturally you love cheese! \n";
}
JOptionPane.showMessageDialog(null, s);
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
tb.setText(e.getActionCommand());
}
}
最佳答案
JTextArea tb = new JTextArea("This is a text area");
您正在
shadowing
您的变量。您将“tb”变量定义为实例变量和局部变量。您不需要局部变量,因为您的actionPerformed()方法无法引用局部变量。因此,代码应为:
//JTextArea tb = new JTextArea("This is a text area");
tb = new JTextArea("This is a text area");
I'm watching a youtube tutorial series by mybringback
我从来没有弄清楚为什么人们会使用youtube作为教程。
我将从Swing tutorial开始。这样,您不仅可以按照自己的进度阅读教程,而且可以实际下载代码,并编译,测试和更改代码。因此,您从工作代码开始,然后自定义代码以执行所需的操作。
关于java - getActionCommand在代码中不起作用(观看教程系列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20316708/