【发布时间】:2017-01-19 16:15:54
【问题描述】:
我正在尝试获取变量correct,其中包含从Window 类中的方法actionPerformed 到Display 类中的方法paint 的信息。我不知道该怎么做,因为Window 类在调用时也需要 3 个 int 值。
public class Window extends JFrame implements ActionListener
{
/**
* Constructor for Window
*/
public Window(int width, int height, int gridSize)
{
//save instance variables:
WIDTH = width;
HEIGHT = height;
this.gridSize = gridSize;
}
public void actionPerformed(ActionEvent e)
{
int moveCode;
int direction;
Words guess = new Words();
if(e.getActionCommand().equals("button")) //button has been pressed
{
//get entered values from textfields:
String num = numField.getText();
String dir = ltrField.getText();
String word = wordField.getText();
String correct;
//convert to number:
int numberWord = Integer.parseInt(num) - 1;
//convert "A" or "D" to 0 or 1:
if(dir == "A")
{
direction = 0;
}
else
{
direction = 1;
}
if(guess.words[numberWord][direction].equals(word))
{
correct = word;
}
else
{
correct = "No";
}
}
}
}
public class Display extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g); //clear the old drawings
final int PAD = 20; //extra space so field isn't right at edges
if(correct == "Java")
{
}
}
}
【问题讨论】:
-
我只是在这里猜测,但 9/10 次窗口对象将在您的超类中创建(您在其中调用 paintComponent)。从那里获取窗口并添加一个 getter 方法。
标签: java class variables methods