JProgressBar文本溢出

JProgressBar文本溢出

本文介绍了JProgressBar文本溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序在JProgressBar中写入文本.问题是文本比JProgressBar的宽度还要宽.

My program writes text in a JProgressBar. The problem is the text is wider than the JProgressBar's width.

我已经更改了JProgressBar的高度,以便能够在两行上写文本,但是我不想更改宽度.

I have already changed the JProgressBar's height to be able to write the text on two lines but I don't want to the change the width.

如果文本太宽,如何更改JProgressBar的溢出以使文本返回到下一行?

How to change the JProgressBar's overflow to make the text going back to the next line if it is too wide?

我希望这足够清楚:)

这就是我想要的:

谢谢

编辑

@mKorbel回复后,结果如下:

After @mKorbel reply the result looks like this:

标签工作得很好,但是为什么要剥离这些标签?

The label works quite fine but why those strips?

我的代码:

// Construct progress bar
JProgressBar progressBar = new JProgressBar(0, 100);
// Set progressBar color
progressBar.setForeground(new Color(0,176,80));

// Edit progress bar height
Dimension prefSize = progressBar.getPreferredSize();
prefSize.height = 50;
progressBar.setPreferredSize(prefSize);

// Set the layout
progressBar.setLayout(new BorderLayout(5, 5));

// Set progress bar value
progressBar.setValue(38);

// Construct the label
JLabel progressLabel = new JLabel("<html>I have already changed the JProgressBar's height to be able to write the text on two lines but I don't want to the change the width.</html>");
// Set alignment
progressLabel.setHorizontalAlignment(JLabel.CENTER);
progressLabel.setVerticalAlignment(JLabel.CENTER);

// Set the borders
progressLabel.setBorder(new EmptyBorder(15, 15, 15, 15));

// Change the font
font = progressLabel.getFont();
font = font.deriveFont(Font.BOLD, 12);
progressLabel.setFont(font);

// Add label to the progress bar
progressBar.add(progressLabel, BorderLayout.CENTER);

// Add progress bar to the frame
frame.add(progressBar);

推荐答案

  • 您能否提供一些有关如何执行此操作的代码? ---> JLayer& @aterai的JProgressBar ,有关更多想法,请参阅他的博客,对于Java6,您可以使用JXLayer

    • could you provide some code on how to do this? --- > JLayer & JProgressBar by @aterai, for more ideas see his blog, for Java6 you can to use JXLayer

      或使用GlassPane具有非常相似的逻辑

      or with very similair logics by using GlassPane

      一些笔记

      • 应使用GBC代替NullLayout

      • should be used GBC instead of NullLayout

      添加图标或透明背景可以变得更好

      can be nicer with added Icon or transparent background

      (通过将LayoutManager添加到JLabel),可以放置一堆JComponent,其效果与JPanel相同

      (by add LayoutManager to JLabel) there can be placed bunch of JComponents with the same effect as for JPanel

      例如

      import java.awt.Container;
      import java.awt.Dimension;
      import java.awt.FlowLayout;
      import java.awt.GridBagConstraints;
      import java.awt.event.ComponentAdapter;
      import java.awt.event.ComponentEvent;
      import javax.swing.JButton;
      import javax.swing.JCheckBox;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JProgressBar;
      import javax.swing.JRadioButton;
      import javax.swing.UIManager;
      //https://stackoverflow.com/questions/14560680/jprogressbar-low-values-will-not-be-displayed
      public class ProgressSample {
      
          private JFrame frame = new JFrame("GlassPane instead of JLayer");
          private JLabel label;
          private GridBagConstraints gbc = new GridBagConstraints();
          private JProgressBar progressSeven;
      
          public ProgressSample() {
              frame.setLayout(new FlowLayout());
              frame.add(new JButton("test"));
              frame.add(new JCheckBox("test"));
              frame.add(new JRadioButton("test"));
              // Nothing is displayed if value is lover that 6
              JProgressBar progressSix = new JProgressBar(0, 100);
              progressSix.setValue(2);
              frame.add(progressSix);
              // but this works value is higher that 6
              progressSeven = new JProgressBar(0, 100);
              progressSeven.addComponentListener(new ComponentAdapter() {
                  @Override
                  public void componentMoved(ComponentEvent e) {
                      label.setBounds(
                              (int) progressSeven.getBounds().getX(),
                              (int) progressSeven.getBounds().getY(),
                              label.getPreferredSize().width,
                              label.getPreferredSize().height);
                  }
              });
              progressSeven.setValue(7);
              frame.add(progressSeven);
              label = new JLabel();
              label.setText("<html> Concurency Issues in Swing<br>"
                      + " never to use Thread.sleep(int) <br>"
                      + " durring EDT, simple to freeze GUI </html>");
              label.setPreferredSize(new Dimension(label.getPreferredSize().width, label.getPreferredSize().height));
              Container glassPane = (Container) frame.getRootPane().getGlassPane();
              glassPane.setVisible(true);
              glassPane.setLayout(null);
              glassPane.add(label, gbc);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.pack();
              frame.setVisible(true);
          }
      
          public static void main(String[] args) {
              try {
                  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
              } catch (Exception e) {
                  e.printStackTrace();
              }
              ProgressSample dialogTest = new ProgressSample();
          }
      }
      

      编辑

      • 评论

      • 结果是将JProgressBar用作Container,放在正确的LayoutManager位置,并在JLabel之前覆盖JProgressBar
        • result is to use JProgressBar as Container, put there proper LayoutManager, overlay JProgressBar by JLabel
          • 增强功能,可将EmptyBorder设置为JLabel,例如label.setBorder(new EmptyBorder(15, 15, 15, 15));
          • enhancements, to set EmptyBorder for JLabel, e.g. label.setBorder(new EmptyBorder(15, 15, 15, 15));

          EDIT2(图标也可以是半透明的,可以覆盖JProgressBar)

          EDIT2 (Icon is, can be semi_transparent too, can overlay JProgressBar)

          代码可能类似于

          import java.awt.BorderLayout;
          import java.awt.Dimension;
          import java.awt.EventQueue;
          import javax.swing.JFrame;
          import javax.swing.JLabel;
          import javax.swing.JProgressBar;
          import javax.swing.UIManager;
          import javax.swing.border.EmptyBorder;
          
          public class JProgressBarWithJLabel {
          
              private JFrame frame = new JFrame("JLabel in JProgressBar");
              private JLabel label;
              private JProgressBar progressSeven;
          
              public JProgressBarWithJLabel() {
                  progressSeven = new JProgressBar(0, 100){
                      @Override
                      public Dimension getPreferredSize() {
                          return new Dimension(300, 60);
                      }
                  };
                  progressSeven.setValue(38);
                  progressSeven.setLayout(new BorderLayout(5, 5));
                  label = new JLabel();
                  label.setHorizontalTextPosition(JLabel.CENTER);
                  label.setVerticalTextPosition(JLabel.CENTER);
                  label.setBorder(new EmptyBorder(15, 15, 15, 15));
                  label.setText("<html>I have already changed the JProgressBar's height "
                          + "to be able to write the text on two lines but I don't want "
                          + "to the change the width.</html>");
                  progressSeven.add(label, BorderLayout.CENTER);
                  frame.add(progressSeven);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.pack();
                  frame.setVisible(true);
              }
          
              public static void main(String[] args) {
                  try {
                      for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                          if ("Nimbus".equals(laf.getName())) {
                              UIManager.setLookAndFeel(laf.getClassName());
                          }
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  EventQueue.invokeLater(new Runnable() {
                      @Override
                      public void run() {
                          new JProgressBarWithJLabel();
                      }
                  });
              }
          }
          

          WindowsClassicLookAndFeel的默认剥离(图标不是半透明的)

          default stripping for WindowsClassicLookAndFeel (Icon isn't semi_transparent)

          这篇关于JProgressBar文本溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:47