Swing cardlayout - przełączanie jPanel

0

Witajcie piszę swoją pierwszą aplikację okienkową gdzie muszę przełączyć content strony użyłem do tego cardlayout ( jako konstruktora w JFrame). Wszystko fajnie chodzi ale jak by nie było ale to bym tutaj nie prosił was o pomoc. Mianowicie jak przełcze się na inny jPanel to nie ładuje mi jego elementów tylko dopiero jak kliknę w miejsce gdzie on powinien być. Może kotoś podpowie mi czemu tak się dzieje ??


import java.awt.;
import java.awt.event.
;
import javax.swing.;
import javax.swing.table.DefaultTableModel;
/

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */

/**
*

  • @Author Michał
    */
    public class main extends javax.swing.JFrame {

    /**

    • Creates new form main
      */
      public main() {
      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() {

      jPanel1 = new javax.swing.JPanel();
      jButton1 = new javax.swing.JButton();
      jScrollPane2 = new javax.swing.JScrollPane();
      jTable2 = new javax.swing.JTable();
      jPanel2 = new javax.swing.JPanel();
      jScrollPane1 = new javax.swing.JScrollPane();
      jTextPane1 = new javax.swing.JTextPane();
      jButton2 = new javax.swing.JButton();
      jMenuBar1 = new javax.swing.JMenuBar();
      jMenu1 = new javax.swing.JMenu();
      jMenu2 = new javax.swing.JMenu();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      getContentPane().setLayout(new java.awt.CardLayout());

      jButton1.setText("jButton1");
      jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
      jButton1MouseClicked(evt);
      }
      public void mousePressed(java.awt.event.MouseEvent evt) {
      jButton1MousePressed(evt);
      }
      });

      jTable2.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {
      {null, null, null, null},
      {null, null, null, null},
      {null, null, null, null},
      {null, null, null, null}
      },
      new String [] {
      "Title 1", "Title 2", "Title 3", "Title 4"
      }
      ));
      jScrollPane2.setViewportView(jTable2);

      javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
      jPanel1.setLayout(jPanel1Layout);
      jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
      .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
      .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addContainerGap())
      );
      jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
      .addContainerGap()
      .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      .addGroup(jPanel1Layout.createSequentialGroup()
      .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addGap(0, 11, Short.MAX_VALUE))
      );

      getContentPane().add(jPanel1, "card2");

      jTextPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
      jTextPane1.setDisabledTextColor(new java.awt.Color(255, 255, 255));
      jScrollPane1.setViewportView(jTextPane1);

      jButton2.setText("jButton2");

      javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
      jPanel2.setLayout(jPanel2Layout);
      jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
      .addGap(58, 58, 58)
      .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jButton2)
      .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE))
      .addContainerGap(257, Short.MAX_VALUE))
      );
      jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
      .addGap(31, 31, 31)
      .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      .addComponent(jButton2)
      .addContainerGap(74, Short.MAX_VALUE))
      );

      getContentPane().add(jPanel2, "card3");

      jMenu1.setText("File");
      jMenuBar1.add(jMenu1);

      jMenu2.setText("Edit");
      jMenuBar1.add(jMenu2);

      setJMenuBar(jMenuBar1);

      pack();
      }// </editor-fold>

    private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
    jPanel1.removeAll();
    jPanel2.setVisible(true);
    ourLayout.
    // getContentPane().add(jPanel2, "card3");
    }

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {

    }

    /**

    • @param args the command line arguments
      /
      public static void main(String args[]) {
      /
      Set the Nimbus look and feel /
      //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
      /
      If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

      • For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
        */
        try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
        javax.swing.UIManager.setLookAndFeel(info.getClassName());
        break;
        }
        }
        } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

      /* Create and display the form */
      java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
      new main().setVisible(true);
      }
      });
      }
      // Variables declaration - do not modify
      private javax.swing.JButton jButton1;
      private javax.swing.JButton jButton2;
      private javax.swing.JMenu jMenu1;
      private javax.swing.JMenu jMenu2;
      private javax.swing.JMenuBar jMenuBar1;
      private javax.swing.JPanel jPanel1;
      private javax.swing.JPanel jPanel2;
      private javax.swing.JScrollPane jScrollPane1;
      private javax.swing.JScrollPane jScrollPane2;
      private javax.swing.JTable jTable2;
      private javax.swing.JTextPane jTextPane1;
      // End of variables declaration
      }


1

Jeżeli masz cardLayput to nie baw się w żadne setVisible() na panelach, tylko zrób to tak jak należy:

CardLayout cl = (CardLayout)(getContentPane().getLayout());
cl.show(getContentPane(), "card3");

1 użytkowników online, w tym zalogowanych: 0, gości: 1