是否可以在垂直JProgressbar中保存水平文本

时间:2013-06-05 07:32:09

标签: java swing jprogressbar

JProgressBar模式下的

JProgressBar.VERTICAL会轮换绘制String

是否可以保存字符串HORIZONTAL


*JProgressBar.VERTICAL*

3 个答案:

答案 0 :(得分:4)

这是true - 哈克,但如果对我有用:

如果UI在打印标签时询问方向,我会在堆栈中查找,如果是这样,我会返回另一个方向。

        JProgressBar progress = new JProgressBar(){
            @Override
            public int getOrientation() {
                for( StackTraceElement elem : new Throwable().getStackTrace() ) {
                    if(elem.getMethodName().equals( "paintText" ) || (elem.getMethodName().equals( "paintString" ))) {
                        return JProgressBar.HORIZONTAL;
                    }
                }
                return JProgressBar.VERTICAL;
            }
        };

答案 1 :(得分:4)

  • 另一种选择是使用BorderLayout + JLabel
  • 或者如果您使用的是Java 1.7.0,则可以使用JLayer

enter image description here

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.LayerUI;

public class ProgressBarLabelTest {
  private static JProgressBar makeProgressBar1(BoundedRangeModel model) {
    JProgressBar progressBar = new JProgressBar(model) {
      private JLabel label = new JLabel("000/100", SwingConstants.CENTER);
      private ChangeListener changeListener = null;
      @Override public void updateUI() {
        removeAll();
        if(changeListener!=null) removeChangeListener(changeListener);
        super.updateUI();
        EventQueue.invokeLater(new Runnable() {
          @Override public void run() {
            setLayout(new BorderLayout());
            changeListener = new ChangeListener() {
              @Override public void stateChanged(ChangeEvent e) {
                int iv = (int)(100 * getPercentComplete());
                label.setText(String.format("%03d/100", iv));
              }
            };
            addChangeListener(changeListener);
            add(label);
            label.setBorder(BorderFactory.createEmptyBorder(0,4,0,4));
          }
        });
      }
      @Override public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        Insets i = label.getInsets();
        d.width = label.getPreferredSize().width + i.left + i.right;
        return d;
      }
    };
    initProgressBar(progressBar);
    return progressBar;
  }
  private static JComponent makeProgressBar2(BoundedRangeModel model) {
    final JLabel label = new JLabel("000/100");
    label.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));
    LayerUI<JProgressBar> layerUI = new LayerUI<JProgressBar>() {
      private final JPanel rubberStamp = new JPanel();
      @Override public void paint(Graphics g, JComponent c) {
        super.paint(g, c);
        Dimension d = label.getPreferredSize();
        int x = (c.getWidth()  - d.width)  / 2;
        int y = (c.getHeight() - d.height) / 2;
        JLayer jlayer = (JLayer)c;
        JProgressBar progress = (JProgressBar)jlayer.getView();
        int iv = (int)(100 * progress.getPercentComplete());
        label.setText(String.format("%03d/100", iv));
        SwingUtilities.paintComponent(
          g, label, rubberStamp, x, y, d.width, d.height);
      }
    };
    JProgressBar progressBar = new JProgressBar(model) {
      @Override public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        Insets i = label.getInsets();
        d.width = label.getPreferredSize().width + i.left + i.right;
        return d;
      }
    };
    initProgressBar(progressBar);
    return new JLayer<JProgressBar>(progressBar, layerUI);
  }
  public JComponent makeUI() {
    final BoundedRangeModel m = new DefaultBoundedRangeModel();
    JProgressBar progressBar0 = new JProgressBar(m);
    initProgressBar(progressBar0);
    progressBar0.setStringPainted(true);
    JPanel p = new JPanel();
    p.add(progressBar0);
    p.add(makeProgressBar1(m));
    p.add(makeProgressBar2(m));
    p.add(new JButton(new AbstractAction("+10") {
      private int i = 0;
      @Override public void actionPerformed(ActionEvent e) {
        m.setValue(i = (i>=100) ? 0 : i + 10);
      }
    }));
    return p;
  }
  private static void initProgressBar(JProgressBar progressBar) {
    progressBar.setOrientation(SwingConstants.VERTICAL);
    progressBar.setStringPainted(false);
    progressBar.setForeground(Color.GREEN);
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() { createAndShowGUI(); }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new ProgressBarLabelTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

答案 2 :(得分:3)

您可以在自定义paintString()中覆盖BasicProgressBarUI。我们看到了一个相关的例子here