Subclass 不继承父 class 字段

Subclass not inheriting parent class fields

我有一个子class,但问题是由于某种原因,它没有继承主要的class字段。我试过将 public 设为私有(尽管你应该能够从 subclasses 访问私有字段)但即使这样也没有用。

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

您将这些字段标记为私有,因此它们不能被继承。

此外,这些 "subclasses" 不会延长您的 Fenetre class。请注意。

应该是:

class BoutonListener extends Fenetre implements ActionListener 

而不是:

class BoutonListener implements ActionListener 

在这两种情况下。

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

您的 'subclasses' 不扩展任何超类。它们不是 Fenetre.

的子类