【问题标题】:JProgressBar painting issueJProgressBar 绘制问题
【发布时间】:2012-04-26 22:29:39
【问题描述】:

当我设置 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) {            
    }
}

【问题讨论】:

  • Event Dispatch Thread也称为EDT上调用JProgressBar的setValue(...)方法是否小心?
  • 在审查您的代码时,正如我所怀疑的那样——您的代码不遵守 Swing 线程规则。请阅读我在上面评论中链接的教程。
  • @Hovercraft Full Of Eels 我现在就读,谢谢你们。我认为这没关系,因为它本身在自己的线程中运行,但我会继续阅读。
  • @Hovercraft Full Of Eels 请告诉我我是否完全错过了这里的要点,但我想我将代码移到了正确的线程,直到第二次打开窗口它仍然无法工作。
  • 我不确定这是否能解决您的问题,但我会使用invokeLater(...),而不是invokeAndWait(...)。你几乎从不使用后者。

标签: java swing jframe jprogressbar event-dispatch-thread


【解决方案1】:

您可能正在 EDT(事件调度线程)中执行工作。该线程有一个事件队列,它们一次按顺序分派一个,因为 AWT 不是线程安全的。这允许 UI 更新和响应事件(程序事件,如 repaint 或 invokeLater,或用户事件,如鼠标和按键事件)。

所以当你在 EDT 中工作时,你会阻塞线程并阻止它调度事件,例如重绘、点击、按键事件等......

通常的解决方案是将工作转移到另一个线程中,例如使用SwingWorker

顺便说一句,Thread.sleep(long) 是一个静态方法,所以不需要调用 currentThread(),如果需要,只需调用 Thread.sleep(...)。但同样,你真的应该避免在 EDT 中这样做,因为它会阻塞它并且也会阻塞 UI。

【讨论】:

  • 我自己也有同样的疑惑,但我希望整个 GUI 都处于冻结状态。好吧,我们很快就会看到希望。不过,他似乎没有显示任何与他的问题相关的代码:(
  • @HovercraftFullOfEels 是的,我真的说不出来,但我经常看到人们因为他们阻挡了 EDT 而出现了重绘问题,所以这比经过验证的科学要多一些猜测。
  • @Guillaume Polet 同意所有事件都在一瞬间完成,但在某些情况下,特别是如果通过 Thread.sleep 冻​​结,您可以在 EDT 期间从 JButton 的事件中使用 JProgressBar 移动,而不是全部台阶,但被画了,今天没有赞成票......
  • @Hovercraft Full Of Eels:我还应该显示哪些代码来帮助更好地诊断?这都是从一个单独的线程运行的。为了彻底,我附上了上面的整个班级。
  • @Guillaume Polet 如果您不介意再次查看,我已经添加了整个班级,看看您是否有任何建议。感谢大家到目前为止的帮助!!!
【解决方案2】:

这对所有Swing JComponentsRunnableThread.sleep(int) 都有效,GUI 上带有准动画

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ShakingButtonDemo implements Runnable {

    private JButton button;
    private JRadioButton radioWholeButton;
    private JRadioButton radioTextOnly;

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new ShakingButtonDemo());
    }

    @Override
    public void run() {
        radioWholeButton = new JRadioButton("The whole button");
        radioTextOnly = new JRadioButton("Button text only");
        radioWholeButton.setSelected(true);
        ButtonGroup bg = new ButtonGroup();
        bg.add(radioWholeButton);
        bg.add(radioTextOnly);
        button = new JButton("  Shake with this Button  ");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                shakeButton(radioWholeButton.isSelected());
            }
        });
        JPanel p1 = new JPanel();
        p1.setBorder(BorderFactory.createTitledBorder("Shake Options"));
        p1.setLayout(new GridLayout(0, 1));
        p1.add(radioWholeButton);
        p1.add(radioTextOnly);
        JPanel p2 = new JPanel();
        p2.setLayout(new GridLayout(0, 1));
        p2.add(button);
        JFrame frame = new JFrame();
        frame.setTitle("Shaking Button Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(p1, BorderLayout.NORTH);
        frame.add(p2, BorderLayout.SOUTH);
        frame.setSize(240, 160);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private void shakeButton(final boolean shakeWholeButton) {
        final Point point = button.getLocation();
        final Insets margin = button.getMargin();
        final int delay = 75;
        Runnable r = new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 30; i++) {
                    try {
                        if (shakeWholeButton) {
                            moveButton(new Point(point.x + 5, point.y));
                            Thread.sleep(delay);
                            moveButton(point);
                            Thread.sleep(delay);
                            moveButton(new Point(point.x - 5, point.y));
                            Thread.sleep(delay);
                            moveButton(point);
                            Thread.sleep(delay);
                        } else {// text only
                            setButtonMargin(new Insets(margin.top, margin.left + 3, margin.bottom, margin.right - 2));
                            Thread.sleep(delay);
                            setButtonMargin(margin);
                            Thread.sleep(delay);
                            setButtonMargin(new Insets(margin.top, margin.left - 2, margin.bottom, margin.right + 3));
                            Thread.sleep(delay);
                            setButtonMargin(margin);
                            Thread.sleep(delay);
                        }
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        };
        Thread t = new Thread(r);
        t.start();
    }

    private void moveButton(final Point p) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                button.setLocation(p);
            }
        });
    }

    private void setButtonMargin(final Insets margin) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                button.setMargin(margin);
            }
        });
    }
}

编辑

示例 SwingWorker 和 JProgressBar & SwingWorker#cancel()

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class SwingWorkerExample extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private final JButton startButton, stopButton;
    private JScrollPane scrollPane = new JScrollPane();
    private JList listBox = null;
    private DefaultListModel listModel = new DefaultListModel();
    private final JProgressBar progressBar;
    private mySwingWorker swingWorker;

    public SwingWorkerExample() {
        super("SwingWorkerExample");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new GridLayout(2, 2));
        startButton = makeButton("Start");
        stopButton = makeButton("Stop");
        stopButton.setEnabled(false);
        progressBar = makeProgressBar(0, 99);
        listBox = new JList(listModel);
        scrollPane.setViewportView(listBox);
        add(scrollPane);
        //Display the window.
        pack();
        setVisible(true);
    }
//Class SwingWorker<T,V> T - the result type returned by this SwingWorker's doInBackground
//and get methods V - the type used for carrying out intermediate results by this SwingWorker's
//publish and process methods

    private class mySwingWorker extends javax.swing.SwingWorker<ArrayList<Integer>, Integer> {
//The first template argument, in this case, ArrayList<Integer>, is what s returned by doInBackground(),
//and by get(). The second template argument, in this case, Integer, is what is published with the
//publish method. It is also the data type which is stored by the java.util.List that is the parameter
//for the process method, which recieves the information published by the publish method.

        @Override
        protected ArrayList<Integer> doInBackground() {
//Returns items of the type given as the first template argument to the SwingWorker class.
            if (javax.swing.SwingUtilities.isEventDispatchThread()) {
                System.out.println("javax.swing.SwingUtilities.isEventDispatchThread() returned true.");
            }
            Integer tmpValue = new Integer(1);
            ArrayList<Integer> list = new ArrayList<Integer>();
            for (int i = 0; i < 100; i++) {
                for (int j = 0; j < 100; j++) { //find every 100th prime, just to make it slower
                    tmpValue = FindNextPrime(tmpValue.intValue());
//isCancelled() returns true if the cancel() method is invoked on this class. That is the proper way
//to stop this thread. See the actionPerformed method.
                    if (isCancelled()) {
                        System.out.println("SwingWorker - isCancelled");
                        return list;
                    }
                }
//Successive calls to publish are coalesced into a java.util.List, which is what is received by process,
//which in this case, isused to update the JProgressBar. Thus, the values passed to publish range from
//1 to 100.
                publish(new Integer(i));
                list.add(tmpValue);
            }
            return list;
        }//Note, always use java.util.List here, or it will use the wrong list.

        @Override
        protected void process(java.util.List<Integer> progressList) {
//This method is processing a java.util.List of items given as successive arguments to the publish method.
//Note that these calls are coalesced into a java.util.List. This list holds items of the type given as the
//second template parameter type to SwingWorker. Note that the get method below has nothing to do with the
//SwingWorker get method; it is the List's get method. This would be a good place to update a progress bar.
            if (!javax.swing.SwingUtilities.isEventDispatchThread()) {
                System.out.println("javax.swing.SwingUtilities.isEventDispatchThread() + returned false.");
            }
            Integer percentComplete = progressList.get(progressList.size() - 1);
            progressBar.setValue(percentComplete.intValue());
        }

        @Override
        protected void done() {
            System.out.println("doInBackground is complete");
            if (!javax.swing.SwingUtilities.isEventDispatchThread()) {
                System.out.println("javax.swing.SwingUtilities.isEventDispatchThread() + returned false.");
            }
            try {
//Here, the SwingWorker's get method returns an item of the same type as specified as the first type parameter
//given to the SwingWorker class.
                ArrayList<Integer> results = get();
                for (Integer i : results) {
                    listModel.addElement(i.toString());
                }
            } catch (Exception e) {
                System.out.println("Caught an exception: " + e);
            }
            startButton();
        }

        boolean IsPrime(int num) { //Checks whether a number is prime
            int i;
            for (i = 2; i <= num / 2; i++) {
                if (num % i == 0) {
                    return false;
                }
            }
            return true;
        }

        protected Integer FindNextPrime(int num) { //Returns next prime number from passed arg.
            do {
                if (num % 2 == 0) {
                    num++;
                } else {
                    num += 2;
                }
            } while (!IsPrime(num));
            return new Integer(num);
        }
    }

    private JButton makeButton(String caption) {
        JButton b = new JButton(caption);
        b.setActionCommand(caption);
        b.addActionListener(this);
        getContentPane().add(b);
        return b;
    }

    private JProgressBar makeProgressBar(int min, int max) {
        JProgressBar progressBar1 = new JProgressBar();
        progressBar1.setMinimum(min);
        progressBar1.setMaximum(max);
        progressBar1.setStringPainted(true);
        progressBar1.setBorderPainted(true);
        getContentPane().add(progressBar1);
        return progressBar1;
    }

    private void startButton() {
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        System.out.println("SwingWorker - Done");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("Start" == null ? e.getActionCommand() == null : "Start".equals(e.getActionCommand())) {
            startButton.setEnabled(false);
            stopButton.setEnabled(true);
// Note that it creates a new instance of the SwingWorker-derived class. Never reuse an old one.
            (swingWorker = new mySwingWorker()).execute(); // new instance
        } else if ("Stop" == null ? e.getActionCommand() == null : "Stop".equals(e.getActionCommand())) {
            startButton.setEnabled(true);
            stopButton.setEnabled(false);
            swingWorker.cancel(true); // causes isCancelled to return true in doInBackground
            swingWorker = null;
        }
    }

    public static void main(String[] args) {
// Notice that it kicks it off on the event-dispatching thread, not the main thread.
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                SwingWorkerExample swingWorkerExample = new SwingWorkerExample();
            }
        });
    }
}

【讨论】:

  • 请查看上面已编辑的问题。我发布了整个课程以表明它没有在同一个线程中运行。这不是我拥有的进度条的“典型”用法。这是为了在游戏中体验。
  • @JayAvon 那么你必须在这里检查我的编辑,如前所述,你在 Swing 中遇到了 Concurency 问题,
  • @JayAvon 请在此处查看我的编辑,结论,SwingWorker 对于简单的后台线程来说非常复杂,优于 Java 方法的所有替代方案(对于 > JDK6_019 的通知,较低版本非常错误),对于多线程是使用 Runnable#Thread 更好,与 SwingWorker 相比,为 Runnable#Thread 编写代码非常直观,
【解决方案3】:

问题似乎是您在设置进度条的值之后设置了最大值,这会将值重置为最小值。

import javax.swing.*;

public class ProgressBarMinValue {
   private static void createAndShowGui() {
      JProgressBar progressBar = new JProgressBar();

      int value = 234;
      int denominator = 400;
      int minValue = 200;
      progressBar.setString(value + "/" + denominator);
      progressBar.setMinimum(minValue);
      System.out.println("value := " + value);

      progressBar.setValue(value);  // (A)

      progressBar.setMaximum(denominator);

      // progressBar.setValue(value);  // (B)

      JPanel mainPanel = new JPanel();
      mainPanel.add(progressBar);

      JOptionPane.showMessageDialog(null, mainPanel);
      System.out.println("progressBar.getValue() := " + progressBar.getValue());
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

交换注释和取消注释行 (A) 和 (B),如果您设置进度条的值设置它的最小值和最大值之后,所有的工作都很好。

这就是你的解决方案。

请注意,我通过不断削减您的代码直到问题无法重现,才获得了这个最小的 SSCCE。

【讨论】:

  • 我不知道如何感谢你...我不确定我是否会想出来!你摇滚!!
  • 你和我……谢谢,谢谢,谢谢,这些 GUI 东西一直让我发疯,真的减慢了我的进步。我的待办事项清单上的下一个是使各种 JInternalFrames 出现在前面(因为 toFront() 方法不起作用)。我认为这是因为我将它们直接添加到添加到我的主框架的面板中。我读了一点,添加 JDesktopPane 可能是解决方案。 手指交叉
  • 您肯定应该使用 JDesttopPane。
猜你喜欢
  • 2011-11-06
  • 2014-09-10
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多