我需要在我在此程序中创建的字段中显示文本,该字段在 actionEvent 中标识,即 RadioButton 选择。我很难让选择显示在现场。请帮忙?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JBasketball {
public static void main(String args[]) {
JFrame frame = new JFrame("JBasketball");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField field = new JTextField(16);
JPanel panel = new JPanel(new FlowLayout());
ButtonGroup group = new ButtonGroup();
JRadioButton sixers = new JRadioButton("Philadelphia 76ers");
JRadioButton raptors = new JRadioButton("Toronto Raptors");
JRadioButton lakers = new JRadioButton("Los Angeles Lakers");
JRadioButton sonics = new JRadioButton("Seattle Supersonics");
JRadioButton bullets = new JRadioButton("Baltimore Bullets");
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
JRadioButton aButton = (JRadioButton) actionEvent.getSource();
String team = aButton.getText();
//This is where I need the field to display the team name
field.setText(team);
}
};
panel.add(sixers);
group.add(sixers);
panel.add(raptors);
group.add(raptors);
panel.add(lakers);
group.add(lakers);
panel.add(sonics);
group.add(sonics);
panel.add(bullets);
group.add(bullets);
panel.add(field);
sixers.addActionListener(action);
raptors.addActionListener(action);
lakers.addActionListener(action);
sonics.addActionListener(action);
bullets.addActionListener(action);
field.addActionListener(action);
frame.add(panel);
frame.setSize(500, 130);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}