各ボタンのイベントクリックを処理するために、選択シーケンスを作成するか、各ボタンの内部クラスを作成したいと考えています。私の課題は、ボタンをクリックしてテキストボックスに表示するか、インターフェイス内でクリックされたボタンにラベルを付けることです。
ここまでで、6 つのボタンと 2 つのパネル (各パネルに 3 つのボタン) を作成しました。私はまだインナークラスを作るのに苦労しています。ActionListener と ActionPerformed の使い方がわかりません。選択したボタンをテキストボックスに表示する内部クラスを作成するのに助けが必要です。ありがとう!
import java.awt.*;
import javax.swing.*;
public class PracticeEventDriven extends JFrame {
public PracticeEventDriven()
{
JButton firstButton = new JButton("Button 1");
JButton secondButton = new JButton("Button 2");
JButton thirdButton = new JButton("Button 3");
JButton fourthButton = new JButton("Button 4");
JButton fifthButton = new JButton("Button 5");
JButton sixthButton = new JButton("Button 6");
/*Something is wrong with this. I want to use Button 4 to display.*/
ActionListener listener = new ActionListener();
fourthButton.addActionListener(listener);
JPanel group1 = new JPanel();
setLayout(new FlowLayout());
group1.add(firstButton);
group1.add(secondButton);
group1.add(thirdButton);
JPanel group2 = new JPanel();
setLayout(new FlowLayout());
group2.add(fourthButton);
group2.add(fifthButton);
group2.add(sixthButton);
this.getContentPane().add(group1);
this.getContentPane().add(group2);
}
public static void main(String[] args)
{
PracticeEventDriven frame = new PracticeEventDriven();
frame.setTitle("Button Panel Example");
frame.setSize(600, 85);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}