简体   繁体   English

JProgressBar.stringPainted(真); 不管用

[英]JProgressBar.stringPainted(true); is not working

This is a part of my java code, in this code I have written that when I click the button the value of JProgressBar should becomes 0 and stringPainted() returns true , but "string painted" is not visible when I click the button, please help. 这是我的Java代码的一部分,在该代码中,我编写了以下代码:单击按钮时, JProgressBar的值stringPainted() 0并且stringPainted()返回true ,但是单击按钮时"string painted"不可见,请救命。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class R implements ActionListener {
    static int y;
    CustomProgressBar b = new CustomProgressBar();

    public static void main(String arg[]) throws Exception {
        new R();
    }

    public R() throws Exception {
        JFrame f = new JFrame();
        JButton btn = new JButton("Click");
        f.setExtendedState(JFrame.MAXIMIZED_BOTH);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setUndecorated(true);
        f.setLayout(new FlowLayout());
        btn.addActionListener(this);
        f.add(b);
        f.add(btn);
        f.setVisible(true);
    }

    class CustomProgressBar extends JProgressBar {
        private static final long serialVersionUID = 1L;
        private boolean isStringToBePainted = false;

        public CustomProgressBar() {
            super(JProgressBar.VERTICAL,0,100);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (isStringToBePainted) {
                Dimension size = CustomProgressBar.this.getSize();
                if (CustomProgressBar.this.getPercentComplete() < 0.9) {
                    R.y = (int) (size.height - size.height * CustomProgressBar.this.getPercentComplete());
                }
                String text = getString();
                g.setColor(Color.BLACK);
                g.drawString(text, 0, R.y);
            }
        }

        @Override
        public void setStringPainted(boolean b) {
            isStringToBePainted = b;
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        b.setValue(0);
        b.setStringPainted(true);
    }
}

You are forgetting to call the super.setStringPainted(b) : 您忘记了调用super.setStringPainted(b)

public void setStringPainted(boolean b) {
    super.setStringPainted(b);
    isStringToBePainted = b;
}

It's a bit unclear what you're trying to do, but you might want to look into overriding ProgressBarUI, instead of the component. 尚不清楚您要执行的操作,但您可能需要研究替代ProgressBarUI而不是组件。 Take a look at: http://docs.oracle.com/javase/6/docs/api/javax/swing/plaf/basic/BasicProgressBarUI.html 看一下: http : //docs.oracle.com/javase/6/docs/api/javax/swing/plaf/basic/BasicProgressBarUI.html

You need to call the JProgressBar setStringPainted method in your overriden method: 您需要在重写的方法中调用JProgressBar setStringPainted方法:

public class TestGame implements ActionListener
{

  static int y;
  CustomProgressBar progressBar = new CustomProgressBar();

  public static void main(String arg[]) throws Exception
  {
    new TestGame();
  }

  public TestGame() throws Exception
  {
    JFrame f = new JFrame();
    JButton btn = new JButton("Click");
    f.setExtendedState(JFrame.MAXIMIZED_BOTH);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setUndecorated(true);
    f.setLayout(new FlowLayout());
    btn.addActionListener(this);
    f.add(progressBar);
    f.add(btn);
    f.setVisible(true);
  }

  class CustomProgressBar extends JProgressBar
  {
    private static final long serialVersionUID = 1L;
    private boolean isStringToBePainted = false;

    public CustomProgressBar()
    {
      super(JProgressBar.VERTICAL, 0, 100);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
      super.paintComponent(g);
      if (isStringToBePainted)
      {
        Dimension size = this.getSize();
        if (this.getPercentComplete() < 0.9)
          TestGame.y = (int) (size.height - size.height * this.getPercentComplete());
        String text = getString();
        g.setColor(Color.BLACK);
        g.drawString(text, 0, TestGame.y);
      }
    }

    @Override
    public void setStringPainted(boolean b)
    {
      super.setStringPainted(b);
      isStringToBePainted = b;
    }
  }

  @Override
  public void actionPerformed(ActionEvent e)
  {
    progressBar.setValue(0);
    progressBar.setStringPainted(true);
  }

}

You need to call the paintAll method after setting the new isStringToBePainted 's value. 设置新的isStringToBePainted的值后,需要调用paintAll方法。 As an advice, make the progressbar wider so you can appreciate the whole percentage label. 作为建议,将进度条扩大,以便您可以欣赏整个百分比标签。 I added a simple threading routine to watch your custom component at work, here are some pictures of it: 我添加了一个简单的线程例程来监视您的自定义组件在工作,下面是其中的一些图片:

自定义进度条占进度的25%自定义进度条,进度为进度的50%自定义进度条占进度的84%

The following code contains the details of the modifications made under comments tagged as #NEW : 以下代码包含在标记为#NEW注释下进行的修改的详细信息:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class R implements ActionListener, Runnable {
    static int y;
    CustomProgressBar b = new CustomProgressBar();

    public static void main(String arg[]) throws Exception {
        new R();
    }

    public R() throws Exception {
        JFrame f = new JFrame();
        JButton btn = new JButton("Click");
        f.setExtendedState(JFrame.MAXIMIZED_BOTH);
        f.setUndecorated(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new FlowLayout());
        btn.addActionListener(this);

        // #NEW: Make the progress bar a little bit wider
        b.setPreferredSize(new Dimension(25, 250));

        f.add(b);
        f.add(btn);
        f.setVisible(true);
    }

    class CustomProgressBar extends JProgressBar {
        private static final long serialVersionUID = 1L;
        private boolean isStringToBePainted = false;

        public CustomProgressBar() {
            super(JProgressBar.VERTICAL, 0, 100);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (isStringToBePainted) {
                Dimension size = CustomProgressBar.this.getSize();
                if (CustomProgressBar.this.getPercentComplete() < 0.9) {
                    R.y = (int) (size.height - size.height * CustomProgressBar.this.getPercentComplete());
                }
                String text = getString();
                g.setColor(Color.BLACK);
                g.drawString(text, 0, R.y);
            }
        }

        @Override
        public void setStringPainted(boolean b) {
            isStringToBePainted = b;

            // #NEW: This updates the progressbar after clicking the button, thus fixing your problem
            paintAll(getGraphics());
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        b.setValue(0);
        b.setStringPainted(true);

        // #NEW: A simple thread that creates a visual of your component working 
        new Thread(this).start();
    }

    // #NEW: This is the threading routine, it only changes the progressbar value from 0 to 100 every 10th of a second  
    @Override
    public void run() {
        while (b.getPercentComplete() < 1.0) { 
            b.setValue(b.getValue() + 1);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM