| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 836 人关注过本帖
标题:[求助]改变窗体的背景图片
只看楼主 加入收藏
非凡DZ
Rank: 1
等 级:新手上路
帖 子:231
专家分:0
注 册:2007-4-6
收藏
 问题点数:0 回复次数:1 
[求助]改变窗体的背景图片

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 
2007-07-19 09:11
jdk2006
Rank: 1
等 级:新手上路
帖 子:244
专家分:0
注 册:2007-5-12
收藏
得分:0 
以下是引用非凡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编辑过]


2007-07-19 10:01
快速回复:[求助]改变窗体的背景图片
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019889 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved