2016-11-17 3 views
-1

하위 클래스가 있지만 문제가있는 이유는 기본 클래스의 필드를 상속하지 않기 때문입니다. 개인 (비록 당신이 어쨌든 서브 클래스에서 개인 필드에 액세스 할 수 있어야한다고하더라도) 개인 대신 공개를 시도했지만 작동하지 않았다.하위 클래스가 상위 클래스 필드를 상속하지 않음

package com.testfoler; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class Fenetre extends JFrame { 

    private Panneau pan = new Panneau(); 
    public JButton bouton = new JButton("Go"); 
    public JButton bouton2 = new JButton("Stop"); 
    private JPanel container = new JPanel(); 
    private JLabel label = new JLabel("Le JLabel"); 

    public Fenetre() { 
     this.setTitle("Animation"); 
     this.setSize(300, 300); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setLocationRelativeTo(null); 

     container.setBackground(Color.white); 
     container.setLayout(new BorderLayout()); 
     container.add(pan, BorderLayout.CENTER); 
     bouton.addActionListener(new BoutonListener()); 
     bouton.setEnabled(false); 
     bouton2.addActionListener(new Bouton2Listener()); 

     JPanel south = new JPanel(); 
     south.add(bouton); 
     south.add(bouton2); 
     container.add(south, BorderLayout.SOUTH); 
     Font police = new Font("Tahoma", Font.BOLD, 16); 
     label.setFont(police); 
     label.setForeground(Color.blue); 
     label.setHorizontalAlignment(JLabel.CENTER); 
     container.add(label, BorderLayout.NORTH); 
     this.setContentPane(container); 
     this.setVisible(true); 
    } 
} 

// Those are the subclasses 
class BoutonListener implements ActionListener { 
    public void actionPerformed(ActionEvent arg0) { 
     bouton.setEnabled(false); 
     bouton2.setEnabled(true); 
    } 
} 

class Bouton2Listener implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     bouton.setEnabled(true); 
     bouton2.setEnabled(false); 
    } 
} 
+0

하위 클래스 *는 수퍼 클래스의 private 멤버에 액세스 할 수 없습니다. – cybersam

+0

아니요, 그렇지 않습니다. 나는 그것들을 공개하기도했다. – Zerox029

+0

* 서브 클래스 * 및 * 수퍼 클래스 * 코드를 게시하는 것이 좋습니다. –

답변

1

필드를 비공개로 표시 했으므로 상속 될 수 없습니다.

또한 이러한 '하위 클래스'는 Fenetre 클래스를 확장하지 않습니다. 주의를 기울이십시오.

은 다음과 같아야합니다

class BoutonListener extends Fenetre implements ActionListener 

대신 :

class BoutonListener implements ActionListener 

두 경우 모두있다.

+0

아니요, 그렇지 않습니다. 나는 그것들을 공개하기도했다. – Zerox029

+0

@ Zerox029 내 대답을 편집했습니다. – null

1
//Those are the subclasses 
class BoutonListener implements ActionListener{ 
    public void actionPerformed(ActionEvent arg0) { 
     bouton.setEnabled(false); 
     bouton2.setEnabled(true); 
    } 
} 

'하위 클래스'는 수퍼 클래스를 확장하지 않습니다. 서브 클래스가 Fenetre이 아닙니다.