다음과 같은 문제가 있습니다.
내 메인 클래스에는 다음 줄이 있습니다.
Integer i;
update.addActionListener(new RewardUpdater(this));
if (argument) {
i++;
}
그리고 RewardUpdater 클래스에는 다음이 있습니다.
int i;
this.i = frame.i;
rewardButtonAddition.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateCenterPanel.removeAll();
c.repaint();
text.setText("Test: " + i);
c.add(beschriftung);
updateCenterPanel.add(additionReward1);
updateCenterPanel.add(additionReward2);
updateCenterPanel.add(additionReward3);
}
});
그러나 얼마나 자주 i ++에 대한 if 반복을 채우더라도 상관 없습니다.
내 나는 항상 0으로 인쇄됩니다.
제한된 코드에 대해 죄송합니다. 모든 것이 꽤 지저분해서 여기에 필요한 것만 넣으려고했습니다. 더 필요한 경우 제공 할 수 있습니다.
짧고 빠른 답변에 감사드립니다!
진심으로 모리츠
당신은 당신의 원하는 경우 Action
(예 : JButton
클릭) 방금 추가 할 수있는 가치 증가하는 i++
내부 ActionListener
.
반면에 다른 곳에서 값을 늘리려면 다음과 같이 새 클래스를 만드는 것이 좋습니다.
public class RewardValue {
private int value;
public RewardValue(int startValue) {
this.value = startValue;
}
public void increment() {
value++;
}
public int getValue() {
return value;
}
}
그런 다음 계속해서를 만들고 RewardValue
필요한 곳으로 전달할 수 있습니다. 당신은 기본적으로 당신의 교환 i
과 함께 RewardValue
. public 메소드 increment
는 당신이 가지고있는 곳에서 호출되어야합니다 i++
. 공개 메소드 get
가 있으므로 새 i
. 작은 예는 다음과 같습니다.
public class MainClass {
private final RewardValue rewardValue = new RewardValue(0);
public MainClass() {
//initiate update
//...
update.addActionListener(new RewardUpdater(rewardValue));
//of cause the next lines don't need to be in the constructor
if (argument) {
rewardUpdater.increment();
}
}
}
public class RewardUpdater implements ActionListener {
private final RewardValue rewardValue;
public RewardUpdater(RewardValue rewardValue) {
this.rewardValue = rewardValue;
}
public void actionPerformed(AcionEvent e) {
//... the other lines
text.setText("Test: "+rewardValue.get());
// ... the other lines
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다