Java:组合JSpinners,事件监听器和文本字段

时间:2012-10-04 01:20:28

标签: java swing

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.*;
public class guiStatDistribution2 extends JFrame {

int str = 1;
int dex = 1;
int intel = 1;
int con = 1;
int cha = 1;
int remainingPoints = 45;

JTextField remainingDisplay = new JTextField("You have " + remainingPoints + " points left to distribute.");

SpinnerNumberModel strPoints = new SpinnerNumberModel(1,1,46,1);
SpinnerNumberModel dexPoints = new SpinnerNumberModel(1,1,46,1);
SpinnerNumberModel intPoints = new SpinnerNumberModel(1,1,46,1);
SpinnerNumberModel conPoints = new SpinnerNumberModel(1,1,46,1);
SpinnerNumberModel chaPoints = new SpinnerNumberModel(1,1,46,1);



public guiStatDistribution2() {

    JSpinner strSpin = new JSpinner(strPoints);
    JSpinner dexSpin = new JSpinner(dexPoints);
    JSpinner intSpin = new JSpinner(intPoints);
    JSpinner conSpin = new JSpinner(conPoints);
    JSpinner chaSpin = new JSpinner(chaPoints);
    //instance for strength
    strSpin.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            if (remainingPoints != 0) {
                str = strPoints.getNumber().intValue();
                remainingPoints = (remainingPoints - (str-1) - (dex-1) - (intel-1) - (con-1) - (cha-1));
                remainingDisplay.revalidate();
            }
            if (remainingPoints == 0) {
                JOptionPane.showMessageDialog(null, "No points left. Lower another score to add more.");
            }
        }
    });
    //instance for dexterity
    dexSpin.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent f) {
            if (remainingPoints != 0) {
                dex = dexPoints.getNumber().intValue();
                remainingPoints = (remainingPoints - (str-1) - (dex-1) - (intel-1) - (con-1) - (cha-1));
                remainingDisplay.revalidate();
            }
            if (remainingPoints == 0) {
                JOptionPane.showMessageDialog(null, "No points left. Lower another score to add more.");
            }
        }
    });
    //instance for intelligence
    intSpin.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent g) {
            if (remainingPoints != 0) {
                intel = intPoints.getNumber().intValue();
                remainingPoints = (remainingPoints - (str-1) - (dex-1) - (intel-1) - (con-1) - (cha-1));
                remainingDisplay.revalidate();
            }
            if (remainingPoints == 0) {
                JOptionPane.showMessageDialog(null, "No points left. Lower another score to add more.");
            }
        }
    });
    //instance for constitution
    conSpin.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent h) {
            if (remainingPoints != 0) {
                con = conPoints.getNumber().intValue();
                remainingPoints = (remainingPoints - (str-1) - (dex-1) - (intel-1) - (con-1) - (cha-1));
                remainingDisplay.revalidate();
            }
            if (remainingPoints == 0) {
                JOptionPane.showMessageDialog(null, "No points left. Lower another score to add more.");
            }
        }
    });
    //instance for charisma
    chaSpin.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent i) {
            if (remainingPoints != 0) {
                cha = chaPoints.getNumber().intValue();
                remainingPoints = (remainingPoints - (str-1) - (dex-1) - (intel-1) - (con-1) - (cha-1));
                remainingDisplay.revalidate();
            }
            if (remainingPoints == 0) {
                JOptionPane.showMessageDialog(null, "No points left. Lower another score to add more.");
            }
        }
    });

    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(1,2,0,5));
    p1.add(new JTextField("Strength: "));
    p1.add(strSpin);

    JPanel p2 = new JPanel();
    p2.setLayout(new GridLayout(1,2,0,5));
    p2.add(new JTextField("Dexterity: "));
    p2.add(dexSpin);

    JPanel p3 = new JPanel();
    p3.setLayout(new GridLayout(1,2,0,5));
    p3.add(new JTextField("Intelligence: "));
    p3.add(intSpin);

    JPanel p4 = new JPanel();
    p4.setLayout(new GridLayout(1,2,0,5));
    p4.add(new JTextField("Constitution: "));
    p4.add(conSpin);

    JPanel p5 = new JPanel();
    p5.setLayout(new GridLayout(1,2,0,5));
    p5.add(new JTextField("Charisma: "));
    p5.add(chaSpin);

    JPanel p6 = new JPanel();
    p6.add(remainingDisplay);

    JButton confirm = new JButton("Confirm");
    confirm.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int confirmed = JOptionPane.showOptionDialog(null, "Is this okay?\nStrength: " + str + "\nDexterity: " + dex + "\nIntelligence: " + intel + "\nConstitution: " + con + "\nCharisma: " + cha, "Confirm distribution", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
            if (confirmed == JOptionPane.YES_OPTION) {
                System.exit(0);
            }
        }
    });
    JPanel p7 = new JPanel();
    p7.add(confirm);

    setLayout(new GridLayout(7,1,0,5));
    add(p6);
    add(p1);
    add(p2);
    add(p3);
    add(p4);
    add(p5);
    add(p7);

}
public static void main(String[] args) {
    guiStatDistribution2 frame = new guiStatDistribution2();
    frame.setTitle("Distribute your skill points");
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

好的,这是用旋转器代替按钮的更新代码。我刚遇到两个问题,但在解决之后,程序将100%完成。到目前为止,我非常感谢你给我的帮助。

2 个答案:

答案 0 :(得分:1)

我建议每个属性使用JSpinners而不是JTextfield和两个JButtons。这个trail会告诉你需要知道的一切。基本步骤首先需要为每个微调器创建数字模型,例如:

SpinnerNumberModel strNumberModel = SpinnerNumberModel(1, 1, 20, 1); // value, min, max, step

创建微调器:

JSpinner strSpinner = new JSpinner(strNumberModel);

然后你需要实现ChangeListener,例如:

strSpinner.addChangeListener(new ChangeListener() {

    @Override
    public void stateChanged(ChangeEvent e) {
        str = strNumberModel.getNumber().intValue();
        remainingPoints = remainingPoints - (str + dex + intel + con + cha);
        if (remainingPoints <= 0) {
            // do something
        }
    }
});

答案 1 :(得分:1)

我同意llya,我会使用JSpinners

但是,如果您喜欢挑战,我会创建一个AttributeAction,其范围可以从AbstractAction延伸,可以采用JTextField修饰符,也可能是范围

public class AttributeAction extends AbstractAction {
    private JTextField field;
    private int modifier;
    private int min;
    private int max;

    public AbstractAttributeAction(String text, JTextField field, int modifier, int max, int min) {
        this.field = field;
        this.modifier = modifier;
        this.max = max;
        this.min = min;
        putValue(NAME, text);
    }

    public void actionPerfomed(ActionEvent evt) {
        try {
            int value = Integer.parseInt(fld.getText());
            value += modifier;
            if (value >= min && value <= max) {
                fld.setText(Integer.toString(value));
            }
        } catch (NumberFormatException exp) {
            // Value was bad
        }
    }
}

然后,对于每个按钮,您只需创建一个适当的操作......

JTextField strengthField = new JTextField();
JButton btnPlus = new JButton("+", strengthField, 1, 0, 100);
JButton btnMinus = new JButton("-", strengthField, -1, 0, 100);

当然,这会打开一大堆蠕虫,比如如何防止用户输入非数字值,但这是另一个问题;)