在java选项卡中添加东西_在JTabbedPane选项卡标题中的Java:JProgressBar(或等价物)...

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值