[英]JLabel won't show the proper value of Getter
In my project, my problem is that the JLabel won't show the incremented value from the getter. 在我的项目中,我的问题是JLabel不会显示来自吸气剂的增量值。 It should be adding up everytime I choose the correct radiobutton.
每当我选择正确的单选按钮时,它应该累加起来。
This is the first JFrame 这是第一个JFrame
public class DifEasy extends javax.swing.JFrame {
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// jPanel1.setVisible(false);
if (q1a1.isSelected()){
ScoreStorage mehh = new ScoreStorage();
mehh.setRawscore(mehh.getRawscore()+1);
}
this.setVisible(false);
new DifEasy1().setVisible(true);
}
This is the 2nd JFrame 这是第二个JFrame
public class DifEasy1 extends javax.swing.JFrame {
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (q1a1.isSelected()){
ScoreStorage mehh = new ScoreStorage();
mehh.setRawscore(mehh.getRawscore()+1);
}
this.setVisible(false);
new DifEasy2().setVisible(true);
}
This is the 3rd JFrame 这是第三个JFrame
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (q1a1.isSelected()){
ScoreStorage mehh = new ScoreStorage();
mehh.setRawscore(mehh.getRawscore()+1);
jLabel1.setText(String.valueOf(mehh.getRawscore()));
}
}
btw I just put a JLabel there for testing. 顺便说一句,我只是在这里放置了一个JLabel进行测试。 After clicking the JButton (Given that I choosed the q1a1 radiobutton), the JLabel should change into 3, but it shows up only 0.
单击JButton(鉴于我选择了q1a1单选按钮)后,JLabel应该更改为3,但它仅显示0。
Getters and Setters class Getters和Setters类
public class ScoreStorage {
private int Rawscore = 0;
public void setRawscore(int rawscore){
this.Rawscore = Rawscore;
}
public int getRawscore(){
return Rawscore;
}
public synchronized void increment(){
setRawscore(Rawscore);
}
public int reset(){
Rawscore = 0;
return Rawscore;
}
}
(Based on the comments from RubioRic and MadProgrammer ) (基于RubioRic和MadProgrammer的评论)
The code has two problems: 该代码有两个问题:
ScoreStorage
doesn't work: ScoreStorage
中的设置器不起作用: You've got a typo in ScoreStorage.setRawscore, you are assigning
this.Rawscore = Raswcore
instead ofthis.Rawscore = rawscore
therefore the value ofRawscore
is always 0.你有在ScoreStorage.setRawscore一个错字,你要分配
this.Rawscore = Raswcore
而不是this.Rawscore = rawscore
因此价值Rawscore
始终为0。
(also note that ScoreStorage.increment()
probably doesn't do what it should since it only reassign the value.) (还请注意,
ScoreStorage.increment()
可能没有做应做的事情,因为它仅重新分配了值。)
ScoreStorage
objects. ScoreStorage
对象。 Each time you select an option, you are creating a brand new instance of
ScoreStorage
, which is initialised to0
.每次选择一个选项时,您都将创建一个全新的
ScoreStorage
实例,该实例被初始化为0
。You can implement a method
setScoreStorage
or create a constructor that accepts that argument in your JFrames.您可以实现
setScoreStorage
方法或创建一个在setScoreStorage
中接受该参数的构造函数。
Here is a short example how to pass one ScoreStorage
between the different JFrame with a constructor 这是一个简短的示例,说明如何使用构造函数在不同的JFrame之间传递一个
ScoreStorage
public class DifEasy extends JFrame {
private ScoreStorage scoreStorage;
public DifEasy(ScoreStorage scoreStorage) {
this.scoreStorage = scoreStorage;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (q1a1.isSelected()){
scoreStorage.setRawscore(scoreStorage.getRawscore()+1);
}
this.setVisible(false);
new DifEasy1(scoreStorage).setVisible(true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.