2017-05-16 1 views
1

나는이 응용 프로그램을 만들었습니다 다시 시작부터 세트로 19에서 40로 시작합니다. 나는 40로 다시 재설정 할 수 있도록 대신에 단지 지우기 버튼지우기 버튼은 카운터 JLabel을 공백으로 만 만들지 만 재설정하지는 않습니다. 카운터 JLabel의 (pointsAvailable)가 19 일 때</p> <p><img src="https://i.stack.imgur.com/tgUOV.png" alt="image"></p> <p>예를 들어 나는 맑은 버튼을 클릭 한 후 카운터 JLabel의 내가 포인트를 추가하기 시작하지만 때, 예상대로 빈 간다 :

private void JButtonActionPerformed(java.awt.event.ActionEvent evt) {           
    speedPoints.setText(""); 
    attackPoints.setText(""); 
    defencePoints.setText(""); 
    powerPoints.setText(""); 
    agilityPoints.setText(""); 
    focusPoints.setText(""); 
    availablePoints.setText(""); 
} 

코드 JLabel의 카운터

public class addingPointsUI extends javax.swing.JFrame { 
int pointsAvailable=40; 
     int speed=0; 
     int power=0; 
     int focus=0; 
     int agility=0; 
     int defence=0; 
     int attack=0; 

코드를 위해 빈

코드를 만들고 싶습니다 +/- 버튼 : 값 추가 또는 감소 허용 "예시 전원 - 버튼"

private void powerMinusActionPerformed(java.awt.event.ActionEvent evt) {           
    if (power > 0){ 
     if (pointsAvailable <= 0) { 
    JOptionPane.showMessageDialog(null, "You are out of available points"); 
    return; 
     } 
     power = power - 1; 

     pointsAvailable = pointsAvailable +1; 

     availablePoints.setText(String.valueOf(pointsAvailable)); 

     powerPoints.setText(String.valueOf(power)); 

    }else { 

      JOptionPane.showMessageDialog(null,"You cannot take anymore points from Power"); 
    } 
} 

친절한 답장을 보내 주셔서 감사합니다.

답변

0

나는 빨리이 개 솔루션을 생각할 수 있습니다 :

private void JButtonActionPerformed(ActionEvent evt){ 
    ... 
    pointsAvailable=40; 
    speed=0; 
    power=0; 
    focus=0; 
    agility=0; 
    defence=0; 
    attack=0; 
} 

이 통계를 모두 다시 설정됩니다 : 당신의 지우기 버튼의 이벤트 핸들러에서 1. 다음과 같습니다. 2. 특정 통계가 비어 있는지 확인하는 통계를 더하거나 뺄 각 단추의 수신기에 if 문을 추가 할 수 있습니다.

if (speedPoints.getText() == ""){ 
    pointsAvailable += speed; 
    speed = 0; 
} 
+0

코드가 맞습니까? 변수 1이 speedPoints JLabel에 있기 때문에 해결 방법 1은 아무런 효과가 없습니다. 해결 방법 2는 "== 또는! ="메시지를 사용하여 문자열 비교를 제공합니다. –

+0

변수를 전역으로 설정하십시오. 이제는 유형 변수의 중요한 변수가 글로벌 일 때 String.compareTo (anotherString)과 문자열을 비교할 수 있습니다. –

+0

변수를 전역으로 만드는 방법은 무엇입니까? –

0

구글에 내 자신의 솔루션을 찾았습니다 예를 들어, 스피드 버튼에 대한 코드는 다음처럼 보일 것이다 완벽하게

private void ClearActionPerformed(java.awt.event.ActionEvent evt) {  
speedPoints.setText(String.valueOf(0)); 
powerPoints.setText(String.valueOf(0)); 
agilityPoints.setText(String.valueOf(0)); 
defencePoints.setText(String.valueOf(0)); 
focusPoints.setText(String.valueOf(0)); 
attackPoints.setText(String.valueOf(0)); 
availablePoints.setText(String.valueOf(40)); 
}          

작품

1

사용을 SpinnerNumberModelJSpinner. 모델의 값을 변경하십시오. 구성 요소가 업데이트되고 변경 사항이 모델의 현재 값에 적용됩니다.