1

我的问题是当数字是从数组列表中获取时,例如 JNumber.size()=10

    for(int a=0;a<JNumber.size();a++)
    {
         btnNumber= new JNumber(""+(a+1));
         btnNumber.setPreferredSize(new Dimension(20, 10));
         panel.setLayout(new GridLayout(10,10));
         panel.add(btnNumber, BorderLayout.SOUTH);
    }

那么如何在点击按钮时返回数字呢?

输出:

数字 2 被点击。

4

3 回答 3

2

您可以在 ActionListener 中执行此操作。使用给定的代码,这应该适用于您的情况:

如果您想要显示的文本不仅是数字,则使用特定文本初始化按钮并将按钮的 actioncommand 设置为实际数字。(见下文。)

btnNumber= new JNumber(""+(a+1));
btnNumber.setActionCommand(""+(a+1));    
btnNumber.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JButton button = (JButton)evt.getSource();
        int num = Integer.parseInt(button.getActionCommand());
        System.out.println(num);
      }
    }); 
于 2012-11-20T18:51:16.347 回答
1

这是一个小例子,希望对您有所帮助。基本上只需将JButtons 添加到JPanel,添加ActionListener到每个JButton,添加JPanelJFrame,单击按钮后,println()将使用 ActionCommand 执行JButton(+1 到 @Pr0gr4mm3r 以供setActionCommand()使用):

在此处输入图像描述

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.SwingUtilities;
import javax.swing.UIManager;

public class ButtonsTest {

    public ButtonsTest() {
        initComponents();
    }

    private void initComponents() {

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new GridLayout(2, 2));//create gridlayput to hold buttons

        ActionListener al = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                //display action command of jbutton
                String ac = ((JButton) ae.getSource()).getActionCommand();
                System.out.println(ac);
                //display full test in Jbutton
                //String text = ((JButton) ae.getSource()).getText();
                //System.out.println(text);
            }
        };

        for (int i = 0; i < 4; i++) {
            JButton b = new JButton("No: " + String.valueOf((i + 1)));
            b.setActionCommand(String.valueOf((i + 1)));
            b.addActionListener(al);
            panel.add(b);
        }

        frame.add(panel);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //set L&F and create UI on EDT
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                try {//set L&F
                    for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (Exception e) {
                    // If Nimbus is not available, you can set the GUI to another look and feel.
                }
                //create UI
                new ButtonsTest();
            }
        });
    }
}
于 2012-11-20T19:59:22.393 回答
0

Declare btnNumber as final and add an ActionListener to it:

btnNumber.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    System.out.println("Number " + btnNumber.getText() + " is clicked");
  }
});
于 2012-11-20T18:44:40.343 回答