0

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

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