2
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% 完成。到目前为止,我非常感谢您给我的帮助。

4

2 回答 2

1

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

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
        }
    }
});
于 2012-10-04T01:24:53.073 回答
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);

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

于 2012-10-04T01:39:06.320 回答