Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it L'avatar di mainetz
    Registrato dal
    Oct 2003
    Messaggi
    132

    [JAVA] Abilitare JPanel a seconda del bottone premuto.

    Ciao a tuttii

    Sono ancora fermo al mio problema dopo numerosi tentativi...

    Ho creato questa cosa:

    codice:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.*;
    
    import javax.swing.*;
    
    public class ProvaGUI extends JPanel implements ActionListener {
    	
        protected JPanel botton, panel1, panel2;
        
        public ProvaGUI(){
        	
        	setLayout(new BorderLayout());
        	
        	panel1 = new JPanel();
        	panel2 = new JPanel();
        	
        	botton = new JPanel();
        	
        	JButton b1 = new JButton("Bottone 1");
        	JButton b2 = new JButton("Bottone 2");
        	JButton a1 = new JButton("Abilita 1");
        	JButton a2 = new JButton("Abilita 2");
        	
        	
        	panel1.add(b1);
        	panel2.add(b2);
        	
        	
        	botton.add(a1);
        	a1.setMnemonic(KeyEvent.VK_D);
        	a1.setActionCommand("vPanel1");
        	botton.add(a2);
        	a2.setMnemonic(KeyEvent.VK_D);
        	a2.setActionCommand("vPanel2");
        	
        	a1.addActionListener(this);
        	a2.addActionListener(this);
    
        	add(botton, BorderLayout.PAGE_START);
        	add(panel1, BorderLayout.CENTER);
        	panel1.setVisible(false);
        	add(panel2, BorderLayout.CENTER);
        	panel2.setVisible(false);
        	
        }
    	
    	public void actionPerformed(ActionEvent e) {
    		
    		if("vPanel1".equals(e.getActionCommand())){
    			panel1.setVisible(true);
    			panel2.setVisible(false);
    			
    		}else{
    			panel1.setVisible(false);
    			panel2.setVisible(true);			
    		}		     
        }
    	
        
        private static void createAndShowGUI() {
    
            //Create and set up the window.
            JFrame frame = new JFrame("ButtonDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            ProvaGUI prova = new ProvaGUI();
            frame.getContentPane().add(prova);
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
        
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI(); 
                }
            });
        }
    
    }
    Che dovrebbe fare questa cosa:

    premendo su a1 dovrebbe abilitare il panel1

    premendo su a2 dovrebbe abilitare il panel2

    Solo che abilita solamente il panel2 alla pressione di a2.

    Dove commetto l'errore??

    Grazie mille!

    mainetz.

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: [JAVA] Abilitare JPanel a seconda del bottone premuto.

    Originariamente inviato da mainetz
    Solo che abilita solamente il panel2 alla pressione di a2.
    Perché

    add(panel2, BorderLayout.CENTER);

    "sovrascrive" il pannello aggiunto con:

    add(panel1, BorderLayout.CENTER);

    Hai sempre e solo 1 componente in una delle aree NORTH, CENTER ecc...

    Ma .... usare un CardLayout o un JTabbedPane ... no?
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.