向循环创建的JButtons添加动作侦听器

时间:2016-02-24 01:43:39

标签: java actionlistener

我无法找到一种方法来调用动作侦听器,该侦听器返回在底部文本区域中单击的按钮的值。

我使用for循环创建了按钮,并没有明确地给按钮命名,所以我不知道在尝试合并ActionListener时如何引用它们。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class buttoner implements ActionListener {
//JFrame
JFrame frame = new JFrame("Button Game");

//Make JPanels
JPanel panelLabel = new JPanel();
JPanel buttonGrid = new JPanel(new GridLayout(0,10));
JPanel bottomPanel = new JPanel();

//JLabel
private JLabel label1 = new JLabel("The Button Game");

public buttoner() {
    //set layout
    frame.setLayout(new BorderLayout());
    frame.add(panelLabel, BorderLayout.NORTH);
    frame.add(buttonGrid, BorderLayout.CENTER);
    frame.add(bottomPanel, BorderLayout.SOUTH);
    //Set stuff
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,700);
    frame.setVisible(true);
    //Change label color
    label1.setForeground(Color.RED);
    //add Label
    panelLabel.add(label1);
    //add Buttons
    for (int i = 1; i <= 60; i++) {
        String val = Integer.toString(i);
        buttonGrid.add(new JButton(val));
    }
    //Add JText Area to bottom JPanel
    String num = "value";
    JTextArea jta = new JTextArea(num, 1, 1);
    bottomPanel.add(jta);
    frame.pack();
}

public static void main(String args[]){
    buttoner gui = new buttoner();
}

public void actionPerformed(ActionEvent a) {

}
}

2 个答案:

答案 0 :(得分:1)

我创建了一个动作侦听器,将值放在GUI底部的文本区域中。

The Button Game

我修复了你的代码的一些问题。

  1. 在main方法中,我调用了SwingUtilities invokeLater方法将Swing GUI放在Event Dispatch thread(EDT)上。必须在EDT上创建和更新Swing组件。

  2. Java类的名称必须以大写字母开头。

  3. 将Swing组件放在JPanel上会更安全,而不是直接将它们添加到JFrame中。

  4. 我将创建JFrame的代码与创建JPanels的代码分开。您的代码的任何读者(包括您自己)都应该更容易理解正在发生的事情。

  5. 在createMainPanel方法中,我对代码进行了分组,以便将与buttonGrid JPanel有关的所有内容放在代码中的一个位置。

  6. 我将动作侦听器添加到创建buttonGrid JPanel的代码中。

  7. 我编写了动作侦听器代码,用左键单击按钮标签更新JTextArea。

  8. 这是更正后的代码。

    package com.ggl.testing;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    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.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    
    public class Buttoner implements ActionListener {
        // JFrame
        private JFrame frame = new JFrame("Button Game");
    
        // Make JPanels
        private JPanel panelLabel = new JPanel();
        private JPanel buttonGrid = new JPanel(new GridLayout(0, 10));
        private JPanel bottomPanel = new JPanel();
    
        // JLabel
        private JLabel label1 = new JLabel("The Button Game");
    
        private JTextArea jta;
    
        public Buttoner() {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(createMainPanel());
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        private JPanel createMainPanel() {
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
    
            // Change label color
            label1.setForeground(Color.RED);
            // add Label
            panelLabel.add(label1);
            panel.add(panelLabel, BorderLayout.NORTH);
    
            // add Buttons
            for (int i = 1; i <= 60; i++) {
                String val = Integer.toString(i);
                JButton button = new JButton(val);
                button.addActionListener(this);
                buttonGrid.add(button);
            }
            panel.add(buttonGrid, BorderLayout.CENTER);
    
            // Add JText Area to bottom JPanel
            String num = "value";
            jta = new JTextArea(num, 1, 1);
            jta.setEditable(false);
            bottomPanel.add(jta);
            panel.add(bottomPanel, BorderLayout.SOUTH);
    
            return panel;
        }
    
        public static void main(String args[]) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    new Buttoner();
                }
            };
            SwingUtilities.invokeLater(runnable);
        }
    
        public void actionPerformed(ActionEvent a) {
            JButton button = (JButton) a.getSource();
            jta.setText(button.getText());
        }
    }
    

答案 1 :(得分:0)

尝试创建一个按钮数组,并将新创建的按钮添加到数组中。见评论。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class buttoner implements ActionListener {
//JFrame
JFrame frame = new JFrame("Button Game");

//Make JPanels
JPanel panelLabel = new JPanel();
JPanel buttonGrid = new JPanel(new GridLayout(0,10));
JPanel bottomPanel = new JPanel();

//JLabel
private JLabel label1 = new JLabel("The Button Game");
private JButton buttons[] = new JButton[60]; //create an array of button for future reference

public buttoner() {
    //set layout
    frame.setLayout(new BorderLayout());
    frame.add(panelLabel, BorderLayout.NORTH);
    frame.add(buttonGrid, BorderLayout.CENTER);
    frame.add(bottomPanel, BorderLayout.SOUTH);
    //Set stuff
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,700);
    frame.setVisible(true);
    //Change label color
    label1.setForeground(Color.RED);
    //add Label
    panelLabel.add(label1);
    //add Buttons
    for (int i = 1; i <= 60; i++) {
        String val = Integer.toString(i);
        JButton btn = new JButton(val);
        btn.addActionListener(this); //add an actionListener right away
        buttons[i] = btn; //add the button in the array for future reference
        buttonGrid.add(btn);
    }
    //Add JText Area to bottom JPanel
    String num = "value";
    JTextArea jta = new JTextArea(num, 1, 1);
    bottomPanel.add(jta);
    frame.pack();
}

public static void main(String args[]){
    buttoner gui = new buttoner();
}

public void actionPerformed(ActionEvent a) {

}
}