Java JFrame加入背景图片后菜单不能正常显示
我加入一张图片做JFrame的背景要求Panel类ChessPad要在背景的前端但我加入背景图后菜单条和菜单都不能正常显示(需要鼠标点击才显示)求达人帮我修改跪谢!另外,...
我加入一张图片做JFrame的背景 要求Panel类ChessPad要在背景的前端 但我加入背景图后 菜单条和菜单都不能正常显示(需要鼠标点击才显示) 求达人帮我修改 跪谢!
另外,为什么我把class ChessPad extends Panel的Panel改为JPanel 后棋盘为什么会显示不出来?
代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.lang.Object;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
//创建棋盘的类
class ChessPad extends JPanel
{
Button button=new Button("重新开局");
TextField text_1=new TextField("请黑棋下子");
ChessPad()
{ setSize(440,440);
setLayout(null);
this.setBackground(new Color(197,97,12)); //注意这里不要漏写 new
add(button);
button.setBounds(10,3,60,26);
add(text_1);
text_1.setBounds(90,3,90,24);
text_1.setEditable(false);
}
public void paint(Graphics g) //绘制棋盘方格
{ for(int i=30;i<=410;i=i+20) //画横线
{ g.drawLine(30,i,410,i);
}
for(int j=30;j<=410;j=j+20) //画竖线
{ g.drawLine(j,30,j,410);
}
}
}
public class WuZiQi02 extends JFrame
{
JMenuBar menubar;
JMenu menu1,menu2;
ChessPad chesspad=new ChessPad();
WuZiQi02()
{
super("简易五子棋"); //程序框标题
menubar=new JMenuBar();
menubar.setOpaque(true); //菜单条是否透明
menu1=new JMenu("游戏");
menu2=new JMenu("帮助");
menubar.add(menu1);
menubar.add(menu2);
setJMenuBar(menubar);
setVisible(true);
setLayout(null);
add(chesspad);
chesspad.setBounds(70,90,440,440);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
pack();
setSize(900,750);
validate();
}
//设置Frame的背景图
public void paint(Graphics g){
BufferedImage image = null;
try{
image = ImageIO.read(new File("icons/d.jpg") ); //图片路径
}
catch(IOException e){
e.printStackTrace();
}
g.drawImage(image,0,70,this);
//g.drawImage(image,0,0,this.getWidth(), this.getHeight(),null); 设置适应窗口
}
public static void main(String args[])
{ WuZiQi02 chess=new WuZiQi02();
}
} 展开
另外,为什么我把class ChessPad extends Panel的Panel改为JPanel 后棋盘为什么会显示不出来?
代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.lang.Object;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
//创建棋盘的类
class ChessPad extends JPanel
{
Button button=new Button("重新开局");
TextField text_1=new TextField("请黑棋下子");
ChessPad()
{ setSize(440,440);
setLayout(null);
this.setBackground(new Color(197,97,12)); //注意这里不要漏写 new
add(button);
button.setBounds(10,3,60,26);
add(text_1);
text_1.setBounds(90,3,90,24);
text_1.setEditable(false);
}
public void paint(Graphics g) //绘制棋盘方格
{ for(int i=30;i<=410;i=i+20) //画横线
{ g.drawLine(30,i,410,i);
}
for(int j=30;j<=410;j=j+20) //画竖线
{ g.drawLine(j,30,j,410);
}
}
}
public class WuZiQi02 extends JFrame
{
JMenuBar menubar;
JMenu menu1,menu2;
ChessPad chesspad=new ChessPad();
WuZiQi02()
{
super("简易五子棋"); //程序框标题
menubar=new JMenuBar();
menubar.setOpaque(true); //菜单条是否透明
menu1=new JMenu("游戏");
menu2=new JMenu("帮助");
menubar.add(menu1);
menubar.add(menu2);
setJMenuBar(menubar);
setVisible(true);
setLayout(null);
add(chesspad);
chesspad.setBounds(70,90,440,440);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
pack();
setSize(900,750);
validate();
}
//设置Frame的背景图
public void paint(Graphics g){
BufferedImage image = null;
try{
image = ImageIO.read(new File("icons/d.jpg") ); //图片路径
}
catch(IOException e){
e.printStackTrace();
}
g.drawImage(image,0,70,this);
//g.drawImage(image,0,0,this.getWidth(), this.getHeight(),null); 设置适应窗口
}
public static void main(String args[])
{ WuZiQi02 chess=new WuZiQi02();
}
} 展开
2个回答
展开全部
需要在你重写的paint方法的最后加上这么一行:super.paint(g);
因为所有组件的显示都要靠paint方法。
JPanel默认为不透明的,要给JPanel添加背景图片需要将其设为透明:setOpaque(false);
不过,在透明模式下看不到backgroundColor,但可以用填充颜色的方式代替
修改如下:(在指氏eclipse下编译通过)
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
/**
* @author 熙熙
* @2011-4-18
*/
class ChessPad extends JPanel {
JButton button = new JButton("重新唯段散开局");
TextField text_1 = new TextField("请黑棋下子");
ChessPad() {
setOpaque(false);
setSize(440, 440);
setLayout(null);
add(button);
button.setBounds(60, 3, 90, 24);
add(text_1);
text_1.setBounds(170, 3, 90, 24);
text_1.setEditable(false);
}
private Color backgroundColor = new Color(197, 97, 12);
public void paint(Graphics g) // 绘制棋盘方格
{
g.setColor(backgroundColor);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
for (int i = 30; i <= 410; i = i + 20) // 画横线
{
g.drawLine(30, i, 410, i);
}
for (int j = 30; j <= 410; j = j + 20) /燃羡/ 画竖线
{
g.drawLine(j, 30, j, 410);
}
super.paint(g);
}
}
public class WuZiQi02 extends JFrame {
JMenuBar menubar;
JMenu menu1, menu2;
ChessPad chesspad = new ChessPad();
WuZiQi02() {
super("简易五子棋"); // 程序框标题
menubar = new JMenuBar();
menubar.setOpaque(true); // 菜单条是否透明
menu1 = new JMenu("游戏");
menu2 = new JMenu("帮助");
menubar.add(menu1);
menubar.add(menu2);
setJMenuBar(menubar);
this.setContentPane(contentPane);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
pack();
validate();
}
private JPanel contentPane = new JPanel() {
{
setOpaque(false);
setLayout(null);
add(chesspad);
chesspad.setBounds(70, 90, 440, 440);
}
private BufferedImage backgroundImage = ImageUtil
.loadBufferedImage("icons/d.jpg");
// 设置Frame的背景图
public void paint(Graphics g) {
g.drawImage(backgroundImage, 0, 0, this.getWidth(), this
.getHeight(), this); // 设置适应窗口
super.paint(g);
}
};
public static void main(String args[]) {
WuZiQi02 chess = new WuZiQi02();
chess.setSize(900, 750);
chess.setVisible(true);
}
}
class ImageUtil {
public static BufferedImage loadBufferedImage(String filename) {
return loadBufferedImage(ImageUtil.class, filename);
}
public static BufferedImage loadBufferedImage(Class<?> clazz,
String filename) {
BufferedImage image = null;
if (null == clazz)
clazz = ImageUtil.class;
InputStream in = clazz.getResourceAsStream(filename);
try {
if (null != in)
image = ImageIO.read(ImageIO.createImageInputStream(in));
else
System.out.println("input == null !");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return image;
}
}
因为所有组件的显示都要靠paint方法。
JPanel默认为不透明的,要给JPanel添加背景图片需要将其设为透明:setOpaque(false);
不过,在透明模式下看不到backgroundColor,但可以用填充颜色的方式代替
修改如下:(在指氏eclipse下编译通过)
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
/**
* @author 熙熙
* @2011-4-18
*/
class ChessPad extends JPanel {
JButton button = new JButton("重新唯段散开局");
TextField text_1 = new TextField("请黑棋下子");
ChessPad() {
setOpaque(false);
setSize(440, 440);
setLayout(null);
add(button);
button.setBounds(60, 3, 90, 24);
add(text_1);
text_1.setBounds(170, 3, 90, 24);
text_1.setEditable(false);
}
private Color backgroundColor = new Color(197, 97, 12);
public void paint(Graphics g) // 绘制棋盘方格
{
g.setColor(backgroundColor);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
for (int i = 30; i <= 410; i = i + 20) // 画横线
{
g.drawLine(30, i, 410, i);
}
for (int j = 30; j <= 410; j = j + 20) /燃羡/ 画竖线
{
g.drawLine(j, 30, j, 410);
}
super.paint(g);
}
}
public class WuZiQi02 extends JFrame {
JMenuBar menubar;
JMenu menu1, menu2;
ChessPad chesspad = new ChessPad();
WuZiQi02() {
super("简易五子棋"); // 程序框标题
menubar = new JMenuBar();
menubar.setOpaque(true); // 菜单条是否透明
menu1 = new JMenu("游戏");
menu2 = new JMenu("帮助");
menubar.add(menu1);
menubar.add(menu2);
setJMenuBar(menubar);
this.setContentPane(contentPane);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
pack();
validate();
}
private JPanel contentPane = new JPanel() {
{
setOpaque(false);
setLayout(null);
add(chesspad);
chesspad.setBounds(70, 90, 440, 440);
}
private BufferedImage backgroundImage = ImageUtil
.loadBufferedImage("icons/d.jpg");
// 设置Frame的背景图
public void paint(Graphics g) {
g.drawImage(backgroundImage, 0, 0, this.getWidth(), this
.getHeight(), this); // 设置适应窗口
super.paint(g);
}
};
public static void main(String args[]) {
WuZiQi02 chess = new WuZiQi02();
chess.setSize(900, 750);
chess.setVisible(true);
}
}
class ImageUtil {
public static BufferedImage loadBufferedImage(String filename) {
return loadBufferedImage(ImageUtil.class, filename);
}
public static BufferedImage loadBufferedImage(Class<?> clazz,
String filename) {
BufferedImage image = null;
if (null == clazz)
clazz = ImageUtil.class;
InputStream in = clazz.getResourceAsStream(filename);
try {
if (null != in)
image = ImageIO.read(ImageIO.createImageInputStream(in));
else
System.out.println("input == null !");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return image;
}
}
追问
paint方法的最后加上这么一行:super.paint(g);
这样图片又显示不了 了喔
追答
帮你修改成功了。你运行看看
2011-04-18
展开全部
层次不戚厅一样败仔没。。。
一个窗体的面板 分为好几察纳层。
在下面显示的 内容会被上面的 遮住
一个窗体的面板 分为好几察纳层。
在下面显示的 内容会被上面的 遮住
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询