将动作侦听器添加到循环创建的JButton中 [英] Adding an action listener to JButtons created by a loop

查看:96
本文介绍了将动作侦听器添加到循环创建的JButton中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having trouble finding a way to invoke an action listener that returns the value of the button clicked in the text area at the bottom.

我使用for循环制作了按钮,但没有明确给按钮起一个名字,所以在尝试合并<$ c $时我不知道如何引用它们c> ActionListener 。

I made the buttons using a for loop and did not expressly give the buttons a name so I do not know how to reference them when trying to incorporate an 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) {

}
}


推荐答案

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

I created an action listener to put the value in the text area at the bottom of the GUI.

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

I fixed a few problems with your code.


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

  1. In the main method, I called the SwingUtilities invokeLater method to put the Swing GUI on the Event Dispatch thread (EDT). Swing components must be created and updated on the EDT.

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

The name of a Java class must start with a capital letter.

将Swing组件放在JPanel上而不是直接将它们添加到JFrame更为安全。

It's safer to put your Swing components on a JPanel, rather than add them directly to a JFrame.

我分开了从创建JPanels的代码创建JFrame的代码。对于包括您自己在内的代码的任何阅读者,都应该更容易理解发生了什么。

I separated the code that creates the JFrame from the code that creates the JPanels. It should be easier for any reader of your code, including yourself, to understand what's going on.

在createMainPanel方法中,我对代码进行了分组,以便所有内容与buttonGrid JPanel关联(以一个实例为例)位于代码的一个位置。

In the createMainPanel method, I grouped the code so that everything having to do with the buttonGrid JPanel, to take one instance, is in one place in the code.

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

I added the action listener to the code that creates the buttonGrid JPanel.

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

I wrote action listener code that updates the JTextArea with the left clicked button label.

这是正确的代码。

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

这篇关于将动作侦听器添加到循环创建的JButton中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆