日期:2014-05-20 浏览次数:20895 次
//按钮背景设置 JButton jbG1; //静态方式设置背景图片 jbG1 = new JButton(new ImageIcon("imgs//BtList_.png")); //动态方式更改背景图片 jbG1.setIcon(new ImageIcon(("imgs//BtList.png")));
------解决方案--------------------
package com.han; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Image; import java.awt.Insets; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.image.BufferedImage; import java.beans.PropertyVetoException; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * 有几个技术改进:<p> * 1. 在desktopPane的背景图片上再加上button等组件<p> * 2. desktopPane的背景图片大小随着整个窗体的大小变化而变化,使图片始终能够铺满整个区域<p> * 3. 对于desktopPane上的所有组件不使用绝对布局而是在backLabel存在的情况下还使用布局管理器 * @author HAN * */ @SuppressWarnings("serial") public class JInternalFrame_1 extends JFrame { JInternalFrame internalFramePersonel = null; JInternalFrame internalFrameCount = null; JInternalFrame internalFramePayment = null; JPanel panelInternal; static JDesktopPane desktopPane = null; static JPanel panel; static Container container; static Dimension dimensionCurrent; static Insets insets; // the frame border int desktopPaneWidth; int desktopPaneHeight; static JLabel backLabel; URL resource; // the backLabel image URL resource public JInternalFrame_1() { // TODO Auto-generated constructor stub panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JButton buttonPersonel = new JButton("人事管理"); JButton buttonCount = new JButton("帐套管理"); JButton buttonPayment = new JButton("待遇管理"); panel.add(buttonPersonel); panel.add(buttonCount); panel.add(buttonPayment); container = getContentPane(); container.add(panel, BorderLayout.NORTH); desktopPane = new JDesktopPane(); container.add(desktopPane, BorderLayout.CENTER); resource = this.getClass().getResource("/images/LightHouse.jpg"); ImageIcon imageIcon = new ImageIcon(resource); backLabel = new JLabel(imageIcon); backLabel.setBounds(0, 0, imageIcon.getIconWidth(), imageIcon.getIconHeight()); // "new Integer(Integer.MIN_VALUE)" ensures that its layer is always // under the others. desktopPane.add(backLabel, new Integer(Integer.MIN_VALUE)); desktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); panelInternal = new JPanel(); // Creates a new JPanel with a double buffer and a flow layout. panelInternal.setBounds(0, 0, 300, 300); System.out.println(panelInternal.isOpaque()); panelInternal.setOpaque(false); System.out.println(desktopPane.getWidth() + "\t" + desktopPane.getHeight()); JButton buttonInternal = new JButton("buttonInternal"); JButton testButton = new JButton("testButton"); testButton.setBounds(200, 0, 100, 40); buttonInternal.setBounds(0, 0, 100, 40); // 设置有布局管理器后,setBounds()就失效了。换句话说,setBounds()只有在没有布局管理器的情况下才有效。 panelInternal.add(testButton); panelInternal.add(buttonInternal); desktopPane.add(panelInternal, new Integer(1)); // the reason for non-using the inner hidden class is that the variable // that it will use is must be "final", // and this causes it can not be assigned in the inner hidden class. // So we use the inner class instead, which is parallel to the level // "method". buttonPersonel.addActionListener(new ButtonPersonelActionListener()); buttonCount.addActionListener(new ButtonCountActionListener()); buttonPayment.addActionListener(new ButtonPaymentActionListener()); // System.out.println(panel.getWidth() + "\t" + panel.getHeight()); System.out.println(panel.getPreferredSize()); System.out.println(desktopPane.getPreferredSize()); addComponentListener(new MyComponentListener()); }