package 背景图片;
import java.awt.*;
import javax.swing.*;
public class Application1 {
boolean packFrame = false;
public Application1() {
Frame1 frame = new Frame1();
if (packFrame) {
frame.pack();
} else {
frame.validate();
}
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
public static void main(String[] args) {
frame.getContentPane().add(contentPane); //提示我这行有错误
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
new Application1();
}
});
}
}
package 背景图片;
import java.awt.*;
import javax.swing.*;
public class Frame1 extends JFrame {
JPanel contentPane;
ImageIcon img=new ImageIcon("流氓图.jpg");
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public void paint(Graphics g)
{
g.drawImage(img.getImage(),0,0,this.getWidth(),this.getHeight(),this);
}
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("背景图片");
}
}
我想为窗体添加一个背景图片,请问我错在什么地方了,该怎么改呢,请高手指点!
----------------解决方案--------------------------------------------------------
package 背景图片;
import java.awt.*;
import javax.swing.*;
public class Application1 {
boolean packFrame = false;
private static Frame1 frame;
public Application1() {
frame = new Frame1();//把frame声明为成员数据
if (packFrame) {
frame.pack();
} else {
frame.validate();
}
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
public static void main(String[] args) {
frame.getContentPane().add(contentPane);//这句话多余反而出错!
JPanel container=Frame1.contentPane;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
new Application1();
}
});
}
}
package 背景图片;
import java.awt.*;
import javax.swing.*;
public class Frame1 extends JFrame {
static JPanel contentPane;//应声明为静态成员数据
ImageIcon img=new ImageIcon("流氓图.jpg");
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public void paint(Graphics g)
{
g.drawImage(img.getImage(),0,0,this.getWidth(),this.getHeight(),this);
}
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("背景图片");
}
}
[此贴子已经被作者于2007-7-19 10:02:00编辑过]
----------------解决方案--------------------------------------------------------