11

如果我真的想做这样的事情,我可以在 JTabbedPane 选项卡中放置一个 JProgressBar(或等效的)吗?(我的意思是,不在标签本身中,

我该怎么做这样的事情?

编辑 我真的想把进度条放在标签的标题中,而不是标签本身。

这是一些ascii艺术:

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

所以它实际上是当前可见的“选项卡 2”,但我希望进度条(或等效项)在第三个选项卡的标题中可见。

编辑 2

这必须适用于 Java 1.5:这必须适用于永远不会配备 Java 6 的无数 MacOS 10.4 和 MacOS 10.5 Apple 计算机(有些会,有些不会:而且很多永远不会,这不是我的称呼)

4

2 回答 2

15

对于早期版本,您可以尝试addTab()使用 used 的合适实现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;
        }
    }
}
于 2010-08-14T16:52:25.910 回答
5

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

编辑:来自JTabbedPane JavaDoc

// 在这种情况下,自定义组件负责渲染选项卡的标题。

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

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

于 2010-08-14T13:58:59.207 回答