简体   繁体   English

Swing JButton事件

[英]Swing JButton event

I have JButton and want on event call this class method show() when this button in pressed. 我有JButton并希望在事件调用此类方法show()时按下此按钮。 I know how to this, if I want use method from another class, but I need call method from same class as button. 我知道怎么做,如果我想从另一个类使用方法,但我需要从同一个类的调用方法作为按钮。

JButton search = new JButton(new ButtonAction("Search", KeyEvent.VK_A));

I try add 我尝试添加

JButton search = new JButton(show());

But it works only 1 time when object creating, but not when button is pressed. 但它在对象创建时只能工作一次,但在按下按钮时却不能工作。

If I understand you correctly, you want to call a method from the class that you created the button in rather than a method from another class. 如果我理解正确,你想从你创建按钮的类中调用一个方法,而不是从另一个类调用一个方法。

First off, notice that while you are technically calling a method here 首先,请注意,虽然您在技术上在这里调用方法

JButton search = new JButton(new ButtonAction("Search", KeyEvent.VK_A));

what is really going on is that you are passing a ButtonAction object to the JButton constructor. 真正发生的是你将ButtonAction对象传递给JButton构造函数。 The closest thing that I can think of that will get what you want is having the class you are using extend ButtonAction. 我能想到的最接近的东西就是得到你想要的东西就是让你使用的类扩展ButtonAction。

Not sure what you want here... but if you want to attach an actionListener() to a JButton , you can do the following. 不知道你想要什么...但是如果你想将一个actionListener()附加到JButton ,你可以执行以下操作。 Why do you need to listen to A ? 为什么你需要听A

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

public class ActionListenerExample1 extends JFrame
        implements ActionListener, KeyListener {
    private static final long serialVersionUID = 1L;

    private JTextField searchText;
    private JButton searchButton1;
    private JButton searchButton2;

    public ActionListenerExample1() {
        initialize();

        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    protected void initialize() {
        searchText = new JTextField(30);
        searchButton1 = new JButton("Search 1");
        searchButton2 = new JButton("Search 2");

        searchText.addKeyListener(this);
        searchButton1.addActionListener(this);
        searchButton2.addActionListener(new ButtonAction());

        this.setLayout(new FlowLayout());
        this.add(searchText);
        this.add(searchButton1);
        this.add(searchButton2);
    }

    public static void main(String[] args) {
        new ActionListenerExample1();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == searchButton1) {
            buttonAction("GLOBAL LISTENER");
        }
    }

    private class ButtonAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            buttonAction("LOCAL LISTENER");
        }
    }

    private void buttonAction(String label) {
        JOptionPane.showMessageDialog(this,
            String.format("%s: %s", label, searchText.getText()));
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyChar() == KeyEvent.VK_A) {
            buttonAction("KEYBOARD");
        }
    }

    @Override
    public void keyReleased(KeyEvent e) { }

    @Override
    public void keyTyped(KeyEvent e) { }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM