Questo è il codice che ho:
public class NewGame3 extends JPanel implements MouseListener {
static JFrame frame3 = new JFrame();
Image immagine;
public NewGame3 (){
String path="C:/Users/andrea/Desktop/Immagini/uno.jpg";
immagine = Toolkit.getDefaultToolkit().createImage(path);
load(immagine);
}
public void load(Image immagine){
MediaTracker mt = new MediaTracker(frame3);
mt.addImage(immagine, 1);
try {
mt.waitForID(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(immagine, 0, 0,720,480, null);
}
public void mouseClicked(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {
frame3.setSize(720, 480);
Start_Menu start = new Start_Menu();
frame3.addKeyListener(start);
NewGame3 a = new NewGame3();
frame3.getContentPane().add(a);
frame3.setVisible(true);
}
public void mouseReleased(MouseEvent arg0) {
}
}
Nel "MousePressed" crea un nuovo frame dopo che il bottone è stato premuto (ti posto anche l'altro codice):
public class NewGame2 extends NewGame implements MouseListener {
static JFrame frame2 = new JFrame();
JButton scenario1 = new JButton("Scenario 1");
JButton scenario2 = new JButton("Scenario 2");
JButton scenario3 = new JButton("Casuale");
public NewGame2(){
scenario1.setDisplayedMnemonicIndex(3);
}
public void mouseClicked(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {
frame.dispose();
GridLayout griglia = new GridLayout(5,5);
frame2.setSize(720, 480);
frame2.setLayout(griglia);
NewGame3 scenario_uno = new NewGame3();
scenario1.addMouseListener(scenario_uno);
NewGame3 scenario_due = new NewGame3();
scenario2.addMouseListener(scenario_due);
Icon primo_scenario = new ImageIcon("C:/Users/andrea/Desktop/immagini/uno.jpg");
JLabel primo = new JLabel();
primo.setIcon(primo_scenario);
Icon secondo_scenario = new ImageIcon("C:/Users/andrea/Desktop/immagini/due.jpg");
JLabel secondo = new JLabel();
secondo.setIcon(secondo_scenario);
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.add(new JLabel("SELEZIONA UNO SCENARIO"));
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.add(primo);
frame2.add(secondo);
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.add(scenario1);
frame2.add(scenario2);
frame2.add(scenario3);
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.add(new JLabel());
frame2.setVisible(true);
}
public void mouseReleased(MouseEvent arg0) {}
}
Io voglio sapere quale bottone è stato premuto...