3 JFrame中的Jpanel,我的按钮不可见

3 Jpanel in JFrame, my button is not visible

我有一个 JFrame 和三个 JPanel 对象。我有两个面板有问题。我可以在框架中看到带有对象的面板 JPanelProduit,但对于面板 JPanelInformationsJPanelVentes,我什么也看不到。我的错误在哪里?

我的代码

package IHM;

import javax.swing.*;

import Donnees.Categories;
import Donnees.CategoriesCellRenderer;
import Donnees.CategoriesListModel;
import Donnees.Marques;
import Donnees.MarquesCellRenderer;
import Donnees.MarquesListModel;
import Donnees.Produits;
import Donnees.ProduitsCellRenderer;
import Donnees.ProduitsListModel;
import Fabriques.FabCategories;
import Fabriques.FabMarques;

import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
public class Fenetre {
    
static Connection conn;

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("org.hsqldb.jdbcDriver");
        conn=DriverManager.getConnection("jdbc:hsqldb:file:BDD/bdd","sa","");

        FabCategories.getInstance().demarrerConnexion(conn);
        FabMarques.getInstance().demarrerConnexion(conn);
        
        JFrame f = new JFrame("Gestion des Produits");
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(1,2,3, 3));
 
        JPanelProduit jPanelProduit = new JPanelProduit();
        JPanelInformations jPanelInformations = new JPanelInformations();
        JPanelVentes jPanelVentes = new JPanelVentes();
        
        jPanelProduit.setBackground(Color.GREEN);
        jPanelProduit.setBackground(Color.YELLOW);
        jPanelVentes.setBackground(Color.PINK);
        
        f.add(jPanelProduit);
        f.add(jPanelInformations);
        f.add(jPanelVentes);
        f.setSize(700,700);
        f.pack();
        f.setVisible(true);
    }
}
 
class JPanelProduit extends JPanel {
     
    public JPanelProduit() throws SQLException {
        setLayout(new GridLayout(5,2,5,5));
        
        String labelCat = "Categories";
        String labelMark = "Marques";    
        String labelProd = "Produits"; 
        
        JList<Categories> listCategories= new JList<Categories> ();
        JList<Marques> listMarques= new JList<Marques> ();
        JList<Produits> listProduits= new JList<Produits> ();
        
        JScrollPane listCategoriesScrollPane = new JScrollPane (listCategories);        
        
        add(new JLabel(labelCat));
        add(new JScrollPane(listCategoriesScrollPane));
        listCategories.setCellRenderer(new CategoriesCellRenderer());;
        listCategories.setModel(new CategoriesListModel());
        
        add(new JLabel(labelMark));
        JScrollPane listMarquesScrollPane = new JScrollPane (listMarques);        
        add(new JScrollPane(listMarquesScrollPane));
        listMarques.setCellRenderer(new MarquesCellRenderer());
        listMarques.setModel(new MarquesListModel());
        
        add(new JLabel(labelProd));
        JScrollPane listProduitScrollPane = new JScrollPane (listProduits);        
        add(new JScrollPane(listProduitScrollPane));
        //listProduits.setCellRenderer(new ProduitsCellRenderer());
        //listProduits.setModel(new ProduitsListModel());
        
    }
}
 
class JPanelInformations extends JPanel {
  
    public JPanelInformations() {
        JPanel PanelInformation = new JPanel();
        setLayout(new GridLayout(7,1,5,5)); 
        
        JLabel labelInfo = new JLabel ("INFORMATION");
        JLabel labelPrix = new JLabel ("Prix");
        JLabel labelDesc = new JLabel ("Description");    
        JLabel labelQuant = new JLabel ("Quantite");    
        JTextField fieldPrix = new JTextField (20); 
        JTextArea fieldDesc = new JTextArea (20, 20); 
        JTextField fieldQuantite = new JTextField (20); 
            
        PanelInformation.add(labelInfo);
        PanelInformation.add(labelPrix);
        PanelInformation.add(fieldPrix);
        PanelInformation.add(labelDesc);
        PanelInformation.add(fieldDesc);
        PanelInformation.add(labelQuant);
        PanelInformation.add(fieldQuantite);
    }
}

class JPanelVentes extends JPanel {
         
    public JPanelVentes() {
        JPanel PanelVentes = new JPanel();
        setLayout(new GridLayout());
        JLabel labelVendre = new JLabel ("VENDRE");
        JLabel labelQte = new JLabel ("Quantite");
        JLabel labelPromo = new JLabel ("Promotion");
        JLabel labelTot = new JLabel ("Total");    
        JTextField fieldQte = new JTextField (20); 
        JTextField fieldPromoEuros = new JTextField (20); 
        JTextField fieldPromoPourcent = new JTextField (20); 
        JTextField fieldTotal = new JTextField (20); 
        
        
        PanelVentes.add (labelVendre);
        PanelVentes.add (labelQte);
        PanelVentes.add (fieldQte);
        PanelVentes.add (labelPromo);
        PanelVentes.add (fieldPromoEuros);
        PanelVentes.add (fieldPromoPourcent);
        PanelVentes.add (labelTot);
        PanelVentes.add (fieldTotal);
    }
}

构造函数中的 JPanelInformations 创建了一个本地实例 JPanel PanelInformation = new JPanel();,它没有添加到主面板。

您应该将其添加到 this 或完全删除它并直接将所有标签添加到 this

与 JPanelVentes 相同

JPanelInformations class 中,您正在创建新的 JPanel class PanelInformation 并在其中添加其他元素。您应该只调用 add(),因为 JPanelInformations 已经扩展了 JPanel,并且您正在创建 JPanelVentes class 的实例。

因此请遵循您在 JPanelProduit class.

中使用的相同 JPanel 逻辑

JPanelVentes也是如此。

还要注意命名约定。让生活变得轻松

JPanel panelVentes = new JPanel();

试试这个:

public JPanelInformations() {
    //JPanel PanelInformation = new JPanel(); remove new instance of panel
    setLayout(new GridLayout(7,1,5,5)); 

    JLabel labelInfo = new JLabel ("INFORMATION");
    JLabel labelPrix = new JLabel ("Prix");
    JLabel labelDesc = new JLabel ("Description");    
    JLabel labelQuant = new JLabel ("Quantite");    
    JTextField fieldPrix = new JTextField (20); 
    JTextArea fieldDesc = new JTextArea (20, 20); 
    JTextField fieldQuantite = new JTextField (20); 

    add(labelInfo); //remove PanelInformation.
    add(labelPrix);//remove PanelInformation.
    add(fieldPrix);//remove PanelInformation.
    add(labelDesc);//remove PanelInformation.
    add(fieldDesc);//remove PanelInformation.
    add(labelQuant);//remove PanelInformation.
    add(fieldQuantite);//remove PanelInformation.
}

public JPanelVentes() {
    //JPanel PanelVentes = new JPanel(); remove the new instance of JPanel
    setLayout(new GridLayout());
    JLabel labelVendre = new JLabel ("VENDRE");
    JLabel labelQte = new JLabel ("Quantite");
    JLabel labelPromo = new JLabel ("Promotion");
    JLabel labelTot = new JLabel ("Total");    
    JTextField fieldQte = new JTextField (20); 
    JTextField fieldPromoEuros = new JTextField (20); 
    JTextField fieldPromoPourcent = new JTextField (20); 
    JTextField fieldTotal = new JTextField (20); 


    add (labelVendre); //remove PanelVentes
    add (labelQte);//remove PanelVentes
    add (fieldQte);//remove PanelVentes
    add (labelPromo);//remove PanelVentes
    add (fieldPromoEuros);//remove PanelVentes
    add (fieldPromoPourcent);//remove PanelVentes
    add (labelTot);//remove PanelVentes
    add (fieldTotal);//remove PanelVentes
}