Class 使用 Swing 扩展不工作

Class extend with Swing not working

我从 java 开始,我正在尝试创建一个带有按钮的 window (class Fenetre) 并在其中添加图片和一些文本 (class AccueilPanel) 但是当我构建它时,只显示 Window+ 按钮。

AccueilPanel class 似乎不起作用。这是我的代码:

Class Fenetre:

package test.accueil;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;


public class Fenetre extends JFrame{    


  public Fenetre(){

      JPanel pan = new JPanel();
        //Définition de sa couleur de fond
        pan.setBackground(Color.YELLOW);        
        //On prévient notre JFrame que notre JPanel sera son content pane
        this.setContentPane(pan);               
        this.setVisible(true);  
    this.setTitle("Box Layout");
    this.setSize(900,900);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);



    JPanel b1 = new JPanel();
    //On définit le layout en lui indiquant qu'il travaillera en ligne
    b1.setLayout(new BoxLayout(b1, BoxLayout.LINE_AXIS));
    b1.add(new JButton("Jouer !"));
    b1.setBackground(Color.YELLOW); 


    JPanel b2 = new JPanel();
    b2.setLayout(new BoxLayout(b2, BoxLayout.LINE_AXIS));
    b2.add(new JButton("Instructions"));
    b2.add(new JButton("Scores"));
    b2.setBackground(Color.YELLOW); 



    JPanel b4 = new JPanel();
    //On positionne maintenant ces trois lignes en colonne
    b4.setLayout(new BoxLayout(b4, BoxLayout.PAGE_AXIS));
    b4.add(b1);
    b4.add(b2);
    b4.setBackground(Color.YELLOW);


    this.getContentPane().add(b4);
    this.setVisible(true);    


  }

  protected JPanel getPanel(){
        return this.b4;
    }   
    protected JPanel b4;    


    public static void main(String[] args) {    
    Fenetre fenetre = new Fenetre();

    }

}

Class AccueilPanel:

package test.accueil;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class AccueilPanel extends Fenetre{

    public AccueilPanel(Dimension dim){
        initPanel();
    }

    public void initPanel(){
        JLabel titre = new JLabel("Bienvenue dans le jeu du pendu\n");
        titre.setHorizontalAlignment(JLabel.CENTER);
        this.b4.add(titre, BorderLayout.NORTH);

        this.b4.add(new JLabel(new ImageIcon("images/moon.jpg")), BorderLayout.CENTER);

        JTextArea texte = new JTextArea("Bienvenue dans mon jeu ! ");

        texte.setEditable(false);
        texte.setBackground(Color.white);

        this.b4.add(texte, BorderLayout.SOUTH);
    }

}

有人可以帮助我吗?

如果两个 类' package 是显式的,则您的代码可以进行子类化的唯一方法。因此,在两个文件的第一行添加 say package maison; 并且 运行time 应该能够毫无问题地访问两个 类。

好的,我让你的代码可以工作了,我正在粘贴这两个文件:

package test.accueil;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class AccueilPanel extends Fenetre{

    public AccueilPanel(Dimension dim){
        super();
        initPanel();
    }

    public void initPanel(){
        JLabel titre = new JLabel("Bienvenue dans le jeu du pendu\n");
        titre.setHorizontalAlignment(JLabel.CENTER);
        b4.add(titre, BorderLayout.NORTH);

        b4.add(new JLabel(new ImageIcon("images/moon.jpg")), BorderLayout.CENTER);

        JTextArea texte = new JTextArea("Bienvenue dans mon jeu ! ");

        texte.setEditable(false);
        texte.setBackground(Color.white);

        b4.add(texte, BorderLayout.SOUTH);
    }

}
package test.accueil;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;


public class Fenetre extends JFrame{    


  public Fenetre(){

      JPanel pan = new JPanel();
        //Définition de sa couleur de fond
        pan.setBackground(Color.YELLOW);        
        //On prévient notre JFrame que notre JPanel sera son content pane
        this.setContentPane(pan);               
        this.setVisible(true);  
    this.setTitle("Box Layout");
    this.setSize(900,900);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);



    JPanel b1 = new JPanel();
    //On définit le layout en lui indiquant qu'il travaillera en ligne
    b1.setLayout(new BoxLayout(b1, BoxLayout.LINE_AXIS));
    b1.add(new JButton("Jouer !"));
    b1.setBackground(Color.YELLOW); 


    JPanel b2 = new JPanel();
    b2.setLayout(new BoxLayout(b2, BoxLayout.LINE_AXIS));
    b2.add(new JButton("Instructions"));
    b2.add(new JButton("Scores"));
    b2.setBackground(Color.YELLOW); 



    JPanel b4 = new JPanel();
    //On positionne maintenant ces trois lignes en colonne
    b4.setLayout(new BoxLayout(b4, BoxLayout.PAGE_AXIS));
    b4.add(b1);
    b4.add(b2);
    b4.setBackground(Color.YELLOW);


    this.getContentPane().add(b4);
    this.setVisible(true);    


  }

  protected JPanel getPanel(){
        return this.b4;
    }   
    protected JPanel b4;    


    public static void main(String[] args) {    
    Fenetre fenetre = new Fenetre();
    }
}

...和我用来编译它的命令行javac -d . Fenetre.java AccueilPanel.java

到运行吧,我用的是java -cp . test.accueil.AccueilPanel。希望对您有所帮助。

您的代码中没有任何地方实际创建了 AccueilPanel class 的实例并将其设置为可见。 (因为它是 JFrame)

您需要在适当的地方添加如下代码,这样就可以创建并显示

AccueilPanel accPanel = new AccueilPanel(new Dimension(100,100));
accPanel.setVisible(true);

但是,class 的命名表明您希望此 class 成为一个面板而不是新框架,也许您应该考虑将其扩展 JPanel