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");
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());
/* use the getScaledInstance() to scale the image */ Image image = Toolkit.getDefaultToolkit().getImage(resource); // is more efficient than ImageIO.read() ImageIcon imageIcon = new ImageIcon(image.getScaledInstance( desktopPaneWidth, desktopPaneHeight, Image.SCALE_FAST)); backLabel.setIcon(imageIcon); backLabel.setBounds(0, 0, imageIcon.getIconWidth(), imageIcon.getIconHeight());
// /* use the AffineTransform to scale the image (it turns out that // * this method is less efficient than getScaledInstance()) */ // BufferedImage bufferedImage; // try { // bufferedImage = ImageIO.read(resource); // BufferedImage bufferedImageScaled = ImageScale.scale( // bufferedImage, desktopPaneWidth, desktopPaneHeight, 1); // ImageIcon imageIcon = new ImageIcon(bufferedImageScaled); // backLabel.setIcon(imageIcon); // backLabel.setBounds(0, 0, imageIcon.getIconWidth(), // imageIcon.getIconHeight()); // } catch (IOException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // }
网上找个方法 我通过Jpanel.setBackgroundPainter 显示无此方法
//按钮背景设置
JButton jbG1;
//静态方式设置背景图片
jbG1 = new JButton(new ImageIcon("imgs//BtList_.png"));
//动态方式更改背景图片
jbG1.setIcon(new ImageIcon(("imgs//BtList.png"))); 如果不行,点此发邮件我
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());
}
private class MyComponentListener extends ComponentAdapter { @Override
public void componentResized(ComponentEvent e) {
// TODO Auto-generated method stub
dimensionCurrent = getSize();
System.out.println("componentResized: " + dimensionCurrent);
desktopPaneWidth = dimensionCurrent.width - insets.left - insets.right;
desktopPaneHeight = dimensionCurrent.height -
panel.getPreferredSize().height - insets.top - insets.bottom;
System.out.println("desktopPaneWidth: " + desktopPaneWidth +
"\tdesktopPaneHeight: " + desktopPaneHeight);
/* use the getScaledInstance() to scale the image */
Image image = Toolkit.getDefaultToolkit().getImage(resource); // is more efficient than ImageIO.read()
ImageIcon imageIcon = new ImageIcon(image.getScaledInstance(
desktopPaneWidth, desktopPaneHeight, Image.SCALE_FAST));
backLabel.setIcon(imageIcon);
backLabel.setBounds(0, 0, imageIcon.getIconWidth(),
imageIcon.getIconHeight());
// /* use the AffineTransform to scale the image (it turns out that
// * this method is less efficient than getScaledInstance()) */
// BufferedImage bufferedImage;
// try {
// bufferedImage = ImageIO.read(resource);
// BufferedImage bufferedImageScaled = ImageScale.scale(
// bufferedImage, desktopPaneWidth, desktopPaneHeight, 1);
// ImageIcon imageIcon = new ImageIcon(bufferedImageScaled);
// backLabel.setIcon(imageIcon);
// backLabel.setBounds(0, 0, imageIcon.getIconWidth(),
// imageIcon.getIconHeight());
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
panelInternal.setBounds(0, 0, desktopPaneWidth, desktopPaneHeight);
}
} private class ButtonPersonelActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub if (internalFramePersonel == null
|| internalFramePersonel.isClosed()) {
JInternalFrame[] allFrames = desktopPane.getAllFrames();
int titleBarHight = 30 * allFrames.length;
int x = titleBarHight + 10;
int y = x;
int width = 250;
int height = 180;
internalFramePersonel = new JInternalFrame("人事管理", true, true,
true, true);
internalFramePersonel.setVisible(true);
internalFramePersonel.setBounds(x, y, width, height);
desktopPane.add(internalFramePersonel);
} try {
// indispensable for the visualization of the internal frame
internalFramePersonel.setSelected(true);
} catch (PropertyVetoException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} }
} private class ButtonCountActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub if (internalFrameCount == null || internalFrameCount.isClosed()) {
JInternalFrame[] allFrames = desktopPane.getAllFrames();
int titleBarHight = 30 * allFrames.length;
int x = titleBarHight + 10;
int y = x;
int width = 250;
int height = 180;
internalFrameCount = new JInternalFrame("帐套管理", true, true,
true, true);
internalFrameCount.setVisible(true);
internalFrameCount.setBounds(x, y, width, height);
URL resource = this.getClass().getResource("/images/Luxun.jpg");
BufferedImage imageScaled = null;
BufferedImage in;
try {
in = ImageIO.read(resource);
imageScaled = ImageScale.scale(in, 0.05, 0.05, 1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ImageIcon imageIcon = new ImageIcon(imageScaled);
internalFrameCount.setFrameIcon(imageIcon);
desktopPane.add(internalFrameCount);
} try {
// indispensable for the visualization of the internal frame
internalFrameCount.setSelected(true);
} catch (PropertyVetoException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} }
} private class ButtonPaymentActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub if (internalFramePayment == null || internalFramePayment.isClosed()) {
JInternalFrame[] allFrames = desktopPane.getAllFrames();
int titleBarHight = 30 * allFrames.length;
int x = titleBarHight + 10;
int y = x;
int width = 250;
int height = 180;
internalFramePayment = new JInternalFrame("待遇管理", true, true,
true, true);
internalFramePayment.setVisible(true);
internalFramePayment.setBounds(x, y, width, height);
URL resource = this.getClass().getResource("/images/Luxun.jpg");
BufferedImage imageScaled = null;
BufferedImage in;
try {
in = ImageIO.read(resource);
imageScaled = ImageScale.scale(in, 0.05, 0.05, 1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ImageIcon imageIcon = new ImageIcon(imageScaled);
internalFramePayment.setFrameIcon(imageIcon);
desktopPane.add(internalFramePayment);
} try {
// indispensable for the visualization of the internal frame
internalFramePayment.setSelected(true);
} catch (PropertyVetoException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JInternalFrame_1 frame = new JInternalFrame_1();
frame.setTitle("企业人事管理系统 ");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 570, 470);
/* obtain the frame border */
insets = frame.getInsets();
System.out.println(insets);
System.out.println(desktopPane.getSize());
System.out.println(container.getSize());
System.out.println(frame.getSize());
System.out.println(panel.getSize());
}}
如果你用netbeans,直接拖啊。