Frage

I'm trying to make a program with Java. It's Working with Jmenu and Jmenu item. My goal is to have a Jpanel for each Jmenu Item on external class. Netbeans have help me to make the basic of my menu... but I don't understand how I could use Jpanel from external class. I've searched for example, but I can't find ones which can help me or look likes my problem.

Thanks for any help.

The Jframe :

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class NewJFrame extends javax.swing.JFrame {


public NewJFrame() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenu2 = new javax.swing.JMenu();
    jMenuItem1 = new javax.swing.JMenuItem();
    jMenu3 = new javax.swing.JMenu();
    jMenuItem2 = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jMenu1.setText("Accueil");

    jMenuBar1.add(jMenu1);

    jMenu2.setText("Caisse");

    jMenuItem1.setText("Ouvrir Caisse");

    jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItem1ActionPerformed(evt);
        }
    });
    jMenu2.add(jMenuItem1);

    jMenuBar1.add(jMenu2);

    jMenu3.setText("Consultation");

    jMenuItem2.setText("Consulter BD");

    jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItem2ActionPerformed(evt);
        }
    });
    jMenu3.add(jMenuItem2);

    jMenuBar1.add(jMenu3);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 455, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 311, Short.MAX_VALUE)
    );

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

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) { 
//Jpanel "PanelCaisse"
}                                          

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
//Jpanel "PanelConsult"
}                                          

/**
 * @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
         java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private JMenu jMenu1;
private JMenu jMenu2;
private JMenu jMenu3;
private JMenuBar jMenuBar1;
private JMenuItem jMenuItem1;
private JMenuItem jMenuItem2;
// End of variables declaration                   

private static class label {

    private static void setText(String mode_Caisse) {
        throw new UnsupportedOperationException("Not supported yet."); 
    }

    public label() {
    }
  }
}

Panel "Caisse" (for test)

import java.awt.Graphics;
import javax.swing.JPanel;
public class PanelCaisse extends JPanel { 
  public void paintComponent(Graphics g){                
    int x1 = this.getWidth()/4;
    int y1 = this.getHeight()/4;                      
    g.fillOval(x1, y1, this.getWidth()/2, this.getHeight()/2);
  }       
}

Panel "Consult"

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PanelConsult extends javax.swing.JPanel {

private JTextField jTextField1;
private JButton jButton1;
private JLabel jLabel1;
private JPanel panelc = new JPanel();

public void ConsultBD() {
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();

jLabel1.setText("Votre recherche");
jButton1.setText("Ok");
jTextField1.setText("Recherche");

GroupLayout layout = new GroupLayout(panelc);
panelc.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

   layout.setHorizontalGroup(
       layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
       .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
           .addContainerGap()
           .addComponent(jLabel1)
           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
           .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
           .addComponent(jButton1)
           .addContainerGap(192, Short.MAX_VALUE))
   );
   layout.setVerticalGroup(
       layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
       .addGroup(layout.createSequentialGroup()
           .addContainerGap()
           .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(jLabel1)
               .addComponent(jButton1))
           .addContainerGap(277, Short.MAX_VALUE))           
   );
 }
}
War es hilfreich?

Lösung

This is functional demo which demonstrates how to apply CardLayout on your problem:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CardLayoutDemo {
    JFrame frame = new JFrame();
    JMenuBar menuBar = new JMenuBar();
    JMenu mnuPanels = new JMenu("Panels");
    JMenuItem mniFirstPanel = new JMenuItem("First panel");
    JMenuItem mniSecondPanel = new JMenuItem("Second panel");
    JPanel panel = new JPanel();
    JPanel pnlFirst = new JPanel();
    JPanel pnlSecond = new JPanel();
    CardLayout cardLayout = new CardLayout();

    public CardLayoutDemo() {
        panel.setLayout(cardLayout);
        pnlFirst.add(new JLabel("This is first panel"));
        pnlSecond.add(new JLabel("This is second panel"));
        //Add panels and set constraints.
        panel.add(pnlFirst, "pnlFirst");
        panel.add(pnlSecond, "pnlSecond");
        //Shows first layout on startup of application.
        /*Pass parent container (where you set CardLayout) and
        * constraint of panel you would like to show.
        * */
        cardLayout.show(panel, "pnlFirst");
        //Add menu and menu items.
        menuBar.add(mnuPanels);
        mnuPanels.add(mniFirstPanel);
        mnuPanels.add(mniSecondPanel);

        mniFirstPanel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Shows pnlFirst
                cardLayout.show(panel, "pnlFirst");
            }
        });

        mniSecondPanel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Shows pnlSecond
                cardLayout.show(panel, "pnlSecond");
            }
        });

        frame.setJMenuBar(menuBar);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(640, 480);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CardLayoutDemo();
            }
        });
    }

}

Idea to use CardLayout is perfect, but I can't tell you how you can achieve this with NetBeans GUI builder since I code it manualy. You should do same.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top