如何将actionlistenerand actioncommand放到多个jbuttons [英] how to put actionlistenerand actioncommand to multiple jbuttons

查看:101
本文介绍了如何将actionlistenerand actioncommand放到多个jbuttons的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我希望我的按钮标记为1-9,但我不想列出每个按钮的所有动作监听器和动作命令。我怎么能这样做

so i want my buttons to be labeled 1-9 but I dont want to list out all the action listeners and action-commands for each button. How can I do that

而且我也不能使用add.ActionListener(this)所以我可以使用什么

and also I cannot use add.ActionListener(this) so what can i use

    JButton[] button = new JButton[9];
    panel.setLayout(new GridLayout(3,3));
    for (int i = 0; i < button.length; i++) {
        button[i] = new JButton();
        panel.add(button[i]);
        String bu = Integer.toString(i);
        button[i].setActionCommand(bu);
        button[i].addActionListener(new ActionListener());

对不起我刚接触java swing所以它的abit令人困惑仍然

Sorry im new to java swing so its abit confusing still

推荐答案


我不能使用add.ActionListener(this)所以我可以使用什么

I cannot use add.ActionListener(this) so what can i use

您创建一个实现ActionListener的类。

You create a class that implements an ActionListener.

或者更好的是创建一个实现Action的类。 Action与ActionListener相同。好处是Action可以与Key Bindings一起使用。

Or better yet create a class that implement Action. An Action is the same as an ActionListener. The benefit is that an Action can be used with Key Bindings.

这是一个基本的例子:

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

public class CalculatorPanel extends JPanel
{
    private JTextField display;

    public CalculatorPanel()
    {
        Action numberAction = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
//              display.setCaretPosition( display.getDocument().getLength() );
                display.replaceSelection(e.getActionCommand());
            }
        };

        setLayout( new BorderLayout() );

        display = new JTextField();
        display.setEditable( false );
        display.setHorizontalAlignment(JTextField.RIGHT);
        add(display, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(0, 5) );
        add(buttonPanel, BorderLayout.CENTER);

        for (int i = 0; i < 10; i++)
        {
            String text = String.valueOf(i);
            JButton button = new JButton( text );
            button.addActionListener( numberAction );
            button.setBorder( new LineBorder(Color.BLACK) );
            button.setPreferredSize( new Dimension(30, 30) );
            buttonPanel.add( button );

            InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            inputMap.put(KeyStroke.getKeyStroke(text), text);
            inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
            button.getActionMap().put(text, numberAction);
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Calculator Panel");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new CalculatorPanel() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

现在你可以点击按钮或输入数字和值将插入文本字段。

Now you can either click on the button or type the number and the value will be inserted into the text field.

这篇关于如何将actionlistenerand actioncommand放到多个jbuttons的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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