【发布时间】:2014-03-16 21:33:29
【问题描述】:
我正在尝试学习这种获取值 - 设置值 - 从编码风格返回的值。我听说这是一种“通过价值或参考”的问题,我做了研究,但仍然坚持这个问题。不管怎样,这是我的源代码:
System.out.println(x);
int x = object2.getX(); **//I HAVE A PROBLEM HERE**
int x2 = rand.nextInt(100);
int y = rand.nextInt(100);
int xpost = rand.nextInt(300);
int ypost = rand.nextInt(150);
allField[x] = new JTextField(String.format(" %s + %s", x2 , y));
allField[x].setBounds(xpost, ypost, 100, 30);
allField[x].setEnabled(false);
add(allField[x]);
object2.setX(x++); **//I HAVE A PROBLEM HERE TOO**
我正在尝试通过 object2.getX(); 从另一个类中获取值;从那里它将设置数组的索引。完成数组的设置后,它仍然为 0,我想增加它(x++),然后这个值为 1 的值被传递给另一个类并设置它。
这是另一个类:
public class TimerTutorial extends JFrame {
JLabel promptLabel, timerLabel;
int counter, x = 0;
int changeTest;
JTextField tf;
JButton button;
Timer timer;
public int getX(){**//I HAVE A PROBLEM HERE**
return x;
}
public int setX(int y){**//I HAVE A PROBLEM HERE**
x = y;
return this.x;
}
}
如果你想要整个代码,但问题在此之前说明:
import java.awt.event.*;
import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class TimerTutorial extends JFrame{
JLabel promptLabel, timerLabel;
int counter, x = 0;
int changeTest;
JTextField tf;
JButton button;
Timer timer;
public int getX(){
return x;
}
public int setX(int y){
x = y;
return this.x;
}
public TimerTutorial(){
setLayout(new GridLayout(2,2,5,5));
tf = new JTextField();
add(tf);
promptLabel = new JLabel("Enter seconds:", SwingConstants.CENTER);
add(promptLabel);
button = new JButton("Start Timing");
add(button);
timerLabel = new JLabel("Waiting...", SwingConstants.CENTER);
add(timerLabel);
event e = new event();
button.addActionListener(e);
}
public class event implements ActionListener{
public void actionPerformed(ActionEvent e){
int count = (int)(Double.parseDouble(tf.getText()));
timerLabel.setText("Time left:" +count);
TimeClass tc = new TimeClass(count);
timer = new Timer(1000, tc);
timer.start();
}
public class TimeClass implements ActionListener{
int counter;
public TimeClass(int counter){
this.counter = counter;
}
public void actionPerformed(ActionEvent tc){//every time timer updates this will spark
counter--;
JTextField[] allField = new JTextField [20];
TimerTutorial object2 = new TimerTutorial();
Random rand = new Random();
System.out.println(x);
int x = object2.getX();
int x2 = rand.nextInt(100);int y = rand.nextInt(100);
int xpost = rand.nextInt(300); int ypost = rand.nextInt(150);
allField[x] = new JTextField(String.format(" %s + %s", x2 , y));
allField[x].setBounds(xpost, ypost, 100, 30);
allField[x].setEnabled(false);
add(allField[x]);
object2.setX(x++);
if(counter>=1) {
timerLabel.setText("Time left: "+counter);
} else {
timer.stop();
timerLabel.setText("Done!");
Toolkit.getDefaultToolkit().beep();
}
}
}
}
public static void main(String args[]){
TimerTutorial gui = new TimerTutorial();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(800,800);
gui.setVisible(true);
}
}
代码实际上运行良好。我的目标是以秒为间隔随机发布 JTextFields,其中包含随机位置和值。这里的问题是 x 不会增加。这使我的 JTextFields 成为索引 [0]。 PS:对java还是新手,任何提示将不胜感激
【问题讨论】:
-
长代码...你能把它缩小到你面临的问题吗?在这里,我们必须为您完成。
-
对不起!等待。我会缩小范围。 @Vak
标签: java pass-by-reference pass-by-value