sasoriSR
22-12-2011, 14:53
ciao a tutti, ho un problema con la grafica di java, o meglio con il metodo
evt.getSoruce()
Il problema č principalmente in questo punto:
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);
}
Mi esce sempre false anche se premo i pulsanti giusti, se serve metto il resto del 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 :)
evt.getSoruce()
Il problema č principalmente in questo punto:
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);
}
Mi esce sempre false anche se premo i pulsanti giusti, se serve metto il resto del 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 :)