使用 JSpinner stateChanged 方法更新 JOptionPane 中的消息
Updating message in JOptionPane with JSpinner stateChanged method
我制作了一个带有 JSpinner 组件的 JOptionPane。我有一个名为 skillMessage 的字符串,它将是 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 方法时得到 "updated"。重新粉刷它似乎不起作用。提前致谢!
你实际上并没有在 JOptionPane
上触发重绘,即使你触发了,我也不确定它是否会更新。
更好的解决方案是将您尝试执行的功能封装到它自己的 class/component 中,这样您就可以控制视图本身并可以根据需要更新组件
举个例子:
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!");
}
}
我制作了一个带有 JSpinner 组件的 JOptionPane。我有一个名为 skillMessage 的字符串,它将是 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 方法时得到 "updated"。重新粉刷它似乎不起作用。提前致谢!
你实际上并没有在 JOptionPane
上触发重绘,即使你触发了,我也不确定它是否会更新。
更好的解决方案是将您尝试执行的功能封装到它自己的 class/component 中,这样您就可以控制视图本身并可以根据需要更新组件
举个例子:
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!");
}
}