如何将图像放置在选项卡中,使其看起来像一个图标 [英] How to place image in a tab so that it looks like an icon

查看:32
本文介绍了如何将图像放置在选项卡中,使其看起来像一个图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像添加到选项卡,使其看起来像一个图标.我想在选项卡上放置一个 png 图像(检查图像)
可以在java中做到这一点吗?

I am trying to add an image to a tab so it looks like an icon.I want to put a png image on the tab (check image)
Is it possible to do this in java?

推荐答案

jtabbedpane 允许您提供一个组件以充当标签渲染器"(各种).

JTabbedPane allows you to provide a component to act as the tab "renderer" (of sorts).

看看JTabbedPane#setTabComponentAt 了解更多详情并查看 此示例 了解更多详情.

Take a look at JTabbedPane#setTabComponentAt for more details and check out this example for more details.

更新示例

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTabbedPaneIcon {

    public static void main(String[] args) {
        new TestTabbedPaneIcon();
    }

    public TestTabbedPaneIcon() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTabbedPane tp = new JTabbedPane();
                tp.addTab("Dates", new JPanel());
                tp.addTab("Deliveries", new JPanel());
                tp.addTab("Exports", new JPanel());

                tp.setTabComponentAt(0, getLabel("Dates", "/Icon03.png"));
                tp.setTabComponentAt(1, getLabel("Deliveries", "/Icon01.png"));
                tp.setTabComponentAt(2, getLabel("Exports", "/Icon02.png"));

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(tp);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected JLabel getLabel(String title, String icon) {
        JLabel label = new JLabel(title);
        try {
            label.setIcon(new ImageIcon(ImageIO.read(getClass().getResource(icon))));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return label;
    }
}

这篇关于如何将图像放置在选项卡中,使其看起来像一个图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆