0

基本的に、「btnCalculate」ボタンをクリックしたときに「lblIndividualScore」のテキストを変更したいのですが、クリックしてもラベルは変更されません。その直後にprintlnを配置しましたが、すべてが正しく計算されていることがわかりました...変更されません。

以下はコード部分です、何かアイデアはありますか?

これがアクションリスナースニペットです

else if (e.getSource() == btnCalculate) {
  setClassification();
  setTargetOrLight();
  setProneTotal();
  setStandingTotal();
  setKneelingTotal();
  setIndividualTotal();
}

これがアクションリスナーが呼び出すものです

public void setClassification() {
  classification = (String)cmbClassification.getSelectedItem();

  if (classification.equals("Senior") && target) {
    txtProne2.setEditable(true);
    txtKneeling2.setEditable(true);
    txtStanding2.setEditable(true);
    txtProne2.setVisible(true);
    txtKneeling2.setVisible(true);
    txtStanding2.setVisible(true);
    lblStanding.setText("Standing");
    lblKneeling.setText("Kneeling");
  }
  else if (classification.equals("Intermediate") || (classification.equals("Senior") && !target)) {
    txtProne2.setEditable(false);
    txtKneeling2.setEditable(false);
    txtStanding2.setEditable(false);
    txtProne2.setVisible(false);
    txtKneeling2.setVisible(false);
    txtStanding2.setVisible(false);
    lblStanding.setText("Standing");
    lblKneeling.setText("Kneeling");
  }
  else {
    txtProne2.setEditable(false);
    txtKneeling2.setEditable(false);
    txtStanding2.setEditable(false);
    txtProne2.setVisible(false);
    txtKneeling2.setVisible(false);
    txtStanding2.setVisible(false);
    lblStanding.setText("Prone");
    lblKneeling.setText("Prone");
  }
}

public void setTargetOrLight() {
  if (((String)cmbTarget.getSelectedItem()).equals("Target Rifle")) {
    target = true;
  }
  else {
    target = false;
  }
}

public void setProneTotal() {
  try {
    if (classification.equals("Senior") && target) {
      int prone1 = 0;
      int prone2 = 0;
      prone1 = Integer.parseInt(txtProne1.getText());
      prone2 = Integer.parseInt(txtProne2.getText());
      proneTotal = prone1 + prone2;
    }
    else if (classification.equals("Intermediate") || (classification.equals("Senior") && !target)) {
      proneTotal = Integer.parseInt(txtProne1.getText());
    }
    else {
      int prone1 = Integer.parseInt(txtProne1.getText());
      int prone2 = Integer.parseInt(txtStanding1.getText()); 
      int prone3 = Integer.parseInt(txtKneeling1.getText()); 
      proneTotal = prone1 + prone2 + prone3;
    }
  }
  catch(NumberFormatException nfe) {
    System.err.println(nfe + ": You must enter a valid number - Prone");
  }
}

public void setStandingTotal() {
  try {
    if (classification.equals("Senior") && target) {
      int standing1 = 0;
      int standing2 = 0;
      standing1 = Integer.parseInt(txtStanding1.getText());
      standing2 = Integer.parseInt(txtStanding2.getText());
      standingTotal = standing1 + standing2;
    }
    else if (classification.equals("Intermediate") || (classification.equals("Senior") && !target)) {
      standingTotal = Integer.parseInt(txtStanding1.getText());
    }
    else {
      standingTotal = 0;
    }
  }
  catch (NumberFormatException nfe) {
    System.err.println(nfe + ": You must enter a valid number - Standing");
  }
}

public void setKneelingTotal() {
  try {
    if (classification.equals("Senior") && target) {
      int kneeling1 = 0;
      int kneeling2 = 0;
      kneeling1 = Integer.parseInt(txtKneeling1.getText());
      kneeling2 = Integer.parseInt(txtKneeling2.getText());
      kneelingTotal = kneeling1 + kneeling2;
    }
    else if (classification.equals("Intermediate") || (classification.equals("Senior") && !target)) {
      kneelingTotal = Integer.parseInt(txtKneeling1.getText());
    }
    else {
      kneelingTotal = 0;
    }
  }
  catch (NumberFormatException nfe) {
    System.err.println(nfe + ": You must enter a valid number - Kneeling");
  }
}

public void setIndividualTotal() {
  individualTotal = proneTotal + kneelingTotal + standingTotal;
  lblIndividualTotal.setText("" + individualTotal);
  System.err.println(individualTotal);
}

上で述べたように、私はその終わりを持っていますprintln'System.err.println(individualTotal);' 合計を印刷すると、印刷されるので、数値はそこに到達しますが、lblは変更されません。

他に必要なものがあれば教えてください。

編集:

setClassification()メソッドのsetTextsも機能しません。

4

1 に答える 1

2

長いコードではなく、 SSCCEを投稿してください。JLabela が押されたときに aのテキストを更新し、JButton派手な手順なしで動作するSSCCE の例については、以下を参照してください。問題を再現する SSCCE を作成できれば、おそらく問題の原因がわかります。そうでない場合は、関係のないコード行を実行する必要はありません。

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChangeLabelText {
  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame( "TestFrame" );
        JPanel content = new JPanel( new FlowLayout(  ) );
        final JLabel label = new JLabel( "Label" );
        content.add( label );
        JButton button = new JButton( "Change text" );
        button.addActionListener( new ActionListener() {
          @Override
          public void actionPerformed( ActionEvent e ) {
            label.setText( "Another text" );
          }
        } );
        content.add( button );
        frame.getContentPane().add( content );
        frame.pack();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setVisible( true );
      }
    } );
  }
}
于 2012-04-07T17:16:50.830 に答える