我正在試着學習這個獲取值 - 設置值 - 返回它來自何處的值編碼。我聽說這是一種'傳遞價值或參考'的問題,做了我的研究,但仍然困擾着這個問題。無論如何,這是我的源代碼:將A類設置值的值從A類設置爲B,並將B類的值返回給A
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中,任何提示可以理解
長碼...你能縮小它到你面臨的問題嗎?這裏我們必須爲你做。 –
對不起!等待。我會縮小它的範圍。 @Vakh –