代码无法正常工作时,JProgressBar无法更新

时间:2019-07-12 13:31:06

标签: java swing concurrency jprogressbar

我必须将几个PlayerDB实例添加到PlayersDB实例,并且我希望JProgressBar显示状态。 问题在于,其余代码可以正常工作时,JProgressBar不会更新。 我搜索了这个问题并找到了不同的答案,但是没有任何效果。 也许我应该为任务使用新线程?我认为这不会有用,因为使用此代码,主线程应该在每个重要的小任务之后而不是仅在一个大任务之后更新进度条。我错了吗?

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.JPanel;
import javax.swing.DefaultComboBoxModel;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JProgressBar;

public class Start {

    private JFrame frame;
    private JComboBox comboBox_1;
    private JProgressBar progressBar;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Start window = new Start();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Start() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 352, 139);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"Ogame.it"}));
        comboBox.setBounds(10, 11, 170, 20);
        frame.getContentPane().add(comboBox);

        JButton btnNewButton = new JButton("LOAD LANG");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                comboBox_1.setModel(new DefaultComboBoxModel(Methods.loadServers(Methods.loadLangCode(""+comboBox.getSelectedItem()))));
            }
        });
        btnNewButton.setBounds(190, 10, 136, 23);
        frame.getContentPane().add(btnNewButton);

        comboBox_1 = new JComboBox();
        comboBox_1.setModel(new DefaultComboBoxModel(new String[] {}));
        comboBox_1.setBounds(10, 36, 170, 20);
        frame.getContentPane().add(comboBox_1);

        JButton btnLoadServer = new JButton("LOAD SERVER");
        btnLoadServer.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String sid = Methods.loadLangCode(""+comboBox.getSelectedItem())+Methods.loadServerCode(Methods.loadLangCode(""+comboBox.getSelectedItem()), ""+comboBox_1.getSelectedItem());
                Methods.dlUniverse(sid);
                Methods.dlPlayers(sid);
                PlayersDB playersDB = new PlayersDB(sid);
                PlayerData temp = null;
                temp = Methods.loadPlayerData(sid, 1);
                int playersNumber = playersDB.getPlayers().getPlayers().size();
                Methods.dlPlayerData(sid, 1);
                progressBar.setMaximum(playersNumber);
                if (temp.getTimestamp() == Methods.loadPlayerData(sid, 1).getTimestamp()) {
                    for (int i=0; i<playersNumber; i++) {
                        playersDB.addPlayer(i);
                        progressBar.setValue(i+1);
                    }
                } else {
                    for (int i=0; i<playersNumber; i++) {
                        Methods.dlPlayerData(sid, playersDB.getPlayers().getPlayers().get(i).getId());
                        playersDB.addPlayer(i);
                        progressBar.setValue(i+1);
                    }
                }
                ServerWindow.newSW(sid, playersDB);
                frame.dispose();
            }
        });
        btnLoadServer.setBounds(190, 35, 136, 23);
        frame.getContentPane().add(btnLoadServer);

        progressBar = new JProgressBar(0, 1);
        progressBar.setStringPainted(true);
        progressBar.setBounds(10, 67, 316, 22);
        frame.getContentPane().add(progressBar);
    }

    public void pbUpdate(int i) {

    }
}

0 个答案:

没有答案