Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swing 单击按钮后,JLabel不会更改_Swing_Jlabel_Settext - Fatal编程技术网

Swing 单击按钮后,JLabel不会更改

Swing 单击按钮后,JLabel不会更改,swing,jlabel,settext,Swing,Jlabel,Settext,基本上,我想在点击按钮“btnCalculate”时更改“lblIndividualScore”的文本…但当我点击它时,标签不会更改。我在它后面放了一个println,我知道所有的计算都是正确的…它不会改变 下面是代码部分,有什么想法吗 下面是操作侦听器片段 else if (e.getSource() == btnCalculate) { setClassification(); setTargetOrLight(); setProneTotal(); setStandingT

基本上,我想在点击按钮“btnCalculate”时更改“lblIndividualScore”的文本…但当我点击它时,标签不会更改。我在它后面放了一个println,我知道所有的计算都是正确的…它不会改变

下面是代码部分,有什么想法吗

下面是操作侦听器片段

else if (e.getSource() == btnCalculate) {
  setClassification();
  setTargetOrLight();
  setProneTotal();
  setStandingTotal();
  setKneelingTotal();
  setIndividualTotal();
}
下面是action listener调用的内容

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()方法中的setxts也不起作用。

请发布一段而不是一段长代码。按下
JButton
时更新
JLabel
文本的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 );
      }
    } );
  }
}