Java:JTabbedPane选项卡标题中的JProgressBar(或等效项)

时间:2010-08-14 13:24:01

标签: java swing jtabbedpane jprogressbar

如果我真的想做类似的事情,我可以在JTabbedPane标签中放一个JProgressBar(或它的等价物)吗? (我的意思是,不在标签本身,

我该怎么做?

编辑我确实希望将进度条放在标签的标题中,而不是标签本身。

这是一些ascii艺术:

----------------------------------------------------
|  Tab 1  || Tab   2||Tab-with-progress-bar||Tab  4|
-----------         --------------------------------
'                                                  '
'                                                  '
'                                                  '
'                                                  '
'                                                  '
'                                                  '
'                                                  '
'                                                  '
----------------------------------------------------

所以它真的是“标签2”当前可见,但我希望在第三个标签的标题中显示进度条(或等效标题)。

编辑2

这必须适用于Java 1.5:这必须适用于无数MacOS 10.4和MacOS 10.5 Apple计算机,这些计算机永远不会配备Java 6(有些人会这样做,有些人不会:很多人都不会,它会,它不是我的电话)

2 个答案:

答案 0 :(得分:15)

对于早期版本,您可以尝试使用Icon的合适实施addTab()来表示进度。

JTabbedTest

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

public class JTabbedTest {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            private final JTabbedPane jtp = new JTabbedPane();

            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                jtp.setPreferredSize(new Dimension(400, 200));
                createTab("Reds", Color.RED);
                createTab("Greens", Color.GREEN);
                createTab("Blues", Color.BLUE);

                f.add(jtp, BorderLayout.CENTER);
                f.pack();
                f.setVisible(true);
            }

            private void createTab(String name, Color color) {
                ProgressIcon icon = new ProgressIcon(color);
                jtp.addTab(name, icon, new ColorPanel(jtp, icon));
            }
        });
    }

    private static class ColorPanel extends JPanel implements ActionListener {

        private static final Random rnd = new Random();
        private final Timer timer = new Timer(1000, this);
        private final JLabel label = new JLabel("Stackoverflow!");
        private final JTabbedPane parent;
        private final ProgressIcon icon;
        private final int mask;
        private int count;

        public ColorPanel(JTabbedPane parent, ProgressIcon icon) {
            super(true);
            this.parent = parent;
            this.icon = icon;
            this.mask = icon.color.getRGB();
            this.setBackground(icon.color);
            label.setForeground(icon.color);
            this.add(label);
            timer.start();
        }

        public void actionPerformed(ActionEvent e) {
            this.setBackground(new Color(rnd.nextInt() & mask));
            this.icon.update(count += rnd.nextInt(8));
            this.parent.repaint();
        }
    }

    private static class ProgressIcon implements Icon {

        private static final int H = 16;
        private static final int W = 3 * H;
        private Color color;
        private int w;

        public ProgressIcon(Color color) {
            this.color = color;
        }

        public void update(int i) {
            w = i % W;
        }

        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.setColor(color);
            g.fillRect(x, y, w, H);
        }

        public int getIconWidth() {
            return W;
        }

        public int getIconHeight() {
            return H;
        }
    }
}

答案 1 :(得分:4)

将JProgressbar包含在JPanel中,并将该JPanel添加到JTabbedPane。

修改:来自JTabbedPane JavaDoc

  

//在这种情况下是自定义组件   负责渲染   标签的标题。

tabbedPane.addTab(null, myComponent); 
tabbedPane.setTabComponentAt(0, new JLabel("Tab"));

所以你基本上可以简单地通过对你的JProgressbar的引用替换new JLabel("Tab")(尽管不能将这个JProgressbar添加到Tab本身)。 但是,我认为在Java 1.6之前不存在这种方法。

相关问题