0

各ボタンのイベントクリックを処理するために、選択シーケンスを作成するか、各ボタンの内部クラスを作成したいと考えています。私の課題は、ボタンをクリックしてテキストボックスに表示するか、インターフェイス内でクリックされたボタンにラベルを付けることです。

ここまでで、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);

    }
}
4

1 に答える 1

0

そのためActionListenerには、ボタンで使用する必要があります。ActionListenerに関するチュートリアルを読み、Oracle ドキュメントを読みます。私はあなたのコードを変更しました、それを調べてください:

public class PracticeEventDriven extends JFrame {

    private JTextField field;

    public PracticeEventDriven() {
        field = new JTextField(10);
        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");

        ActionListener listener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JButton b = (JButton) arg0.getSource();
                field.setText(b.getText());
            }
        };
        firstButton.addActionListener(listener);
        secondButton.addActionListener(listener);
        thirdButton.addActionListener(listener);
        fifthButton.addActionListener(listener);
        fourthButton.addActionListener(listener);
        sixthButton.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);
        this.getContentPane().add(field);
    }

    public static void main(String[] args) {
        PracticeEventDriven frame = new PracticeEventDriven();
        frame.setTitle("Button Panel Example");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }
}
于 2013-11-13T07:06:17.897 に答える