下面是我在底部的代码。首先要做的是。这不是家庭作业。这是我下一次考试的学习指南。我在将JLabel公开以便在keyboardlistener方法中使用它时遇到了问题。你能不能也看一下,看看当我把JLabel公之于众的时候,它看起来是否合适?
public class Driver {
public static void main( String[] args ) {
Driver gui = new Driver();
// No other Java code should be added to this method
}
/**
* Our driver class
*/
public Driver() {
Window win = new Window();
win.setLocation(50, 30);
win.setSize(500, 400);
win.setBackground( Color.DARK_GRAY );
Oval circle;
circle = new Oval(win.getWidth()/2,win.getHeight()/2,200,200);
circle.setBackground( Color.MAGENTA );
circle.setLocation(150,100);
win.add(circle);
Triangle t;
t = new Triangle(win.getWidth()/2,win.getHeight()/2,120, 120, 0);
t.setSize(30,40);
t.setBackground( Color.MAGENTA );
t.setLocation(295,110);
win.add( t );
Triangle tt;
tt = new Triangle(win.getWidth()/100, win.getHeight()/2, 120, 120, 0);
tt.setSize(30,40);
tt.setBackground( Color.MAGENTA );
tt.setLocation(175,110);
win.add( tt );
Oval eye;
eye = new Oval(win.getWidth()/2,win.getHeight()/2,200,200);
eye.setBackground( Color.BLUE );
eye.setSize(25,25);
eye.setLocation(275,160);
win.add(eye);
Oval eye2;
eye2 = new Oval(win.getWidth()/2,win.getHeight()/2,200,200);
eye2.setSize(25,25);
eye2.setBackground( Color.BLUE );
eye2.setLocation(210,160);
win.add(eye2);
Oval pupil1;
pupil1 = new Oval(win.getWidth()/2,win.getHeight()/2,200,200);
pupil1.setSize(10,10);
pupil1.setBackground( Color.BLACK );
pupil1.setLocation(215,170);
win.add(pupil1);
Oval pupil2;
pupil2 = new Oval(win.getWidth()/2,win.getHeight()/2,200,200);
pupil2.setSize(10,10);
pupil2.setBackground( Color.BLACK );
pupil2.setLocation(280,170);
win.add(pupil2);
Color myColor = new Color (12,15,23);
Triangle nose;
nose = new Triangle(win.getWidth()/100, win.getHeight()/2, 120, 120, 0);
nose.setSize(30,40);
nose.setBackground( myColor );
nose.setLocation(240,190);
win.add( nose );
Rectangle mouth = new Rectangle (win.getWidth()/2,win.getHeight()/2,200,200);
mouth.setSize(50,20);
mouth.setBackground(Color.BLACK);
mouth.setLocation(230,250);
win.add(mouth);
Line whisker1;
whisker1 = new Line(280,245,255,240);
whisker1.setBackground( Color.BLACK );
win.add( whisker1 );
Line whisker2;
whisker2 = new Line(280,220,255,240);
whisker2.setBackground( Color.BLACK );
win.add( whisker2 );
Line whisker3;
whisker3 = new Line(220,245,255,240);
whisker3.setBackground( Color.BLACK );
win.add( whisker3 );
Line whisker4;
whisker4 = new Line(220,220,255,240);
whisker4.setBackground( Color.BLACK );
win.add( whisker4 );
final JLabel catName;
catName = new JLabel();
catName.setText("George, he likes buses :(");
catName.setBounds(200, 300, 150, 20);
catName.setForeground(Color.WHITE);
win.add(catName);
Line mouth2nose;
mouth2nose = new Line(255,220,255,250);
mouth2nose.setBackground( Color.BLACK );
win.add( mouth2nose );
// Attach the keyboard listener
KeyboardListener kl = new KeyboardListener(this, win);
// Once all components are added, set visibility to make the window show up
win.setVisible(true);
}
/**
* Event handler for KeyboardListener.
* Handle a keyboard press event (when focused on the window)
*
* @param key The key that was pressed as the code from KeyEvent.
*/
public void handleKeyboardEvent(int key) {
if (key == KeyEvent.VK_UP) {
System.out.println("DEBUG: Key = UP");
Driver.catName(+0,+1,+0,+0);
} else if (key == KeyEvent.VK_DOWN) {
System.out.println("DEBUG: Key = DOWN");
Driver.catName(+0,-1,+0,+0);
} else if (key == KeyEvent.VK_LEFT) {
System.out.println("DEBUG: Key = LEFT");
Driver.catName(-1,+0,+0,+0);
} else if (key == KeyEvent.VK_RIGHT) {
System.out.println("DEBUG: Key = RIGHT");
Driver.catName(+1,+0,+0,+0);
}
}
}发布于 2016-04-10 04:28:51
我建议您将JLabel catName定义为类成员并在构造函数中进行初始化,之后您将能够在该类的任何方法中使用它。
示例:
public class Driver {
private JLabel catName;
public static void main( String[] args ) {
Driver gui = new Driver();
// No other Java code should be added to this method
}
// the constructor
public Driver() {
Window win = new Window();
win.setLocation(50, 30);
....
....
catName = new JLabel();
catName.setText("George, he likes buses :(");
catName.setBounds(200, 300, 150, 20);
catName.setForeground(Color.WHITE);
win.add(catName);
....https://stackoverflow.com/questions/36522196
复制相似问题