我想添加一個圖像到一個標籤,所以它看起來像一個圖標。我想把一個PNG圖像放在標籤上(查看圖片)
是否有可能在java中做到這一點?如何在一個標籤中放置圖像,使其看起來像一個圖標
0
A
回答
4
JTabbedPane
允許您提供一個組件作爲標籤「渲染器」(各種)。
查看JTabbedPane#setTabComponentAt瞭解更多詳情,並查看this example瞭解更多詳情。
已更新,例如
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;
}
}
2
JTabbedPane中具有API設置到標籤的圖標,加入的標籤內容或更高時或者:
// when adding
tabbedPane.addTab(String, Icon, Component);
// after having added
tabbedPane.setIconAt(int, Icon);
+0
+1但注意一次,我試過我有一個問題與SwingUtilities.layoutLabel()???保護尺寸,甚至圖標有適當H&W以像素爲單位 – mKorbel
相關問題
- 1. 在圖像中放置一個標記
- 2. 如何在點擊後使標籤看起來像一個標籤
- 3. 如何使TextView看起來像一個設置的PreferenceCategory標頭
- 4. 在imageview中設置圖像,使其看起來像一個進度條
- 5. 將一個背景圖像放在一個div標籤中
- 6. 如何刪除SWT按鈕的邊框,使其看起來像一個標籤
- 7. TabItem的標題看起來就像一個標籤
- 8. 如何使IKImageBrowserView Quick Look圖標看起來像Finder圖標?
- 9. ImageView,如何設置圖像看起來在另一個圖像的前面?
- 10. Magnific-Popup Gallery在一個標籤中有兩個圖像標籤
- 11. Html在圖像的底部放置一個標籤
- 12. 圖像看起來像一個破碎的圖像
- 13. 如何在一個矩形內部放置一個標籤圖
- 14. 添加圖標的圖像看起來
- 15. 如何讓我的jlabel圖標看起來像桌面圖標?
- 16. 我想創建一個看起來像烤麪包的標籤
- 17. 如何僅使用一個圖像標籤顯示多個圖像?
- 18. 如何在UILabel中插入圖像,或使其看起來像我一樣?
- 19. 如何使圖像看起來像一個畫布或與php imagick圖書館
- 20. 如何使文本或html標籤看起來像一個按鈕
- 21. 使圖像看起來像iOS圖標programmaticaly
- 22. Android:在另一個圖像上放置一個圖像
- 23. 如何在圖像視圖中設置圖像的標籤?
- 24. 整個身體看起來像一個圖像,但有文字
- 25. 收拾一個圖像標籤
- 26. 在圖像上居中放置一個圖層查看
- 27. 在android中的另一個圖像視圖中放置圖像
- 28. 如何在另一個圖像的圖像標籤內添加一個src圖像路徑rails
- 29. 如何在背景圖像上放置一個css圖像庫
- 30. 如何顯示一個圖像查看另一個圖像?
HMM .. 。在屏幕截圖中看不到圖標/圖片 - 爲什麼tabbedPane api無法設置適合您需要的圖標? – kleopatra