Salve,
ho voluto fare un semplice calcolatore che somma due numeri.
Eclipse compila ma quando inserisco i due numeri non mi restituisce alcuna somma e non capisco perchè.
Posto il codice completo qui in basso.
codice:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calcolatrice extends JFrame {
public JButton addButton;
public JTextField firstNumber;
public JTextField secondNumber;
public JTextField answer;
public JLabel answerLabel;
public Calcolatrice(){
this.setTitle("Calcolatrice");
this.setSize(new Dimension(260,165));
this.setLocation(MouseInfo.getPointerInfo().getLocation());
this.setLayout(new FlowLayout());
this.setResizable(false);
this.addComponentListener(new ComponentListener(){
@Override
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {}
public void componentResized(ComponentEvent e){}
public void componentShown(ComponentEvent e) {
Calcolatrice_Load();
}
});
this.addButton = new JButton();
this.addButton.setText("Add");
this.addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
addButton_ActionPerformed();
}
});
this.firstNumber = new JTextField();
this.firstNumber.setText(" ");
this.secondNumber = new JTextField();
this.secondNumber.setText(" ");
this.answer = new JTextField();
this.answer.setText(" ");
this.answer.setEditable(false);
this.answerLabel = new JLabel();
this.answerLabel.setText("Risultato: ");
add(addButton);
add(firstNumber);
add(secondNumber);
add(answerLabel);
add(answer);
}
private void Calcolatrice_Load(){
firstNumber.setText(" ");
secondNumber.setText(" ");
answer.setText(" ");
}
private void addButton_ActionPerformed(){
try{
int num1 , num2;
num1 = Integer.parseInt(firstNumber.getText());
num2 = Integer.parseInt(secondNumber.getText());
int answer2 = num1+num2;
answer.setText(String.valueOf(answer2));
}catch(Exception ex){
}
}
}
codice:
public class main {
public static void main(String[] args){
Calcolatrice newform = new Calcolatrice();
newform.setVisible(true);
}
}