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

标签 java swing jframe jpanel

我有一个带有三个 JPanel 对象的 JFrame 。我的两个面板有问题。我可以在我的框架中看到带有对象的面板 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 相同

关于java - 3 JFrame中的Jpanel,我的按钮不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29864215/

相关文章:

java - 无法更改java中的itext图像边框

java - 简单的编程理论问题

java - 老定时器不停歇

java - 如何确定 JAVA 中 jdbcTemplate 的 sql 查询类型?

java - 在 Swing 中使用 GridLayout 时设置框架大小

java - 向 JFrame 添加 2 个或更多对象

java - 定位模态 jframe

java - 如何更改 Java Swing TextArea 中的突出显示颜色?并且,更改与突出显示位置对应的文本开头

java - 如何检测两个鼠标按钮是否同时松开?

java - 定时器在满足条件时给出错误