今天,在浏览各种问题时,我遇到了一个问题,在我看来有点不可思议,为什么一个人要在上面加上JPanela JLabel,对于这种情况会不会有什么真正的原因,所以这只是微不足道的吗?
JPanel
JLabel
动画图像作为GUI的BG。我使用HTML来调整此尺寸(x3),但是如果它已经是所需的尺寸,则可以直接将其设置为Icon标签的。
Icon
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(); } }); } }
不知道它是否是“真正的”。这似乎是一个主观术语,需要更多说明。我从来没有使用过这种方法,只是想通了,今晚摸索着。;)