使用JSpinner stateChanged方法在JOptionPane中更新消息

时间:2017-05-15 07:15:22

标签: java swing joptionpane jspinner

我已经在其中创建了一个带有JSpinner组件的JOptionPane。我有一个名为skillMessage的String,它将是JOptionPane中显示的消息。我结合了我在网上做的研究来制作这样一个选项窗格(带有消息和微调器以及它被覆盖的stateChanged方法)。这是我的代码:

skillMessage = "Current skill level: " + currLevel + "\n";

if (skillPoint > 0) {
    skillMessage += "Do you want to upgrade this skill to level " + (currLevel + 1)  + "?\n";
} else {
    skillMessage += "You do not have enough skill point to upgrade this skill!\n";
}

skillMessage += "Cost: 1 skill point\nYou have: " + skillPoint + " skill point(s) left";

if (skillPoint > 0) {
    Object[] options = {"Upgrade", "Cancel"};

    JSpinner spinner = new JSpinner(new SpinnerNumberModel(currLevel, currLevel, maxLevel, 1));
    JComponent comp = spinner.getEditor();
    JFormattedTextField field = (JFormattedTextField)comp.getComponent(0);
    DefaultFormatter formatter = (DefaultFormatter)field.getFormatter();
    formatter.setCommitsOnValidEdit(true);
    ((JSpinner.DefaultEditor)comp).getTextField().setEditable(false);
    ((JSpinner.DefaultEditor)comp).getTextField().setFocusable(false);

    spinner.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent ce) {
            int valueChanged = (Integer)spinner.getValue() - currLevel;
            skillMessage = "Current skill level: " + spinner.getValue() + "\n";

            if ((skillPoint - valueChanged) > 0) {
                skillMessage += "Do you want to upgrade this skill to level " + (currLevel + 1)  + "?\n";
            } else {
                skillMessage += "You do not have enough skill point to upgrade this skill!\n";
            }

            skillMessage += "Cost: 1 skill point\nYou have: " + (skillPoint - valueChanged) + " skill point(s) left";
            repaint();
        }
    });

    if (JOptionPane.showOptionDialog(null, new Object[] {skillMessage, spinner}, "Upgrade Skill", 1, 3, null, options, options[0]) == 0) {
        if ((skillPoint - (Integer)spinner.getValue() - currLevel) >= 0) {
            currLevel++;
            JOptionPane.showMessageDialog(null, "You've successfully upgraded this skill to level " + currLevel);
        } else {
            JOptionPane.showMessageDialog(null, "You do not have enough skill point!");
        }
    }
}

我现在要做的是让JOptionPane中的skillMessage更新"更新"每次调整微调器的stateChanged方法时都会被调用。重新绘制它似乎并不能完成这项工作。提前谢谢!

1 个答案:

答案 0 :(得分:2)

您实际上并没有在JOptionPane上触发重新粉饰,即使您这样做了,我也不确定它是否会更新。

更好的解决方案是将您尝试执行的功能封装到其自己的类/组件中,这样,您可以控制视图本身并可以根据需要更新组件

举个例子:

public class SkillPane extends JPanel {

    private JSpinner spinner;
    private JLabel[] messages;
    private int skillPoints;
    private int currLevel;

    public SkillPane(final int currLevel, final int maxLevel, final int skillPoint) {
        spinner = new JSpinner(new SpinnerNumberModel(currLevel, currLevel, maxLevel, 1));
        this.skillPoints = skillPoint;
        this.currLevel = currLevel;

        messages = new JLabel[]{new JLabel(), new JLabel(), new JLabel()};
        updateMessages();

        setBorder(new EmptyBorder(4, 4, 4, 4));
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(4, 4, 4, 4);
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.LINE_START;
        add(messages[0], gbc);
        add(messages[1], gbc);
        add(messages[2], gbc);
        add(spinner, gbc);

        spinner.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent ce) {
                updateMessages();
            }
        });
    }

    public int getValue() {
        return (int) spinner.getValue();
    }

    public void updateMessages() {
        int valueChanged = (Integer) spinner.getValue() - currLevel;
        messages[0].setText("Current skill level: " + spinner.getValue());

        if ((skillPoints - valueChanged) > 0) {
            messages[1].setText("Do you want to upgrade this skill to level " + (currLevel + 1) + "?");
        } else {
            messages[1].setText("You do not have enough skill point to upgrade this skill!");
        }

        messages[2].setText("Cost: 1 skill point\nYou have: " + (skillPoints - valueChanged) + " skill point(s) left");
    }

}

nb:我的原始设计有一个标签并使用HTML格式化文本,但我发现不断破坏对话框,您还可以使用不可编辑的JTextArea或类似组件来生成类似的效果

您可以使用类似的内容拨打电话。

    int skillPoint = 10;
    int currLevel = 0;

    Object[] options = {"Upgrade", "Cancel"};
    SkillPane pane = new SkillPane(currLevel, 100, skillPoint);

    if (JOptionPane.showOptionDialog(null, pane, "Upgrade Skill", 1, 3, null, options, options[0]) == 0) {
        int value = pane.getValue();

        if ((skillPoint - value - currLevel) >= 0) {
            currLevel++;
            JOptionPane.showMessageDialog(null, "You've successfully upgraded this skill to level " + currLevel);
        } else {
            JOptionPane.showMessageDialog(null, "You do not have enough skill point!");
        }
    }