一个JSpinner改变了所有

时间:2016-10-19 16:35:02

标签: java swing jspinner

我在使用JSpinner时遇到问题。当我改变spinerkp时,所有变量(kp,sp,lk,ct)都在改变值,当我改变任何其他JSpinner时,没有任何事情发生。我不知道这有什么问题。有人知道这有什么问题吗?

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class PorownanieLokat
{
private JFrame frame;
private JLabel naglowek;
private JLabel status;
private JPanel panel;

public int kp;
public int lk;
public int ct;
public int sp;



public PorownanieLokat()
{
    przygotujGUI();
}

public static void main(String args[])
{
    PorownanieLokat porownanieLokat = new PorownanieLokat();
    porownanieLokat.Lokata();
}

private void przygotujGUI() {
    frame = new JFrame("SWING");
    frame.setSize(new Dimension(400, 400));
    frame.setLayout(new GridLayout(4, 1));
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
        }
    });
    naglowek = new JLabel("", JLabel.CENTER);
    status = new JLabel("", JLabel.CENTER);

    status.setSize(350, 100);

    panel = new JPanel();
    panel.setLayout(new FlowLayout());

    frame.add(naglowek);
    frame.add(status);
    frame.add(panel);
    frame.setVisible(true);
}

public void Lokata()
{
    Obliczenia obliczenia = new Obliczenia();
    naglowek.setText("Wypełnij wszystkie pola aby obliczyć kapitał końcowy!");
    JButton x = new JButton( "Oblicz!");
    x.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            status.setText("Kapitał końcowy to: "+obliczenia.Licz(kp,sp,lk,ct));
        }
    });

    SpinnerModel spinnerModel1 = new SpinnerNumberModel(1,0,10000,1);
    JSpinner spinerkp = new JSpinner(spinnerModel1);
    spinerkp.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e1) {
            kp =(int) ((JSpinner)e1.getSource()).getValue();
            System.out.println(kp);
        }
    });
    SpinnerModel spinnerModel2 = new SpinnerNumberModel(1,0,100,1);
    JSpinner spinersp = new JSpinner(spinnerModel2);
    spinerkp.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e2) {
            sp= (int) ((JSpinner)e2.getSource()).getValue();
            System.out.println(sp);
        }
    });
    SpinnerModel spinnerModel3 = new SpinnerNumberModel(1,1,12,1);
    JSpinner spinerlk = new JSpinner(spinnerModel3);
    spinerkp.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e3) {
            lk=(int) ((JSpinner)e3.getSource()).getValue();
            System.out.println(lk);
        }
    });
    SpinnerModel spinnerModel4 = new SpinnerNumberModel(1,0,50,1);
    JSpinner spinerct = new JSpinner(spinnerModel4);
    spinerkp.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e4) {
            ct= (int) ((JSpinner)e4.getSource()).getValue();
            System.out.println(ct);
        }
    });

    panel.add(spinerkp);
    panel.add(spinersp);
    panel.add(spinerlk);
    panel.add(spinerct);
    panel.add(x);

    frame.setVisible(true);


}
}

1 个答案:

答案 0 :(得分:2)

您的代码会创建四个JSpinner个实例和四个ChangeListener个实例,但会将每个ChangeListener添加到spinerkp(而不是将它们添加到相应的JSpinner {1}})。

JSpinner spinerkp...
spinnerkp.addChangeListener(./..)
JSpinner spinersp...
spinnersp.addChangeListener(./..)//add the change listener to the appropriate JSpinner
...