Java:在JTabbedPane选项卡标题中使用JProgressBar(或等效控件)

11
如果我真的想做那样的事情,我可以在JTabbedPane选项卡中放置JProgressBar(或它的等效物)吗?(我的意思是不在选项卡本身中放置,

我应该怎么做才能实现这样的效果?

编辑 我真的想将进度条放在选项卡标题中,而不是选项卡本身。

下面是一些ASCII艺术:

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

目前可见的是“标签2”,但我想让进度条(或类似的东西)在第三个标签的标题中可见。

编辑2

这必须在Java 1.5上工作:这必须能够在无数台MacOS 10.4和MacOS 10.5的苹果电脑上工作,这些电脑将永远不会配备Java 6(一些有,一些没有:而且很多永远也不会有,这不是我的决定)。

2个回答

16

对于较早的版本,您可以尝试使用addTab()方法和适当的Icon实现来指示进度。

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;
        }
    }
}

2
那真的把我“杀”了,棒极了 +1 - mKorbel

6
将JProgressbar放在JPanel中,并将该JPanel添加到JTabbedPane中。
编辑:来自JTabbedPane JavaDoc
// 在这种情况下,自定义组件负责渲染选项卡的标题。
tabbedPane.addTab(null, myComponent); 
tabbedPane.setTabComponentAt(0, new JLabel("Tab"));

所以基本上您可以通过将new JLabel("Tab")替换为对JProgressbar的引用来完成,尽管此JProgressbar不必添加到选项卡本身中。 但是,我认为在Java 1.6之前不存在这种方法。

我编辑了我的问题,对于第一次措辞感到抱歉。如果我想要一个进度选项卡在选项卡标题中,您的答案是否有效? - NoozNooz42
JavaDoc 没有给我太多信息,原因是 Java 1.6 是一个大忌。这必须在 MacOS X 10.4 和 MacOS X 10.5 上工作,而一些配备了这些系统的苹果电脑将永远不会拥有 Java 1.6 :) - NoozNooz42

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接