|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Junior Member
Iscritto dal: Oct 2008
Messaggi: 16
|
[Java] Problemi Grafica e Eventi
ciao a tutti, ho un problema con la grafica di java, o meglio con il metodo
evt.getSoruce() Il problema è principalmente in questo punto: Codice:
private void UGUActionPerformed(java.awt.event.ActionEvent evt){ System.out.println(evt.getSource() == btnSOT); System.out.println(evt.getSource() == btnSOM); System.out.println(evt.getSource() == btnDIV); System.out.println(evt.getSource() == btnMOL); } Codice:
import javax.swing.*; import java.awt.*; public class Gui2 extends JFrame { // VARIABILI DI ISTANZA // oggetti grafici JButton btnSOM; JButton btnSOT; JButton btnDIV; JButton btnMOL; /** * Constructor for objects of class InterfacciaGui */ public Gui2() { // setto i parametri della finestra setTitle("Calcolatrice Razionale by ..."); setBounds(100, 200, 500, 250); // creiamo i vari oggetti grafici Container cnt = getContentPane(); cnt.setLayout(null); JButton btnSOM = new JButton("+"); btnSOM.setBounds(250, 10, 50, 50); JButton btnSOT = new JButton("-"); btnSOT.setBounds(250, 60, 50, 50); JButton btnDIV = new JButton("*"); btnDIV.setBounds(250, 110, 50, 50); JButton btnMOL = new JButton("/"); btnMOL.setBounds(250, 160, 50, 50); // aggiungo i pulsanti al pannello cnt.add(btnSOM); cnt.add(btnSOT); cnt.add(btnDIV); cnt.add(btnMOL); // aggiungo gli eventi btnSOM.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { UGUActionPerformed(evt); } }); btnSOT.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { UGUActionPerformed(evt); } }); btnDIV.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { UGUActionPerformed(evt); } }); btnMOL.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { UGUActionPerformed(evt); } }); setVisible(true); setResizable(false); } private void UGUActionPerformed(java.awt.event.ActionEvent evt){ System.out.println("hai premuto"); System.out.println(evt.getSource() == btnSOT); System.out.println(evt.getSource() == btnSOM); System.out.println(evt.getSource() == btnDIV); System.out.println(evt.getSource() == btnMOL); } } Grazie a tutti in anticipo ![]() Ultima modifica di sasoriSR : 22-12-2011 alle 15:00. |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
L'ho corretto, adesso funziona:
Codice:
import javax.swing.*; import java.awt.*; public class Gui2 extends JFrame { // VARIABILI DI ISTANZA // oggetti grafici JButton btnSOM; JButton btnSOT; JButton btnDIV; JButton btnMOL; /** * Constructor for objects of class InterfacciaGui */ public Gui2() { // setto i parametri della finestra setTitle("Calcolatrice Razionale by ..."); setBounds(100, 200, 500, 250); // creiamo i vari oggetti grafici Container cnt = getContentPane(); cnt.setLayout(null); btnSOM = new JButton("+"); btnSOM.setBounds(250, 10, 50, 50); btnSOT = new JButton("-"); btnSOT.setBounds(250, 60, 50, 50); btnDIV = new JButton("*"); btnDIV.setBounds(250, 110, 50, 50); btnMOL = new JButton("/"); btnMOL.setBounds(250, 160, 50, 50); // aggiungo i pulsanti al pannello cnt.add(btnSOM); cnt.add(btnSOT); cnt.add(btnDIV); cnt.add(btnMOL); // aggiungo gli eventi btnSOM.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { UGUActionPerformed(evt); } }); btnSOT.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { UGUActionPerformed(evt); } }); btnDIV.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { UGUActionPerformed(evt); } }); btnMOL.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { UGUActionPerformed(evt); } }); setVisible(true); setResizable(false); } private void UGUActionPerformed(java.awt.event.ActionEvent evt){ System.out.println("hai premuto"); System.out.println(evt.getSource() == btnSOT); System.out.println(evt.getSource() == btnSOM); System.out.println(evt.getSource() == btnDIV); System.out.println(evt.getSource() == btnMOL); } }
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
![]() |
![]() |
![]() |
#3 |
Junior Member
Iscritto dal: Oct 2008
Messaggi: 16
|
Interessante, ma ma quindi non va mai messo in nessun caso?
|
![]() |
![]() |
![]() |
#4 | |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
L'errore era dato dal fatto che le reference ai bottoni dichiarate come membri di istanza, non venivano neanche inizializzate con i JButton (il loro valore restava a null) perchè la creazione di dette istanze avveniva dentro al costruttore di Gui2 a beneficio però di altre 4 reference locali al metodo che erano state nominate come i membri di istanza, però non erano i membri di istanza, ma appunto, reference locali, ergo la loro esistenza terminava alla fine dell'esecuzione del costruttore (ciao ciao bottoni).
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 22-12-2011 alle 16:09. |
|
![]() |
![]() |
![]() |
#5 |
Junior Member
Iscritto dal: Oct 2008
Messaggi: 16
|
ok, grazie di tutto
![]() PS: se volete si può chiudere |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 21:58.