我有JFrame
个,有三个JPanel
个对象。我有两个面板有问题。我可以在我的框架中看到面板JPanelProduit
包含对象,但对于面板JPanelInformations
和JPanelVentes
,我什么也看不见。我的错误在哪里?
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);
}
}
答案 0 :(得分:4)
构造函数中的JPanelInformations创建一个未添加到主面板的本地实例JPanel PanelInformation = new JPanel();
。
您应该将其添加到this
或完全取消它,并将所有标签直接添加到this
。
与JPanelVentes相同
答案 1 :(得分:1)
在JPanelInformations
课程内,您正在创建新的JPanel类PanelInformation
并在其中添加其他元素。您应该调用add()
,因为JPanelInformations
已经扩展了JPanel并且您正在创建JPanelVentes
类的实例。
请遵循您在JPanelProduit
类中使用的相同JPanel逻辑。
同样适用于JPanelVentes
。
还要注意命名约定。让生活变得轻松
JPanel panelVentes = new JPanel();
答案 2 :(得分:0)
试试这个:
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
}