java swing program sumujący

0

witam napisałem program który sumuje dwie podane liczby i prosiłbym o opinie na temat napisanego kodu

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsuma;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import javax.swing.JOptionPane;


/**
 *
 * @author marcin
 */
public class KlasaSuma extends JFrame implements ActionListener{

    private int wysokosc = 100;
    private int szerokosc = 300;
    private int pozycja_okna_x = 500;
    private int pozycja_okna_y = 400;
    private JPanel jPanel = new JPanel();
    private JButton jButtonLicz = new JButton();
    private JButton jButtonWyjscie = new JButton();
    private JButton jButtonInfo = new JButton();
    private JLabel jLabelA = new JLabel();
    private JLabel jLabelB = new JLabel();
    private JLabel jLabelC = new JLabel();
    private JTextField textPoleA = new JTextField();
    private JTextField textPoleB = new JTextField();
    private JTextField textPoleC = new JTextField();
    private GridLayout gridLayout = new GridLayout(3, 3);
    
    public KlasaSuma() {
        setTitle("suma");
        setLocation(pozycja_okna_x, pozycja_okna_y);
        setSize(szerokosc, wysokosc);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(jPanel);
        
        jLabelA.setText("a=");
        jLabelB.setText("b=");
        jLabelC.setText("a+b=");
        
        textPoleA.setText("0");
        textPoleB.setText("0");
        textPoleC.setText("0");
        
        jButtonLicz.setText("licz");
        jButtonLicz.addActionListener(this);
        jButtonWyjscie.setText("wyjscie");
        jButtonWyjscie.addActionListener(this);
        jButtonInfo.setText("info");
        jButtonInfo.addActionListener(this);
        
        jPanel.setLayout(gridLayout);
        jPanel.add(jLabelA);
        jPanel.add(textPoleA);
        jPanel.add(jButtonLicz);
        jPanel.add(jLabelB);
        jPanel.add(textPoleB);      
        jPanel.add(jButtonInfo);
        jPanel.add(jLabelC);  
        jPanel.add(textPoleC);
        jPanel.add(jButtonWyjscie);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object object = e.getSource();
        if(object == jButtonLicz){
            suma();
        } else if(object == jButtonInfo){
            JOptionPane.showMessageDialog(null, "program liczy sume dwoch podanych liczb calkowitych !!!", "info", JOptionPane.INFORMATION_MESSAGE);
        } else if(object == jButtonWyjscie){
            System.exit(0);
        }
    }
    
    private void suma(){
        int a = Integer.parseInt(textPoleA.getText());
        int b = Integer.parseInt(textPoleB.getText());
        textPoleC.setText(String.valueOf(a + b));
    }
    
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingsuma;

import javax.swing.SwingUtilities;

/**
 *
 * @author marcin
 */
public class JavaSwingSuma {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                KlasaSuma klasaSuma = new KlasaSuma();
            }
        });
    }
}

0

dokładnie nie czytałem, ale drobna modyfikacja ode mnie:

try{
int a = Integer.parseInt(textPoleA.getText());
        int b = Integer.parseInt(textPoleB.getText());
        textPoleC.setText(String.valueOf(a + b));
}catch(NumberFormatException nfe){
nfe.printStacktrace;
}

to tak na wypadek usera myślącego, że ';gsre[\ to liczba całkowita

0

Program nie zawsze poprawnie liczy. Wpisz np. do obu pól tekstowych liczby 2 000 000 000 (bez spacji).

0

Zamiast JTextField prawdopodobnie lepiej było by wykorzystać też JFormattedTextField. Zamiast przycisku wywołującego akcję, mógłbyś też zastanowić się czy nie warto zastosować jakichś DocumentListenerów na polach tekstowych i automatycznie obliczać wynik wyrażenia po kontrolce.

1 użytkowników online, w tym zalogowanych: 0, gości: 1