Buongiorno sto facendo un progetto dove ho il menu orizzontale con bottoni e quando clicco devo poter cambiare i vari panel...ho provato cosi ma non mi funziona sapete aiutarmi a trovare dove sta il problema?
package GuiFood;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.JSeparator;
import javax.swing.JButton;
import Classi.Controller;
import javax.swing.JToolBar;
import javax.swing.JMenuBar;
import java.awt.CardLayout;
import java.awt.SystemColor;
public class Menu extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
protected Object frame1;
private JTextField txtCosaPreferisciMangiare;
private String Acquista_cmd = "Acquista";
private String ModificaProfilo_cmd = "Modifica Profilo";
private String Uscire_cmd = "Uscire";
private String Filtri_cmd = "Filtri";
private String Antipasti_cmd = "Antipasti";
private String primi_cmd = "primi";
private String secondi_cmd = "secondi";
private String contorni_cmd = "contorni";
private String dolci_cmd = "dolci";
private String bevande_cmd = "bevande";
Controller ctrl;
private Container panel_3;
private Component panel_Antipasti;
private Component panel_5;
private Component panel_6;
private Component panel_7;
private Component panel_8;
private Component panel_9;
private Component panel_10;
public Menu(Controller ctrl) {
setTitle("Login");
this.getContentPane().setBackground(new Color(255, 165, 0));
this.setResizable(false);
this.setBounds(100, 100, 747, 504);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().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);
getContentPane().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.setIcon(scaleImageIcon(new ImageIcon(Menu.class.getResource("/Menu./ricerca.png")), 10, 10));
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.setIcon(scaleImageIcon(new ImageIcon(Menu.class.getResource("/Menu./filtri.png")), 21, 21));
btnFiltri.setFocusable(false);
btnFiltri.setBounds(416, 2, 108, 25);
panel.add(btnFiltri);
btnFiltri.addActionListener(this);
btnFiltri.setActionCommand(Filtri_cmd);
JPanel panel_2 = new JPanel();
panel_2.setBackground(new Color(255, 165, 0));
panel_2.setBounds(0, 41, 532, 58);
getContentPane().add(panel_2);
panel_2.setLayout(null);
JToolBar menuBar = new JToolBar();
menuBar.setBackground(new Color(255, 165, 0));
menuBar.setBounds(0, 0, 518, 58);
panel_2.add(menuBar);
JButton antipasti = new JButton("Anitpasti");
antipasti.setForeground(new Color(255, 165, 0));
antipasti.setBackground(new Color(250, 240, 230));
antipasti.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
menuBar.add(antipasti);
antipasti.setFocusable(false);
antipasti.setIcon(scaleImageIcon(new ImageIcon(Menu.class.getResource("/Menu./antipasti.png")), 32, 32));
antipasti.setPreferredSize(new Dimension(100,40));
antipasti.addActionListener(this);
antipasti.setActionCommand(Antipasti_cmd);
JButton primi = new JButton("primi");
primi.setForeground(new Color(255, 165, 0));
primi.setBackground(Color.WHITE);
primi.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
menuBar.add(primi);
primi.setFocusable(false);
primi.setIcon(scaleImageIcon(new ImageIcon(Menu.class.getResource("/Menu./primi.png")), 32, 32));
primi.setPreferredSize(new Dimension(100,40));
primi.addActionListener(this);
primi.setActionCommand(primi_cmd);
JButton secondi = new JButton("secondi");
secondi.setForeground(new Color(255, 165, 0));
secondi.setBackground(Color.WHITE);
secondi.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
menuBar.add(secondi);
secondi.setFocusable(false);
secondi.setIcon(scaleImageIcon(new ImageIcon(Menu.class.getResource("/Menu./secondi.png")), 32, 32));
secondi.setPreferredSize(new Dimension(100,40));
secondi.addActionListener(this);
secondi.setActionCommand(secondi_cmd);
JButton contorni = new JButton("contorni");
contorni.setForeground(new Color(255, 165, 0));
contorni.setBackground(Color.WHITE);
contorni.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
menuBar.add(contorni);
contorni.setFocusable(false);
contorni.setIcon(scaleImageIcon(new ImageIcon(Menu.class.getResource("/Menu./contorni.png")), 32, 32));
contorni.addActionListener(this);
secondi.setActionCommand(contorni_cmd);
JButton dolci = new JButton("dolci");
dolci.setForeground(new Color(255, 165, 0));
dolci.setBackground(Color.WHITE);
dolci.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
menuBar.add(dolci);
dolci.setFocusable(false);
dolci.setIcon(scaleImageIcon(new ImageIcon(Menu.class.getResource("/Menu./dolci.png")), 32, 32));
dolci.setPreferredSize(new Dimension(100,40));
dolci.addActionListener(this);
secondi.setActionCommand(dolci_cmd);
JButton bevande = new JButton("bevande");
bevande.setForeground(new Color(255, 165, 0));
bevande.setBackground(Color.WHITE);
bevande.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
menuBar.add(bevande);
bevande.setFocusable(false);
bevande.setIcon(scaleImageIcon(new ImageIcon(Menu.class.getResource("/Menu./bevande.png")), 32, 32));
bevande.setPreferredSize(new Dimension(100,40));
bevande.addActionListener(this);
secondi.setActionCommand(bevande_cmd);
JPanel panel_3 = new JPanel();
panel_3.setBackground(new Color(255, 140, 0));
panel_3.setBounds(0, 99, 532, 382);
getContentPane().add(panel_3);
panel_3.setLayout(new CardLayout(0, 0));
JPanel panel_Antipasti = new JPanel();
panel_Antipasti.setBackground(Color.MAGENTA);
panel_3.add(panel_Antipasti, "name_38699264993790");
panel_Antipasti.setLayout(new CardLayout(0, 0));
JPanel panel_5 = new JPanel();
panel_5.setBackground(Color.DARK_GRAY);
panel_3.add(panel_5, "name_38699279120459");
panel_5.setLayout(new CardLayout(0, 0));
JPanel panel_6 = new JPanel();
panel_6.setBackground(Color.BLUE);
panel_3.add(panel_6, "name_38699292200553");
panel_6.setLayout(new CardLayout(0, 0));
JPanel panel_7 = new JPanel();
panel_7.setBackground(Color.RED);
panel_3.add(panel_7, "name_38699305254308");
panel_7.setLayout(new CardLayout(0, 0));
JPanel panel_8 = new JPanel();
panel_8.setBackground(Color.GRAY);
panel_3.add(panel_8, "name_38699318684918");
panel_8.setLayout(new CardLayout(0, 0));
JPanel panel_9 = new JPanel();
panel_9.setBackground(SystemColor.controlHighlight);
panel_3.add(panel_9, "name_38699331815907");
panel_9.setLayout(new CardLayout(0, 0));
JPanel panel_10 = new JPanel();
panel_3.add(panel_10, "name_38738228823090");
panel_10.setLayout(new CardLayout(0, 0));
JPanel panel_1 = new JPanel();
panel_1.setBackground(new Color(255, 165, 0));
panel_1.setBounds(532, 0, 215, 481);
getContentPane().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);
completeOrder.setIcon(scaleImageIcon(new ImageIcon(Menu.class.getResource("/Menu./carrello.png")), 25, 25));
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.setIcon(scaleImageIcon(new ImageIcon(Menu.class.getResource("/Menu./Menu.png")), 18, 18));
Servizi.addActionListener(this);
JMenuItem ModificaProfilo = new JMenuItem("Modifica Profilo");
ModificaProfilo.setIcon(scaleImageIcon(new ImageIcon(Menu.class.getResource("/Menu./Modifica.png")), 20, 20));
Servizi.add(ModificaProfilo);
ModificaProfilo.addActionListener(this);
ModificaProfilo.setActionCommand(ModificaProfilo_cmd);
JMenuItem Uscire = new JMenuItem("Uscire");
Servizi.add(Uscire);
Uscire.setIcon(scaleImageIcon(new ImageIcon(Menu.class.getResource("/Menu./Uscire.png")), 25, 25));
Uscire.addActionListener(this);
Uscire.setActionCommand(Uscire_cmd);
}
static ImageIcon scaleImageIcon(ImageIcon image, int width, int height) {
Image tmp = image.getImage();
Image scaledImage = tmp.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
ImageIcon scaledImageIcon = new ImageIcon(scaledImage);
return scaledImageIcon;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(Filtri_cmd))
{
// ctrl.Filtri(inserire stringa);
}
else if(e.getActionCommand().equals(Acquista_cmd))
{
ctrl.visualizzazioneFattura();
}
else if(e.getActionCommand().equals(ModificaProfilo_cmd))
{
ctrl.ModificaProfilo();
}
else if(e.getActionCommand().equals(Uscire_cmd))
{
//dispose();
ctrl.visualizzazioneLogin();
}
else if(e.getActionCommand().equals(Antipasti_cmd))
{
panel_3.removeAll();
panel_3.add(panel_Antipasti);
panel_3.repaint();
panel_3.revalidate();
}
else if(e.getActionCommand().equals(primi_cmd))
{
panel_3.removeAll();
panel_3.add(panel_5);
panel_3.repaint();
panel_3.revalidate();
}
else if(e.getActionCommand().equals(primi_cmd))
{
panel_3.removeAll();
panel_3.add(panel_6);
panel_3.repaint();
panel_3.revalidate();
}
else if(e.getActionCommand().equals(secondi_cmd))
{
panel_3.removeAll();
panel_3.add(panel_7);
panel_3.repaint();
panel_3.revalidate();
}
else if(e.getActionCommand().equals(contorni_cmd))
{
panel_3.removeAll();
panel_3.add(panel_8);
panel_3.repaint();
panel_3.revalidate();
}
else if(e.getActionCommand().equals(dolci_cmd))
{
panel_3.removeAll();
panel_3.add(panel_9);
panel_3.repaint();
panel_3.revalidate();
}
else if(e.getActionCommand().equals(bevande_cmd))
{
panel_3.removeAll();
panel_3.add(panel_10);
panel_3.repaint();
panel_3.revalidate();
}
}
}