2016-11-17 5 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のサブクラスではありません。

関連する問題