gpt4 book ai didi

java - 如何将图像放置在选项卡中,使其看起来像图标

转载 作者:行者123 更新时间:2023-12-01 07:09:04 24 4
gpt4 key购买 nike

我正在尝试将图像添加到选项卡,使其看起来像一个图标。我想在选项卡上放置一个 png 图像(检查图像)they are those 4 tabs within a tabbed pane
java 可以做到这一点吗?

最佳答案

JTabbedPane 允许您提供一个组件来充当选项卡“渲染器”(某种形式)。

看看JTabbedPane#setTabComponentAt欲了解更多详情,请查看this example了解更多详情。

已更新示例

enter image description here

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

关于java - 如何将图像放置在选项卡中,使其看起来像图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17648780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com