JProgressBar.stringPainted(真);不管用

时间:2014-08-20 22:02:22

标签: java swing paintcomponent jprogressbar

这是我的java代码的一部分,在这段代码中我写过,当我点击按钮时,JProgressBar的值应变为0stringPainted()返回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);
    }
}

3 个答案:

答案 0 :(得分:0)

您忘记拨打super.setStringPainted(b)

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

您有点不清楚自己要做什么,但是您可能希望调查重写ProgressBarUI而不是组件。看看:http://docs.oracle.com/javase/6/docs/api/javax/swing/plaf/basic/BasicProgressBarUI.html

答案 1 :(得分:0)

您需要在覆盖的方法中调用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);
  }

}

答案 2 :(得分:0)

您需要在设置新isStringToBePainted的值后调用paintAll方法。作为建议,使进度条更宽,以便您可以欣赏整个百分比标签。我添加了一个简单的线程例程来观察你的自定义组件,下面是它的一些图片:

The custom progressbar at 25% of progress The custom progressbar at 50% of progress The custom progressbar at 84% of progress

以下代码包含在标记为#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();
            }
        }
    }
}