当我设置 JProgressBar 的最小值、值和最大值时,它们不会更新,除非我关闭窗口并重新打开它。
图片:
谁能给我任何见解?我在这个问题上摸不着头脑。我进行了测试以确保解析正确完成(就是这样)。我测试只是直接输入数字。显然它有效,它只是在第一次打开窗口时不显示(这让我认为如果我更新值它只会显示最后一个值。
* 编辑 *
女士们,先生们……我可以介绍一下……SSCCE。我很抱歉发布这个,因为现在你会因为这个而感到我的痛苦:x
package com.jayavon.game.helper;
import javax.swing.*;
import java.awt.event.*;
public class SSCCE extends JFrame implements WindowListener {
private static final long serialVersionUID = 1L;
JFrame frame;
JPanel panel;
JButton characterButton;
JInternalFrame characterFrame;
/* Character Window */
JProgressBar totalExpProgressBar;
Action ClassCharacterButton = new ClassCharacterButton();
public static void main(String[] args){
//for thread safety
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SSCCE();
}
});
}
SSCCE() {
initGUI();
}
public void initGUI(){
frame = new JFrame("SSCCE");
panel = (JPanel)frame.getContentPane();
/**********************************
**
** Buttons
**
*********************************/
characterButton = new JButton("");
characterButton.setBounds(50,175,395,100);
characterButton.setVisible(true);
characterButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("C"), "characterButtonPress");
characterButton.getActionMap().put("characterButtonPress", ClassCharacterButton);
characterButton.setAction(ClassCharacterButton);
characterButton.setText("Click me three times(open/close/open) to get progress bar to fill");
panel.add(characterButton);
/**********************************
**
** Internal Frames
**
*********************************/
//#### Character frame start ####
characterFrame = new JInternalFrame("Character", true, true, false, false);
characterFrame.setLocation(50, 50);
characterFrame.setSize(300,105);
totalExpProgressBar = new JProgressBar();
totalExpProgressBar.setString("0/0");
totalExpProgressBar.setStringPainted(true);
characterFrame.add(totalExpProgressBar);
characterFrame.setResizable(false);
panel.add(characterFrame);
//#### Character frame end ####
/**********************************
**
** Panel Code
**
*********************************/
panel.setLayout(null);
panel.setFocusable(true);
/**********************************
**
** Frame Code
**
*********************************/
frame.setLocation(100, 100);
frame.setSize(500, 350);
frame.setVisible(true);
frame.setFocusable(true);
frame.addWindowListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class ClassCharacterButton extends AbstractAction {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
if (characterFrame.isVisible()){
characterFrame.setVisible(false);
} else {
fakeGetServerResponse();
}
}
}
public void fakeGetServerResponse(){
String incommingReply = "proskier-charactersWindow@20|10|10|10|0|234|3|200|400"; //fake message from server
final String splitAt[] = incommingReply.split("@"); //split the character name from the incommingReply at the '@' sign
String beforeAt[] = splitAt[0].split("-");
String commandName = beforeAt[1];
final String afterAt[] = splitAt[1].split("\\|");
if (commandName.equals("charactersWindow")){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
totalExpProgressBar.setString(afterAt[5] + "/" + afterAt[8]);
totalExpProgressBar.setMinimum(0);
totalExpProgressBar.setMinimum(Integer.parseInt(afterAt[7])); //TODO::SCREW YOU JAVA
totalExpProgressBar.setValue(Integer.parseInt(afterAt[5])); //TODO::SCREW YOU JAVA
totalExpProgressBar.setMaximum(Integer.parseInt(afterAt[8])); //TODO::SCREW YOU JAVA
characterFrame.setVisible(true);
}
});
}
}
@Override
public void windowClosing(WindowEvent arg0){
frame.dispose();
System.exit(1);
}
@Override
public void windowActivated(WindowEvent arg0) {
}
@Override
public void windowClosed(WindowEvent arg0) {
}
@Override
public void windowDeactivated(WindowEvent arg0) {
}
@Override
public void windowDeiconified(WindowEvent arg0) {
}
@Override
public void windowIconified(WindowEvent arg0) {
}
@Override
public void windowOpened(WindowEvent arg0) {
}
}