当前位置: 代码迷 >> Java相关 >> [求助]改变窗体的背景图片
  详细解决方案

[求助]改变窗体的背景图片

热度:116   发布时间:2007-07-19 09:11:47.0
[求助]改变窗体的背景图片

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("背景图片");
}
}

我想为窗体添加一个背景图片,请问我错在什么地方了,该怎么改呢,请高手指点!

搜索更多相关的解决方案: 背景图片  窗体  frame  Dimension  import  

----------------解决方案--------------------------------------------------------
以下是引用非凡DZ在2007-7-19 9:11:47的发言:

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编辑过]


----------------解决方案--------------------------------------------------------