2016-04-09 7 views
0

ここに私のコードがあります。最初のものが最初に。これは宿題ではありません。これは私の次の試験のための学習ガイドです。私はJLabelをpublicにするのに問題があるので、keyboardlistenerメソッドで使用できます。あなたはJLabelを公開するときにそれが正しいかどうかを見るためにそれを見ることができますか?Java。グローバルJLabels

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); 
     } 
    } 

} 
+0

何のフィールドはあなたの例では全くありません。だから何も公開することはできません。とにかく何もすべきではない。このコードはコンパイルできません。Driver.catName()メソッドはどこにも定義されていません。あなたの質問は何ですか? –

+0

KeyListenerの代わりにキーバインディングAPIを使用する – MadProgrammer

答えて

1

私はあなたがそのクラスのいずれかの方法でそれを使用することができるようになりますそのあとは、クラスのメンバーとして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); 
    ....