如何通过ActionPerformed获取JButton标签文本?

发布于 2025-02-13 01:54:53 字数 1498 浏览 1 评论 0原文

 public Keypad() {
        setLayout(new GridLayout(4,3));
        for(int i=9; i>=0; i--){
            JButton tmp = new JButton(Integer.toString(i));
            add(tmp);
            tmp.addActionListener(this);
        }
        JButton btnPoint = new JButton(".");
        JButton btnEqual = new JButton("=");
        add(btnPoint);
        btnPoint.addActionListener(this);
        add(btnEqual);
        btnEqual.addActionListener(this);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }

    public void actionPerformed(ActionEvent event) {
        System.out.println(event.getSource());
    }

作为JFRAME的初学者,我尝试使用循环创建一些JButton。但是,我不知道如何处理通过相应按钮的操作,因为它们具有相同的变量名称“ tmp”,所以if(event.getSource()== tmp)可能不适合这种情况。 。

在ActionPerformed中,我尝试通过单击不同的按钮来打印源,结果是: javax.swing.jbutton [,0,0,75x29,alignmentX = 0.0,Alignmenty = 0.5,border = com.apple.apple.laf.aquabuttonborder $ dynamic@4BE40942,flags = 288,maximimsize =,maximimSize =,最小size =,prefimumsize =,preferredsizesize = ,defaulticon =,disabledicon =,disabledsectedicon =,m argin = javax.swing.plaf.insetsuiresource [top = 0,左= 2,底部= 0,右= 2],paintborder = true,paintfocus = true,prastedicon =,prolovereNabled = falsoverenabled = false,frolovericon =,lolloverectecon =,lolloversectedicon =,selectedicon = ,text = 9,defaultcapable = true]

似乎可以在我单击“ 9”按钮时正确获取按钮标签text = 9。那么,我可以做之类的事情,如果(event.getSource().getText()==“ 9”)

 public Keypad() {
        setLayout(new GridLayout(4,3));
        for(int i=9; i>=0; i--){
            JButton tmp = new JButton(Integer.toString(i));
            add(tmp);
            tmp.addActionListener(this);
        }
        JButton btnPoint = new JButton(".");
        JButton btnEqual = new JButton("=");
        add(btnPoint);
        btnPoint.addActionListener(this);
        add(btnEqual);
        btnEqual.addActionListener(this);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }

    public void actionPerformed(ActionEvent event) {
        System.out.println(event.getSource());
    }

As a beginner of JFrame, I try to create some JButton with for loop. However, I have no idea how to handle the actionPerformed by corresponding button as they have the same variable name "tmp", so if(event.getSource() == tmp) may not suitable for this case.

In the actionPerformed, I try to print out the source by clicking different button, the result is: javax.swing.JButton[,0,0,75x29,alignmentX=0.0,alignmentY=0.5,border=com.apple.laf.AquaButtonBorder$Dynamic@4be40942,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=9,defaultCapable=true]

It seem that it can get the button label text=9 correctly when I click button "9". so can I do something like if(event.getSource().getText() == "9") ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩物 2025-02-20 01:54:53

如何处理通过相应按钮的操作效果,因为它们具有相同的变量名称“ TMP”

实际上范围 tmp键>键盘类构造函数。因此,您无法在方法ActionPerformed中引用它。

您将相同的ActionListener分配给所有jbutton s,并且需要一种方法来确定Action Perperformed方法,哪个jbutton单击。由于您创建的每个jbutton都有不同的文本,因此您获得jbutton文本的想法似乎是一种做到这一点的方法。那么,如何获取在您的Action Performed方法中单击的jbutton的文本?

A JButton具有 buttonmodel 具有 action命令是一个字符串。默认情况下,jbutton的文本也是其 Action命令ActionEvent,即方法ActionPerformed的参数,声明方法 getActionCommand 返回单击的jbutton Action命令。因此,在actionperformed方法中获取jbutton的文本的最简单方法是调用方法getActionCommand action> action Performed << /代码>方法参数。

public void actionPerformed(java.awt.event.ActionEvent event) {
    String buttonText = event.getActionCommand();
    switch (buttonText) {
        case "1":
            // Handle button "1".
            break;
        // Remaining cases...
    }
}

此网页也可能有帮助:
如何编写动作

听众分开actionListener与每个jbutton。自Java 8以来,这很容易使用 ActionListener是一个功能接口。

public Keypad() {
    setLayout(new GridLayout(4,3));
    for(int i=9; i>=0; i--){
        JButton tmp = new JButton(Integer.toString(i));
        add(tmp);
        switch (i) {
            case 1:
                tmp.addActionListener(this::handleButton1);
                break;
            // Remaining cases...
        }
        tmp.addActionListener(this);
    }
}

private void handleButton1(java.awt.event.ActionEvent event) {
    // No need to check the "event" source since the source is always the same.
}

为了成为有效的方法参考,方法hellybutton1必须返回与方法Action Performed返回的相同值 - void,必须具有与方法ActionPerformed相同的参数。

how to handle the actionPerformed by corresponding button as they have the same variable name "tmp"

Actually the scope of tmp is the for loop in Keypad class constructor. Hence you can't refer to it in method actionPerformed.

You are assigning the same ActionListener to all your JButtons and you want a way to determine, in the actionPerformed method, which JButton was clicked. Since every JButton you create has a different text, your idea of getting the JButton text seems like one way to do this. So how can you get the text of the JButton that was clicked in your actionPerformed method?

A JButton has a ButtonModel which has an action command which is a string. By default, the text of the JButton is also its action command. The ActionEvent, i.e. the parameter of method actionPerformed, declares method getActionCommand which returns the action command of the JButton that was clicked. Hence the simplest way to get the text of the JButton, in your actionPerformed method, is to call method getActionCommand on the actionPerformed method parameter.

public void actionPerformed(java.awt.event.ActionEvent event) {
    String buttonText = event.getActionCommand();
    switch (buttonText) {
        case "1":
            // Handle button "1".
            break;
        // Remaining cases...
    }
}

This Web page may also be helpful:
How to Write an Action Listener

Alternatively, you can assign a separate ActionListener to each JButton. Since Java 8, this is easily done using method references because ActionListener is a functional interface.

public Keypad() {
    setLayout(new GridLayout(4,3));
    for(int i=9; i>=0; i--){
        JButton tmp = new JButton(Integer.toString(i));
        add(tmp);
        switch (i) {
            case 1:
                tmp.addActionListener(this::handleButton1);
                break;
            // Remaining cases...
        }
        tmp.addActionListener(this);
    }
}

private void handleButton1(java.awt.event.ActionEvent event) {
    // No need to check the "event" source since the source is always the same.
}

In order to be a valid method reference, method handleButton1 must return the same value that method actionPerformed returns – which is void and it must have the same parameters as method actionPerformed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文