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.