【问题标题】:Java: JProgressBar (or equivalent) in a JTabbedPane tab titleJava:JTabbedPane 选项卡标题中的 JProgressBar(或等效项)
【发布时间】:2011-03-29 20:23:01
【问题描述】:

如果我真的想做这样的事情,我可以在 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 计算机(有些会,有些不会:而且很多永远不会,它不是我的电话)

【问题讨论】:

    标签: java swing jtabbedpane jprogressbar


    【解决方案1】:

    将 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 之前是不存在的。

    【讨论】:

    • 我编辑了我的问题,很抱歉第一个措辞。如果我想要标签标题中的进度标签,你的回答会起作用吗?
    • JavaDoc 没有给我太多帮助,这是有原因的:Java 1.6 是一个很大的禁忌。这必须在 MacOS X 10.4 和 MacOS X 10.5 上运行,几台配备这些的 Apple 计算机将永远拥有 Java 1.6 :)
    【解决方案2】:

    对于早期版本,您可以尝试使用addTab() 与用于指示进度的Icon 的适当实现。

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

    【讨论】:

    • 那真的是 kille*d me, 优秀的 +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多