2016-05-16 99 views
1

制作一个计算器,用于将最终,有有我的JLabel更新困难没有更新。当标签初始化时,它显示的opNum起始值就好了,但是每当方法被调用来添加一个数字的opNum最后,它似乎没有更新。我不确定它是否不重绘,或者当我从按钮调用方法时出现问题。我正在做一些愚蠢或不好的事情,或者两者兼而有之。帮帮我?的JLabel当按下按钮

的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"); 
    } 
} 
} 
+0

你不应该以大写字母开头的你的方法的名称。 – Dave

+0

另外,你的Button1类不应该扩展JButton而不是JPanel吗? – Dave

+0

不知道为什么,但当我把它作为JButton,按钮是巨大的,看起来很奇怪。所以我离开了它。 –

回答

0

actionPerformed(...)您编辑NumberText.Edit("1")不添加任何帧的新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"); 
     } 
    } 
} 
+0

请原谅我,我强调,因为这是明天到期的,而且我对Java还比较陌生,你能举一个你的意思吗? –

+0

@PatrickDonny我编辑了我的答案来包含一个例子。 – Titus

+0

你现在对我来说简直就是英雄。我唯一的问题是,现在当第一次按下按钮时,它显示为空以及1.但是,谢谢 –