このプログラムで作成したフィールド内にテキストを表示する必要があります。このフィールドは、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);
}
}