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

标签 java swing jtabbedpane jprogressbar

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

最佳答案

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

关于Java:JTabbedPane 选项卡标题中的 JProgressBar(或等效项),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3483485/

相关文章:

java - 没有使用 Spring Security 进行身份验证和授权

java - 使用回车键在java中激活一个按钮

java - JButton 视觉故障

java - 如何使用 WindowBuilder 将选项卡添加到 JTabbedPane

java - swing - 如何设置为选项卡式 Pane 分配选项卡按钮?

java - 使用 Java 命名更改可分辨名称

java - 游标状态无效 - 插入行时没有当前行

java - 随机类不适用于 Android 4.1+

Java:语句未按顺序执行

java - 将列从一个 jtable 复制到另一个 jtable