日期:2014-05-20  浏览次数:20902 次

java setLayout(null) 的错误
........
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
MainFrameTest frame = new MainFrameTest();
frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}

/**
 * Create the frame.
 */
public MainFrameTest()
{

BackGroundPanel background_panel = new BackGroundPanel(1144, 712);
this.initComponents();
// this.initDate();
// setSize(1024, 789);

Toolkit toolkit = getToolkit(); // 不懂啊?怎么不是 new 呢?
Dimension dimension = toolkit.getScreenSize();

int screenHeight = dimension.height;
int screenWidth = dimension.width;
int frameHeight = this.getHeight();
int frameWidth = this.getWidth();
this.setLocation((screenWidth - frameWidth) / 2,
(screenHeight - frameHeight) / 2);

this.setComponentsPosition();

}.......
//////////////////////////////////////////////////////
private void setBackgroundSize()
{

int frameWidth = this.getWidth();
int frameHeight = this.getHeight();
int modifyFrameHeight = frameHeight - menuBar.getHeight() - 30;
//System.out.println("frameWdith = " + frameWidth);
//System.out.println("frameWidth = " + frameWidth);
//System.out.println("modifyFrameHeight = " + modifyFrameHeight);
background_panel.setBounds(0, 0, frameWidth, modifyFrameHeight);

}

private void initDate()
{

}

private void setComponentsPosition()
{
 
Component[] component = fastPanel.getComponents();

background_panel.setLayout(null);
fastPanel.add(background_panel, new Integer(Integer.MAX_VALUE)); // 将背景面板添加到原来的fastPanel面板上
this.setBackgroundSize();
for (int i = 0; i < component.length; i++)
{

fastPanel.remove(component[i]);
background_panel.add(component[i]);
component[i].setBounds(component[i].getX(), component[i].getY(),
component[i].WIDTH, component[i].getHeight());

}

}
////////////////////////////////////////////////////////////////////////////////
package com.MainFrame;

import java.awt.Graphics;

import javax.swing.ImageIcon;

import java.awt.Image;

import java.net.URL;

import javax.swing.JPanel;

public class BackGroundPanel extends JPanel
{
int height;
int width;
Image image;
public BackGroundPanel(int w, int h)
{
URL url = BackGroundPanel.class.getResource("/res/背景.jpg");
image = new ImageIcon(url).getImage();
this.initComponents();     //  调用类中的方法
this.height = h;
this.width = w;
}
public void setWidth(int w)
{
this.width = w;
}
public int getWidth()
{
return this.width;
}
public void setHeight(int h)
{
this.height = h;
}
public int getHeight()
{
return this.height;
}
protected void paintComponent(Graphics g)    // 与paintComponent的区别
{
super.paintComponent(g);
Graphics g2 = (Graphics)g;
g2.drawImage(image, 0, 0, width, height, this);
}
private void initComponents()
{
setLayout(new java.awt.BorderLayout());    // 这个是神马意思呢
//this.paintComponent()
}
}
////////////////////////////////////////////////////////////////////////////////
package com.swtdesigner;

/*
public class SwingResourceManager
{

}
*/

import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.ImageIcon;

/**
 * Utility class for managing resources such as colors, fonts, images, etc.
 * 
 * This class may be freely distributed as part of any application or plugin.
 * <p>
 * Copyright (c) 2003 - 2004, Instantiations, Inc. <br>All Rights Reserved
 * 
 * @author scheglov_ke
 */
public class SwingResourceManager {

/**
 * Maps image names to images
 */
private static HashMap<String, Image> m_ClassImageMap = new HashMap<String, Image>();