vedendo il tuo esempio ho provato a fare cosi ma non mi funziona lo stessoNon ho guardato quello che hai mandato, ti lascio un esempio che è simile al tuo caso.
Non utilizzo CardLayout, ma utilizzo - quasi - decentemente i Layout. Come noti un layout come il tuo non è complesso da realizzare, devi solo prendere dimestichezza con quelli che sono i Layout. Sono fondamentali se vuoi realizzare interfacce, ti conviene guardare il tutorial, ci sono esempi abbastanza chiari, con tanto di screenshot.
Quando pensi a un menu come il tuo, devi già immaginarti le sezioni, diciamo i "segmenti" che lo compongono. Tu hai di fatto 2 spazi, uno in alto e uno al centro, che copre tutto.
Quindi una cosa che puoi fare è usare 2 JPanel di base: uno che virrà posizionato a NORTH e uno CENTER. Quello a nord sarà il panel dedicato ai tuoi bottoni, quello centrato è il contenuto che cambia quando clicki sui bottoni.
Quando clicki un bottone andrai a rimuovere dal pannello contenitore, il panel che non ti serve, e aggiungi l'altro. Dovrai ovviamente fare un revalidate() sul pannello contenitore.
Codice scritto in una ventina di minuti, per farti l'esempio (non è sicuramente perfetto... non lo toccavo da anni, sono anche stupito nel ricordare ancora tutto senza consultare le doc!).
Come vedi puoi anche fare la verifica sull'evento in un modo diverso da quello che utilizzi tu.
Java:import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.SwingUtilities; class Example extends JFrame implements ActionListener { private JPanel panel1, // pannello in alto, menu bottoni panel2, // prima area visibile, centrale (azione su btn1) panel3, // seconda area visibile, centrale (azione su btn2) container; // contenitore di panel2/panel3 private JButton btn1, btn2; Example() { super("Panel Example"); // Contenuto "centrale" della finestra container = new JPanel(new BorderLayout()); // Contenitore dei bottoni del menu, in alto // ---------------------------------------- panel1 = new JPanel(); panel1.setBackground(Color.WHITE); btn1 = new JButton("Panel 1"); btn2 = new JButton("Panel 2"); btn1.addActionListener(this); btn2.addActionListener(this); panel1.add(btn1); panel1.add(btn2); // ---------------------------------------- // Label e pannelli, sono il contenuto JLabel lbl1 = new JLabel("PANEL 1"); panel2 = new JPanel(); panel2.setBackground(Color.CYAN); panel2.add(lbl1); JLabel lbl2 = new JLabel("PANEL 2"); panel3 = new JPanel(); panel3.setBackground(Color.ORANGE); panel3.add(lbl2); container.add(panel2); // ---------------------------------------- // Posizionamento dei pannelli add(panel1, BorderLayout.NORTH); add(container, BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent ae) { if(ae.getSource() == btn1) { container.remove(panel3); container.add(panel2); } else if(ae.getSource() == btn2) { container.remove(panel2); container.add(panel3); } container.revalidate(); container.repaint(); } public static void makeGUI() { Example e = new Example(); e.setSize(610,600); e.setResizable(false); e.setVisible(true); } public static void main(String[] args) { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { makeGUI(); } }); } catch(Exception e) {} } }
Java:
package food2;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
public class MENU4 extends JPanel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField txtCosaPreferisciMangiare;
private String Acquista_cmd = "Acquista";
private String ModificaProfilo_cmd = "Modifica Profilo";
private String Uscire_cmd = "Uscire";
private String Filtri_cmd = "Filtri";
Controller ctrl;
private JPanel panel5;
private JPanel panel_4,panel3,container;
private JButton btn1, btn2;
public MENU4(Controller ctrl) {
setBackground(new Color(255, 165, 0));
setBounds(100, 100, 747, 504);
setLayout(null);
this.ctrl=ctrl;
JPanel panel = new JPanel();
panel.setBackground(new Color(255, 140, 0));
panel.setForeground(new Color(255, 140, 0));
panel.setBounds(0, 0, 532, 41);
add(panel);
panel.setLayout(null);
JSeparator separator_Ricerca = new JSeparator();
separator_Ricerca.setForeground(Color.WHITE);
separator_Ricerca.setBounds(6, 23, 248, 12);
panel.add(separator_Ricerca);
txtCosaPreferisciMangiare = new JTextField();
txtCosaPreferisciMangiare.setText("Cosa preferisci mangiare?");
txtCosaPreferisciMangiare.setSelectionColor(Color.LIGHT_GRAY);
txtCosaPreferisciMangiare.setForeground(Color.BLACK);
txtCosaPreferisciMangiare.setFont(new Font("Times New Roman", Font.ITALIC, 14));
txtCosaPreferisciMangiare.setDisabledTextColor(Color.LIGHT_GRAY);
txtCosaPreferisciMangiare.setColumns(10);
txtCosaPreferisciMangiare.setBorder(null);
txtCosaPreferisciMangiare.setBackground(new Color(255, 140, 0));
txtCosaPreferisciMangiare.setBounds(6, 6, 248, 20);
panel.add(txtCosaPreferisciMangiare);
JButton btnCerca = new JButton("Cerca");
btnCerca.setForeground(new Color(255, 165, 0));
btnCerca.setFocusable(false);
btnCerca.setBounds(255, 4, 79, 25);
panel.add(btnCerca);
btnCerca.addActionListener(this);
JButton btnFiltri = new JButton("Filtri");
btnFiltri.setForeground(new Color(255, 165, 0));
btnFiltri.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
btnFiltri.setFocusable(false);
btnFiltri.setBounds(416, 2, 108, 25);
panel.add(btnFiltri);
btnFiltri.addActionListener(this);
btnFiltri.setActionCommand(Filtri_cmd);
JLabel lbl2 = new JLabel("PANEL 2");
panel5 = new JPanel();
panel5.setBackground(Color.ORANGE);
panel5.add(lbl2);
JPanel panel_1 = new JPanel();
panel_1.setBackground(new Color(255, 153, 51));
panel_1.setBounds(532, 0, 215, 504);
add(panel_1);
panel_1.setLayout(null);
JLabel username = new JLabel("Nickname Utente");
username.setBounds(6, 19, 138, 16);
panel_1.add(username);
JSeparator separator_1 = new JSeparator();
separator_1.setForeground(Color.WHITE);
separator_1.setBounds(0, 34, 215, 12);
panel_1.add(separator_1);
JSeparator separator_2 = new JSeparator();
separator_2.setForeground(Color.WHITE);
separator_2.setBounds(0, 436, 215, 12);
panel_1.add(separator_2);
JButton completeOrder = new JButton("Acquista");
completeOrder.setForeground(new Color(255, 165, 0));
completeOrder.setBounds(63, 446, 99, 29);
panel_1.add(completeOrder);
completeOrder.setName("Acquista");
completeOrder.addActionListener(this);
completeOrder.setActionCommand(Acquista_cmd);
JPanel panel1 = new JPanel();
panel1.setBackground(new Color(255, 165, 0));
panel1.setBounds(169, 3, 40, 32);
panel_1.add(panel1);
JMenuBar menuBar1 = new JMenuBar();
panel1.add(menuBar1);
JMenu Servizi = new JMenu("");
menuBar1.add(Servizi);
Servizi.addActionListener(this);
JMenuItem ModificaProfilo = new JMenuItem("Modifica Profilo");
Servizi.add(ModificaProfilo);
ModificaProfilo.addActionListener(this);
ModificaProfilo.setActionCommand(ModificaProfilo_cmd);
JMenuItem Uscire = new JMenuItem("Uscire");
Servizi.add(Uscire);
Uscire.addActionListener(this);
Uscire.setActionCommand(Uscire_cmd);
container = new JPanel(new BorderLayout());
panel1 = new JPanel();
panel1.setBackground(Color.WHITE);
btn1 = new JButton("Panel 1");
btn1.setBounds(174, 5, 89, 29);
btn2 = new JButton("Panel 2");
btn2.setBounds(268, 5, 89, 29);
btn1.addActionListener(this);
btn2.addActionListener(this);
JPanel panel_2 = new JPanel();
panel_2.setBounds(0, 40, 532, 464);
add(panel_2);
panel_2.setLayout(null);
panel_2.add(btn1);
panel_2.add(btn2);
JLabel lbl1 = new JLabel("PANEL 1");
lbl1.setBounds(217, 19, 61, 16);
JPanel panel_4 = new JPanel();
panel_4.setBounds(6, 46, 520, 412);
panel_2.add(panel_4);
panel_4.setLayout(null);
panel_4.add(lbl1);
JLabel lbl21 = new JLabel("PANEL 2");
lbl21.setBounds(0, 0, 61, 16);
JPanel panel3 = new JPanel();
panel3.setBounds(6, 46, 520, 412);
panel_2.add(panel3);
panel3.setLayout(null);
panel3.add(lbl21);
container.add(panel_4);
add(panel1, BorderLayout.NORTH);
add(container, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == btn1) {
container.remove(panel3);
container.add(panel_4);
}
else if(ae.getSource() == btn2) {
container.remove(panel_4);
container.add(panel3);
}
container.revalidate();
container.repaint();
}
}
Ultima modifica: