2012-10-30 3 views
0

저는 Tic-Tac-Toe 게임을 만들면서 Java에 익숙하지 않습니다. (ASP.NET 멤버로서 OOPS 개념을 알고 있지만 지난 2 년간 Java를 사용하지 않았습니다).Java Eclipse에서 데이터 필드 액세스를 수행하려면 어떻게해야합니까?

Deleted code here as I am putting my code below. 

여기 아래의 회선이 어떻게 작동하는지 궁금합니다.

game frame = new game(); 
frame.setVisible(true); 

는 그 프레임이 게임 클래스의 객체와() 생성자 새로운 게임에 의해 초기화하지만 어떻게 .setVisible() 메서드는이 개체에 대해 작동하는지 이해하지 이해했다.

내가 직면 한 또 다른 어려움은 다른 클래스 나 메서드에서 초기화 된 개체에 어떻게 액세스 할 수 있는지입니다. 내가 여기이 어려움에 직면하고 있기 때문에


안녕하세요, 내가 이전 질문을했다 : 먼저 내가 여기

public class Game extends JFrame { 


    private JPanel contentPane; 
    //private JPanel settingPane; 

    protected JLabel lblPlayerName2; // I am using this one for my testing 
    JLabel[] lbls = new JLabel[9]; 
    private final ButtonGroup buttonGroup = new ButtonGroup(); 


    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        Game frame = new Game(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 

    public Game() { 
     setTitle("Tic Tac Toe"); 
     setResizable(false); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 810, 607); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 
     contentPane.setName("Tic Tac Toe"); 
.  
. 
.. 
. 
. 
. 
. 
. 
. 
. 
. 
. 

    for (int i=0; i<lbls.length; i++) { 
     lbls[i] = new JLabel(""+(i+1)); 
     lbls[i].addMouseListener(new AllbtnBehvr()); 

     lbls[i].setFont(new Font("Trebuchet MS", Font.BOLD, 95)); 
     lbls[i].setHorizontalAlignment(SwingConstants.CENTER); 
     lbls[i].setBorder(new LineBorder(new Color(0, 0, 0), 2)); 
     panelGameArea.add(lbls[i]); 
    } 
     . 
    . 
    . 
    . 
    . 
    . 

    . 
    . 
    . 
    . 
    . 
    . 

} // End of Game() 

class AllbtnBehvr implements MouseInputListener{ //Created this class as SubClass of Game Class 

     @Override 
     public void mouseClicked(MouseEvent src) { 
      // TODO Auto-generated method stub 

      JLabel b = (JLabel) src.getSource(); 
      lblPlayerName2.setText(b.getText()); 


.  } 
... 
. 
. 
. 
. 
. 
. 
} // End of AllbtnBehvr 
}// End of Game Class 

코드 줄 것을 나는 관찰, 변수 'lblPlayerName2'와 lbls [] Game() 생성자 외부에서 정의되며 액세스 할 수 있지만 Game() 내부의 다른 변수는 액세스 할 수 없습니다. 여기에 묻고 싶습니다.

Game() 내 변수에 어떻게 액세스합니까? 및 게임 클래스 외부에서 클래스를 만들면 Game() 내 변수에 어떻게 액세스합니까?

답변

2

게임은 java.awt.Window를 확장하는 JFrame을 확장하므로 inherits its methods입니다.

추신. 수업은 대문자로 시작해야합니다. Game.

+0

예 JFrame은 확장되지만 생성자가 JFrame 객체를 구체적으로 반환하는 방법을 알았습니까? –

+0

이것은 생성자가 아닙니다 (실제로 무언가를 반환하지는 않습니다. 무효화 메소드입니다). _expression_ new game()은 JFrame 객체 인 게임 객체와 Window 객체를 반환합니다. 모든 Window 객체에는 setVisible이 있으므로 게임에 setVisible도 있습니다. – ignis

+0

이 개념을 닦을 수 있도록 몇 가지 참조를 줄 수 있습니까? 그럼 어떻게 다른 메소드 나 클래스의 game() 생성자 필드에 액세스 할 수 있습니까? ('game'클래스 또는 그 안에 선언되어 있습니까?) –

1

생성자 호출은 게임 객체를 반환하지만 게임은 JFrame에서 파생되었으므로 객체도 Jlrame, java.awt.Frame, java.awt.Window 등 java.lang에 있습니다. .목적.

하위 클래스는 상위 클래스에서 동작을 상속하므로 java.awt.Window에 정의 된 setVisible() 메서드는 Window에서 하위 클래스 인 JFrame을 통해 사용할 수 있습니다.

관련 문제