java - 按下按钮时 JLabel 不更新

标签 java jpanel jbutton jlabel

为期末考试制作计算器,在更新 JLabel 时遇到困难。当 Label 初始化时,它会很好地显示 opNum 的起始值,但是每当调用该方法以在 opNum 末尾添加数字时,它似乎不会更新。我不确定它是否没有重新绘制,或者当我从按钮调用该方法时是否出现问题。我正在做一些蠢事或坏事,或者两者兼而有之。帮忙?

JLabel 和方法类:

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

public class NumberText extends JPanel
{
private JLabel opNumLabel;
private String opNum;
private double storeNum = 0;
private boolean firstOp = true;
private char op;
private char sign = '+';

//Panel for Number
public NumberText()
{
    //I heard that it could be an issue with the Label not having enough space, though this didnt help
    opNumLabel = new JLabel ("1234567890111");
    opNumLabel.setText(opNum);
    add(opNumLabel);
    setPreferredSize (new Dimension (150, 40));
    setBackground (Color.white);
}

//Clears the current typed number
public void Clear ()
{
    opNum = "";
    opNumLabel.setText(opNum);
}

//Sets it back to conditions like the start of the program
public void ClearAll ()
{
    opNum = "";
    storeNum = 0;
    firstOp = true;
    opNumLabel.setText(opNum);
}


//for storing the operation
public void OpStore (char newOp)
{
    op = newOp;
    firstOp = false;
}

//for adding to the opNum
public void Edit (String button)
{
    opNum = opNum + button;
    opNumLabel.setText(opNum);
    opNumLabel.repaint();
}

//for testing for the first Operation
public boolean IsFirstOp ()
{
    return firstOp;
}

//for changing the sign
public void SignChange ()
{
    if (sign == '+')
    {
        opNum = "-" + opNum;
        opNumLabel.setText(opNum);
        sign = '-';
    }

    else if (sign == '-')
    {
        opNum.replaceAll("-", "");
        opNumLabel.setText(opNum);
        sign = '+';
    }
}

//for storing a number when an operation is pressed
public void StoreNum ()
{
    storeNum = Double.parseDouble(opNum);
    opNum = "";
}

//Math when an Operation is to be done
public void Operation()
{
    double value = Double.parseDouble(opNum);
    double total = 0;

    switch(op)
    {
        case '+': total = storeNum + value;
            break;

        case '-': total = storeNum - value;
            break;

        case '/': total = storeNum / value;
            break;

        case '*': total = storeNum * value;
            break;

        case '%': total = storeNum % value;
            break;
    }

    opNum = Double.toString(total);
    opNumLabel.setText(opNum);
}


}

按钮示例:

public class Button1 extends JPanel
{
private JButton push;
private JLabel label;
public Button1 ()
{

  push = new JButton ("1");
  push.addActionListener (new ButtonListener());

  add (push);
}
private class ButtonListener implements ActionListener
{
  public void actionPerformed (ActionEvent event)
  {
      //to avoid non-static cant be ref by static
     NumberText NumberText = new NumberText();
     NumberText.Edit("1");
  }
}
}

最佳答案

actionPerformed(...) 中,您正在编辑 NumberText.Edit("1") 一个新的 NumberText,该新的 NumberText 未添加到任何框架中。如果您希望在 Edit(...) 方法中所做的更改生效,则需要获取对已显示的 NumberText 的引用。

这是一个例子:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    ....
    NumberText numberText = new NumberText();
    Button1 button = new Button1(numberText);
    JPanel pan = new JPanel();
    pan.add(numberText);
    pan.add(button);
    frame.add(pan);
    .....
}


public class Button1 extends JPanel {
    private JButton push;
    private JLabel label;
    NumberText alreadyDisplayed;
    public Button1 (NumberText alreadyDisplayed) {
        this.alreadyDisplayed = alreadyDisplayed;
        push = new JButton ("1");
        push.addActionListener (new ButtonListener());
        add(push);
    }

    private class ButtonListener implements ActionListener {
        public void actionPerformed (ActionEvent event) {
            alreadyDisplayed.Edit("1");
        }
    }
}

关于java - 按下按钮时 JLabel 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37246749/

相关文章:

java - 如何在RelativeLayout中固定Textview的位置

java - 如何使用不同的 fxml 文件创建多个 javafx Controller ?

java - Java中文件传输的FTP客户端服务器模型

java - 在另一个 JPanel 中重新绘制 JPanel 的问题

java - 由空指针异常引起的初始化程序异常

java - iload_1、iload_2、iload_3 和 iload #index 字节码有什么区别?

Java 面板无法绘制

java - 在 JOptionPane showConfirmDialog 中更改按钮文本

java - 执行 .bat 文件时启用按钮

java - GridBagLayout 从屏幕中间启动 Jbuttons