【问题标题】:Changing label text using ActionListener使用 ActionListener 更改标签文本
【发布时间】:2015-05-11 19:40:53
【问题描述】:

我正在尝试制作一个程序,允许我在单击不同按钮时更改标签的文本,但是无论我单击哪个按钮,标签的文本都会更改为actionPerformed 的最后一行。

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Gui extends JFrame{
private JButton add;
private JButton subtract;
private JButton multiply;
private JButton divide;
private JTextField box1;
private JTextField box2;
private JLabel label;
private JButton ansbutton;
public String operator;


public Gui(){
    super("Luke's Calculator");
    setLayout(new FlowLayout());
    add = new JButton("Add");
    subtract = new JButton("Subtract");
    multiply = new JButton("Multiply");
    divide = new JButton("Divide");
    add(add);
    add(subtract);
    add(multiply);
    add(divide);
    box1 = new JTextField(10);
    label = new JLabel();
    box2 = new JTextField(10);
    ansbutton = new JButton("Answer");
    add(box1);
    add(label);
    add(box2);
    add(ansbutton);

    HandlerClass handler = new HandlerClass();
    add.addActionListener(handler);
    subtract.addActionListener(handler);
    multiply.addActionListener(handler);
    divide.addActionListener(handler);
    ansbutton.addActionListener(handler);
}
public class HandlerClass implements ActionListener{
    public double sum;
    public double fn;
    public double sn;

    public void actionPerformed(ActionEvent e) {
        if(add.isSelected())
            operator = "+";
        label.setText("+");

        if(subtract.isSelected())
            operator = "-";
        label.setText("-");

        if(multiply.isSelected())
            operator = "x";
        label.setText("x");

        if(divide.isSelected())
            operator = "/";
        label.setText("/");

        if(ansbutton.isSelected())
            JOptionPane.showMessageDialog(null, "The answer is " + sum, "Answer", JOptionPane.PLAIN_MESSAGE);
        }       
    }
}

【问题讨论】:

  • 使用方括号将每个 if 子句括起来,并在第一个 if 之后使用“else if”

标签: java jbutton actionlistener jlabel settext


【解决方案1】:

您的问题是您的 if 语句中没有包含 label.setText() 方法。因此,它将执行每个label.setText() 调用并继续覆盖文本,直到它默认为您方法中的最终调用。最好使用大括号,即使是一行if 语句,也可以避免这个问题。

 if(e.getSource() == add) {
    operator = "+";
    label.setText("+");
}

此外,由于只有一个条件为真,您可以使用else if

【讨论】:

  • 我真的很困惑,我已经按照你说的做了,现在没有改变到最后一个,它根本没有改变!有什么想法吗?
  • @LukeHebblethwaite 尝试将if 语句中的条件更改为:if(e.getSource() == add) 等...
  • 是的,谢谢!你知道为什么我的方法不起作用吗?我对它为什么不起作用很感兴趣
  • @LukeHebblethwaite isSelected() 仅适用于切换按钮。使用e.getSource() 检查哪个对象专门触发了ActionEvent,因此是您想要用于普通按钮的对象。
【解决方案2】:

在您的代码中

if(add.isSelected())
        operator = "+";
    label.setText("+");

if 后面没有大括号,所以只有设置操作符是有条件的,标签总是改变的。所以最后一个标签总是赢。

if(add.isSelected()) {
        operator = "+";
    label.setText("+");
}

或者

if(add.isSelected())
        operator = "+";
...
label.setText(operator)

【讨论】:

  • 我真的很困惑,我已经按照你说的做了,现在没有改变到最后一个,它根本没有改变!有什么想法吗?
猜你喜欢
  • 2013-04-30
  • 2011-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多