Olá Pessoal, estou postando aki, pois estou com um problema acho que meio besta, tenho um JFrame Principal e tenho vários JPanel que gostaria de add neste frame principal na medida em que os usuarios derem os eventos, oq não entendi foi que criei a classe demo para add o JPanel no meu JFrame principal so que ele não add meu JPanel no Frame, ele executa mas so mostra o Jframe vazio. Alguem tem ideia de como posso resolver isso. Obrigada
JFame e JPanel
6 Respostas
Poste seu código, se nao fica dificil.
Mas uma razão é que, em geral, quando vc quer adicionar panels numa janela, vc tem que fazer assim: seuFrame.getContentPane().add(painelQueVcDesejaAdicionar). Se vc utilizar direto o método add do seu frame, provavelmente nao vai funfar…
Como vc está adicionando o panel?
Qual layout vc usa?
Coloque um JDesktopPane no seu JFrame, e adicione os JPanels nele.
JPanel’s dentro de JInternalFrame’s daí dentro do JDesktopPane, certo?
oq vou colocar aki é so um exemplo para ficar mais facil de entender, o FrameP é o frame principal e o JPanel 1 e Jpanel2 são os panels que vao ser add no Frame apos a chamada dos eventos dos botões Panel1 ou Panel2 o meu problema é so isso, a questão de dimensionar os Jpanel ta tranquilo. Obrigada
*/
public class FrameP extends javax.swing.JFrame {
/** Creates new form FrameP */
public FrameP() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButtonp1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Frame Principal");
jButtonp1.setText("Panel 1");
jButtonp1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonp1ActionPerformed(evt);
}
});
jButton2.setText("Panel 2");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jButtonp1)
.add(jButton2))
.addContainerGap(569, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(33, 33, 33)
.add(jButtonp1)
.add(18, 18, 18)
.add(jButton2)
.addContainerGap(312, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButtonp1ActionPerformed(java.awt.event.ActionEvent evt) {
new JPanel1().show();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrameP().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton2;
private javax.swing.JButton jButtonp1;
// End of variables declaration
}
segue outra classe
public class JPanel1 extends javax.swing.JPanel {
/** Creates new form JPane1l */
public JPanel1() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jComboBoxP1 = new javax.swing.JComboBox();
jButton1 = new javax.swing.JButton();
jComboBoxP1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jButton1.setText("OK1");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(51, 51, 51)
.add(jComboBoxP1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 99, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 54, Short.MAX_VALUE)
.add(jButton1)
.add(141, 141, 141))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(79, 79, 79)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jButton1)
.add(jComboBoxP1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(198, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBoxP1;
// End of variables declaration
}
public class JPanel2 extends javax.swing.JPanel {
/** Creates new form JPane2l */
public JPanel2() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jComboBoxP2 = new javax.swing.JComboBox();
jButtonOk = new javax.swing.JButton();
setLayout(null);
jComboBoxP2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
add(jComboBoxP2);
jComboBoxP2.setBounds(50, 50, 140, 22);
jButtonOk.setText("OK2");
add(jButtonOk);
jButtonOk.setBounds(210, 50, 55, 23);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JButton jButtonOk;
private javax.swing.JComboBox jComboBoxP2;
// End of variables declaration
}
Poste seu código, se nao fica dificil.Mas uma razão é que, em geral, quando vc quer adicionar panels numa janela, vc tem que fazer assim:
seuFrame.getContentPane().add(painelQueVcDesejaAdicionar). Se vc utilizar direto o método add do seu frame, provavelmente nao vai funfar…
Valeu pela dica mas tentei fazer e naum deu certo, ja postei o codigo lá para ficar mais fácil.
Valew!!!