Noo*_*z42 10 java swing jtabbedpane jprogressbar
如果我真的想做类似的事情,我可以在JTabbedPane选项卡中放置一个JProgressBar(或它的等价物)吗?(我的意思是,不在标签本身,
我该怎么办呢?
编辑 我真的想把进度条放在标签的标题中,而不是标签本身.
这是一些ascii艺术:
----------------------------------------------------
| Tab 1 || Tab 2||Tab-with-progress-bar||Tab 4|
----------- --------------------------------
' '
' '
' '
' '
' '
' '
' '
' '
----------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
因此,它实际上是"选项卡2",目前可见,但我希望在第三个选项卡的标题中显示进度条(或等效项).
编辑2
这必须适用于Java 1.5:这必须适用于无数MacOS 10.4和MacOS 10.5 Apple计算机,这些计算机永远不会配备Java 6(有些人会这样做,有些人不会这样做:而且很多人都不会这样做,这不是我的呼叫)
tra*_*god 15
对于早期版本,您可以尝试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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
将 JProgressbar 包含在 JPanel 中并将该 JPanel 添加到 JTabbedPane。
编辑:从JTabbedPane JavaDoc:
// 在这种情况下,自定义组件负责呈现选项卡的标题。
tabbedPane.addTab(null, myComponent);
tabbedPane.setTabComponentAt(0, new JLabel("Tab"));
Run Code Online (Sandbox Code Playgroud)
所以你基本上可以简单地替换new JLabel("Tab")为对你的 JProgressbar 的引用(尽管这个 JProgressbar 不能添加到选项卡本身)。但是,我认为这种方法在 Java 1.6 之前不存在。