为什么要将JPanel添加到JLabel,在什么情况下会出现这种情况? [英] Why to add JPanel to a JLabel, under what circumstance, this situation can arise?
问题描述
今天在浏览各种问题时,我遇到了一个问题,这在我看来有点奇怪,为什么会有一个想要一个 JPanel
添加到 JLabel
,是否有任何真正的理由可以出现这种情况,所以它是只是微不足道?
Today while surfing through various questions, I encountered one QUESTION, this seems to me a bit weird, why would one wants to add a JPanel
to a JLabel
, are there any genuine reasons as to such situation can arise, so is it just trivial ?
推荐答案
动画图像作为GUI的BG。我使用HTML来调整这个(x3),但如果它已经是所需的大小,你可以直接将它设置为标签的 Icon
。
An animated image as a BG for the GUI. I use HTML to resize this one (x3), but if it is already the desired size, you could set it directly as the Icon
of the label.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
class LabelAsBackground {
public static final String HTML =
"<html>" +
"<style type'text/css'>" +
"body, html { padding: 0px; margin: 0px; }" +
"</style>" +
"<body>" +
"<img src='http://pscode.org/media/starzoom-thumb.gif'" +
" width=320 height=240>" +
"";
LabelAsBackground() {
JFrame f = new JFrame("Animated Image BG");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JLabel contentPane = new JLabel(HTML);
contentPane.setLayout(new GridLayout());
JPanel gui = new JPanel(new GridLayout(3,3,15,15));
gui.setOpaque(false);
contentPane.add(gui);
gui.setBorder(new EmptyBorder(20,20,20,20));
for (int ii=1; ii<10; ii++) {
gui.add( new JButton("" + ii));
}
f.setContentPane(contentPane);
f.pack();
//f.setResizable(false); // uncomment to see strange effect..
f.setVisible(true);
}
public static void main(String[] args) {
//Create the frame on the event dispatching thread
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
LabelAsBackground lab = new LabelAsBackground();
}
});
}
}
不确定它是否是正版。这似乎是一个主观术语,需要进行大量澄清。我从来没有使用过这种方法,只是想出来,今晚摸索着。 ;)
Not sure if it is 'genuine' or not. That seems a subjective term that requires much clarification. I've never used this method and only just figured it out, fumbling around tonight. ;)
这篇关于为什么要将JPanel添加到JLabel,在什么情况下会出现这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!