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

发布于 2024-09-14 16:34:43 字数 1038 浏览 8 评论 0原文

如果我真的想做类似的事情,我可以将 JProgressBar (或等效的)放在 JTabbedPane 选项卡中吗? (我的意思是,不是在选项卡本身中,

我该如何做类似的事情?

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

这是一些 ASCII 艺术:

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

)在第三个选项卡的 标题 中可见。

所以当前确实是“选项卡2

”,但我希望进度条(或同等内容 在 Java 1.5 上工作:这必须在无数永远不会配备 Java 6 的 MacOS 10.4 和 MacOS 10.5 Apple 计算机上工作(有些配备,有些不配备:而且很多永远不会配备,这不是我的决定)

If I really wanted to do something like that, can I put a JProgressBar (or it's equivalent) in a JTabbedPane tab? (I mean, not in the tab itself,

How would I do something like that?

EDIT I really do want to put the progressbar in the title of the tab, not the tab itself.

Here's some ascii art:

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

So it's really "tab 2" that is currently visible, but I want the progress bar (or equivalent) to be visible in the third tab's title.

EDIT 2

This has to work on Java 1.5: this has to work on the countless MacOS 10.4 and MacOS 10.5 Apple computers that will never be equipped with Java 6 (some do, some don't: and quite a lot never will, it is not my call)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

無處可尋 2024-09-21 16:34:43

对于早期版本,您可以尝试 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;
        }
    }
}

For earlier versions, you might try addTab() with a suitable implementation of Icon used to indicate progress.

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;
        }
    }
}
┈┾☆殇 2024-09-21 16:34:43

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

Enclose the JProgressbar in a JPanel and add that JPanel to the JTabbedPane.

Edit: From the JTabbedPane JavaDoc:

// In this case the custom component
is responsible for rendering the
title of the tab.

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

So you could basically simply replace new JLabel("Tab") by a reference to your JProgressbar (though this JProgressbar must not be added to the Tab itself).
However, I think this method doesn't exist prior to Java 1.6.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文