放置在5个不同JPanel中的按钮数组中的动作侦听器

时间:2014-05-05 00:32:56

标签: java swing jbutton actionlistener

我有一项任务是将字母表中的字母放在5个面板中,并使用我在下面编码的布局。当您选择一个按钮时,它会更改JLabel以读取单击的按钮。 我做的第一种方式是,我通过将每个按钮分别添加到每个面板并添加所有ActionListeners和文本来编写所有内容。这是大约200线,但工作正常。我认为将JButtons放入数组并在此处添加侦听器和文本会更简洁。我能够将JFrame设置为布局,但我不知道如何使用ActionListener。我是否必须添加MouseListener以找出点击的面板然后确定按下了按钮?

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class JFileCabinet1 extends JFrame implements ActionListener {
    String[] lettersPanel1 = { "A", "B", "C", "D", "E", "F" };
    String[] lettersPanel2 = { "G", "H", "I", "J", "K", "L" };
    String[] lettersPanel3 = { "M", "N", "O", "P", "Q", "R" };
    String[] lettersPanel4 = { "S", "T", "U", "V", "W", "X" };
    String[] lettersPanel5 = { "Y", "Z" };

    private JPanel jp1 = new JPanel(new GridLayout(1, 6));
    private JPanel jp2 = new JPanel(new GridLayout(1, 6));
    private JPanel jp3 = new JPanel(new GridLayout(1, 6));
    private JPanel jp4 = new JPanel(new GridLayout(1, 6));
    private JPanel jp5 = new JPanel(new GridLayout(1, 6));

    private JButton jblank1 = new JButton();
    private JButton jblank2 = new JButton();
    private JButton jblank3 = new JButton();
    private JLabel jl = new JLabel("");

    private Container con = getContentPane();
    private GridLayout layout = new GridLayout(5, 1, 5, 5);

    public JFileCabinet1() {

        super("File cabinet");

        con.add(jp1);
        for (int i = 0; i < lettersPanel1.length; i++) {
            JButton button[] = new JButton[lettersPanel1.length];
            button[i] = new JButton(lettersPanel1[i]);
            jp1.add(button[i]);
            button[i].addActionListener(this);
        }

        con.add(jp2);
        for (int i = 0; i < lettersPanel2.length; i++) {
            JButton button[] = new JButton[lettersPanel2.length];
            button[i] = new JButton(lettersPanel2[i]);
            jp2.add(button[i]);
            button[i].addActionListener(this);
        }

        con.add(jp3);
        for (int i = 0; i < lettersPanel2.length; i++) {
            JButton button[] = new JButton[lettersPanel3.length];
            button[i] = new JButton(lettersPanel3[i]);
            jp3.add(button[i]);
            button[i].addActionListener(this);
        }
        con.add(jp4);
        for (int i = 0; i < lettersPanel4.length; i++) {
            JButton button[] = new JButton[lettersPanel4.length];
            button[i] = new JButton(lettersPanel4[i]);
            jp4.add(button[i]);
            button[i].addActionListener(this);
        }

        con.add(jp5);
        for (int i = 0; i < lettersPanel5.length; i++) {
            JButton button[] = new JButton[lettersPanel5.length];
            button[i] = new JButton(lettersPanel2[i]);
            jp5.add(button[i]);
            button[i].addActionListener(this);
        }

        jp5.add(jblank1).setVisible(false);
        jp5.add(jblank2).setVisible(false);
        jp5.add(jblank3).setVisible(false);

        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLayout(layout);
    }

    public void actionPerformed(ActionEvent e) {
        JButton src = (JButton) e.getSource();

    }

    public static void main(String[] args) {
        JFileCabinet1 frame = new JFileCabinet1();
        frame.setVisible(true);

    }
}

2 个答案:

答案 0 :(得分:2)

  

我是否必须添加鼠标侦听器才能找到单击的面板   然后确定按下了按钮?

不,不要在按钮上使用鼠标侦听器。您的ActionListener实施正常,getSource将为您提供按下的按钮。

要将标签设置为按钮文字,请使用jl.setText(src.getText())内的actionPerformed

如果您需要按钮所在的面板,请使用按钮的getParent()

答案 1 :(得分:2)

实际情况是,对于ActionListener,您实际上并不需要任何方式的数组,一旦actionPerformed被触发,您就可以从{{JButton获取ActionEvent 1}} s source属性并从按钮中提取文本...

public void actionPerformed(ActionEvent e) {
    JButton src = (JButton) e.getSource();
    System.out.println(src.getText());
}

现在,如果您希望按钮携带更多信息(或不同的信息,那么显示的内容),您可以使用按钮actionCommand属性。

另外,这......

for (int i = 0; i < lettersPanel2.length; i++) {
    JButton button[] = new JButton[lettersPanel2.length];

没有意义。每次循环运行时,它都会创建一个新的JButton数组,它只在for-loop

的上下文中创建一个本地按钮数组

如果你看一下你的for-loop,它们通常会做同样的事情,唯一的区别是源内容(字符数组)以及你添加按钮的位置。您编写了一个方法来执行此操作,例如......

public void addButtonsTo(String[]  letters, JPanel panel)  {
    for (int i = 0; i < letters.length; i++) {
        JButton button = new JButton(letters[i]);
        panel.add(button);
        button.addActionListener(this);
    }
}

简单地称之为......

addButtonsTo(lettersPanel1, jp1);
addButtonsTo(lettersPanel2, jp2);
addButtonsTo(lettersPanel3, jp3);
addButtonsTo(lettersPanel4, jp4);
addButtonsTo(lettersPanel5, jp5);

请仔细阅读How to Use Buttons, Check Boxes, and Radio ButtonsHow to Write an Action Listener了解更多详情......