Java 创建选定的序列或内部类

Java 创建选定的序列或内部类,java,swing,actionlistener,inner-classes,Java,Swing,Actionlistener,Inner Classes,我想为每个按钮创建一个选择序列或创建一个内部类来处理每个按钮的事件单击。我的任务是单击一个按钮以显示在文本框中,或者标记在界面中单击的按钮 到目前为止,我制作了六个按钮和两个面板(每个面板上有三个按钮)。我仍然在努力建立一个内部阶级。我不知道如何使用ActionListener和ActionPerformed。我需要帮助使内部类在文本框中显示所选按钮。谢谢 import java.awt.*; import javax.swing.*; public class PracticeEventDr

我想为每个按钮创建一个选择序列或创建一个内部类来处理每个按钮的事件单击。我的任务是单击一个按钮以显示在文本框中,或者标记在界面中单击的按钮

到目前为止,我制作了六个按钮和两个面板(每个面板上有三个按钮)。我仍然在努力建立一个内部阶级。我不知道如何使用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);

    }
}

为此,您需要在按钮上使用
ActionListener
。阅读有关的教程,然后阅读。我已更改了您的代码,请检查:

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);

    }
}