2016-05-16 5 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); 
} 


} 

ボタン例:actionPerformed(...)

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クラスがJPanelの代わりにJButtonを拡張してはいけませんか? – Dave

+0

なぜ私はJButtonとしてそれを置くとき、ボタンが巨大で、奇妙に見えるかわかりません。だから、それを残した。 –

答えて

0

あなたは、任意のフレームに追加されません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を表示するときです。しかし、ありがとうございます –

関連する問題