【问题标题】:JPanel, instantiate an array of JButtonsJPanel,实例化一个 JButtons 数组
【发布时间】:2013-07-23 09:31:02
【问题描述】:

我无法将我的 Panel 课程放到我的主课程中。我似乎无法将按钮实例化并注册到 actionPerformed 方法。这个项目假设使用带有 9 个输入按钮(3 个其他按钮用于输入、空格和清除)的网格布局,然后在 JTextArea 上显示输入。我相信我已经正确设置了 Panel 类,但是将 JButton 数组放在一起,然后将其注册到 actionPerformed 方法时遇到问题。任何指针将不胜感激。 (附带问题,如何复制和粘贴代码,并将其全部包含在代码块中?)

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class TextButtons extends JFrame implements ActionListener {

    private JButton[] buttons;
    private JTextArea textArea;
    private final int ENTER;   //Index of Enter button in buttons
    private final int SPACE;    //Index of Space button in buttons
    private final int CLEAR;    //Index of Clear button in buttons

    public TextButtons(String title) {
        super(title);
        JFrame frame = new JFrame("Text Button");

        //TODO: instantiate all JButtons, add them to the buttons array,
        //  and register "this" as the ActionListener for each button.

        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = new JButton();
            //this.buttons.addActionListener(e);
        }

        ENTER = 9;
        SPACE = 10;
        CLEAR = 11;
        buttons[ENTER] = new JButton("\n");
        buttons[SPACE] = new JButton(" ");
        buttons[CLEAR] = new JButton("clear");

        JTextArea textArea = new JTextArea();
        textArea.setEditable(false);

        TextButtonsPanel mainPanel = new TextButtonsPanel(buttons, textArea);

        this.getContentPane().add(mainPanel);
        this.pack();
        this.setVisible(true);
    }

    /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        // ??
    }

    public static void main(String[] args) {
        final TextButtons f = new TextButtons("Text Buttons");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

面板类

    public class TextButtonsPanel extends JPanel {

        public TextButtonsPanel(JButton[] buttons, JTextArea textArea) {

            int ENTER = 11;

            JPanel mainPanel = new JPanel(new GridLayout(4, 3));

            JButton b1 = new JButton("A");
            JButton b2 = new JButton("B");
            JButton b3 = new JButton("C");
            JButton b4 = new JButton("1");
            JButton b5 = new JButton("2");
            JButton b6 = new JButton("3");
            JButton b7 = new JButton("X");
            JButton b8 = new JButton("Y");
            JButton b9 = new JButton("Z");

            add(b1);
            mainPanel.add(b2);
            mainPanel.add(b3);
            mainPanel.add(b4);
            mainPanel.add(b5);
            mainPanel.add(b6);
            mainPanel.add(b7);
            mainPanel.add(b8);
            mainPanel.add(b9);

            JScrollPane scrollPane = new JScrollPane(textArea);
            scrollPane.setPreferredSize(new Dimension(80, 120));
            mainPanel.add(scrollPane);
        }
    }
}

【问题讨论】:

  • //this.buttons.addActionListener(e);应该是buttons[i].addActionListener(this);
  • 1) 对代码块使用一致且符合逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。 2) 为了尽快获得更好的帮助,请发布SSCCE。 3) “附加问题”实际上这是该文本中的“唯一问题”。
  • @MehulRathod 仔细看看。添加到数组中的按钮从不使用
  • 别在意我的上一篇文章

标签: java arrays swing jbutton actionlistener


【解决方案1】:

我似乎无法实例化按钮并将其注册到 actionPerformed 方法。

你可以这样组织你的代码:

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class TextButtonPanel extends JPanel implements ActionListener {

    public TextButtonPanel(String[] labels) {
        setLayout(new GridLayout(0, 3));  //same as self.setLayout(..)

        JButton button;

        for (String label : labels) {
            button = new JButton(label);
            button.addActionListener(this);
            add(button);  //same as self.add(button)
        }
    }   

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

}


class MyGui {

    public MyGui() {
        JFrame frame = new JFrame("Name");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setBounds(200, 100, 500, 300);
        Container cpane = frame.getContentPane();

        String[] labels = {"A", "B", "C", "D"};
        TextButtonPanel panel = new TextButtonPanel(labels);
        cpane.add(panel);

        frame.setVisible(true);
    }
}



public class SwingProg {
    private static void createAndShowGUI() {
        new MyGui();
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

【讨论】:

    【解决方案2】:

    您的TextButtonsPanel 扩展了JPanel,但您将所有内容添加到JPanel,称为mainPanel,它从未开始添加到任何内容中。

    因为JButtons 是局部变量,所以您永远无法将外部ActionListener 附加到它们。不确定这是好事还是坏事。

    我会创建一个TextButtonsPane,它只包含按钮,没有其他组件。它所做的只是生成事件。

    我会提供一个addActionListenerremoveActionListener,它们只会针对其中包含的所有按钮注册侦听器。

    这样,按钮窗格不关心它的用途,它只是产生其他东西可以使用的事件。

    您可能需要一些时间阅读Creating a GUI with Swing

    【讨论】:

    • Your TextButtonsPanel extends JPanel, but you are adding everything to JPanel called mainPanel 该操作对 JFrame 做了同样的事情。那里显然有些混乱。
    • 我将重新访问 GUI Swing api 文档。我想我脑子里有一些基本的基础错误。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    相关资源
    最近更新 更多