Java frame中调用了setContentPane(new Panel());方法,但不显示Panel
实现Lay类的子类代码我就不贴了...
问题主要在于:
已经在FrameGame类里头用了this.setContentPane(new PanelGame());
希望通过PanelGame里输出了一些图片(通过创建Lay对象输出图片)
但是Run以后,貌似连Panel都不显示...图片路径检查了应该没问题。
求指点,谢了!
代码如下:
PanelGame类:
package ui;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class PanelGame extends JPanel{
//创建一个对象数组,存储各个边框
private Lay[] lays=null;
//TODO 【硬编码】是非常不好的开发习惯
//TODO 尽量将数字货字符串定义为常量,或写入配置文件
public PanelGame() {
this.setVisible(true);
lays = new Lay[]{
new LayDataBase(40,32,334,279),
new LayDisk(40,343,334,279),
new LayGame(414,32,334,590),
new LayButton(788,32,334,124),
new LayNext(788,188,176,148),
new LayLevel(964,188,158,148),
new LayPoint(788,368,334,200)
};
}
public void pointComponent(Graphics g){
//test
g.fillRect(32, 32, 32, 32);
//循环刷新游戏画面
for(int i=0;i<lays.length;i++){
//刷新每一个边框
lays[i].paint(g);
}
}
}
FrameGame类:
package ui;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class FrameGame extends JFrame{
public FrameGame() {
//设置标题
this.setTitle("Tetris by 柴源");
//设置窗口关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口大小
this.setSize(1200, 700);
//设置 不允许用户改变窗口大小
this.setResizable(false);
//设置窗口位置
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screen = toolkit.getScreenSize();
int x = (screen.width-this.getWidth())/2;
int y = (screen.height-this.getHeight())/2-32; //此处用-32,因为Y轴正方向向下
this.setLocation(x,y);
//设置默认Panel
this.setContentPane(new PanelGame());
}
}
Lay类(抽象类):
package ui;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
/*
抽象类LAY,用于实现各个子窗体的绘制,
*/
public abstract class Lay {
//定义几个静态变量,供绘制方法createWindow使用
private static final int SIZE=7;
private static Image WINDOW_IMG=new ImageIcon("graphics/window/Window.png").getImage();
private static int WINDOW_W=WINDOW_IMG.getWidth(null);
private static int WINDOW_H=WINDOW_IMG.getHeight(null);
//定义属性,窗口左上角坐标(x,y)以及窗口宽W高H
private int x;
private int y;
private int w;
private int h;
//构造器,赋予窗体左上角位置(X,Y) 以及宽W高H
public Lay(int x,int y, int w, int h) {
// TODO Auto-generated constructor stub
this.x=x;
this.y=y;
this.w=w;
this.h=h;
}
//绘制窗口方法 绘制的方法必须有Graphics g参数,类似画笔
public void createWindow(Graphics g){
//原函数:g.drawImage(img,x1,y1,x2,y2,x3,y3,x4,y4,observer);
//左上角
g.drawImage(WINDOW_IMG, x, y,x+SIZE,y+SIZE, 0, 0, SIZE, SIZE, null);
//右上角
g.drawImage(WINDOW_IMG, x+w-SIZE, y,x+w,y+SIZE, WINDOW_W-SIZE, 0, WINDOW_W, SIZE, null);
//左下角
g.drawImage(WINDOW_IMG, x, y+h-SIZE,x+SIZE,y+h, 0, WINDOW_H-SIZE, SIZE, WINDOW_H, null);
//右下角
g.drawImage(WINDOW_IMG, x+w-SIZE, y+h-SIZE,x+w,y+h, WINDOW_W-SIZE, WINDOW_H-SIZE, WINDOW_W, WINDOW_H, null);
//上边
g.drawImage(WINDOW_IMG, x+SIZE, y, x+w-SIZE, y, SIZE,0, WINDOW_W-SIZE, 0, null);
//下边
g.drawImage(WINDOW_IMG, x+SIZE, y+h, x+w-SIZE, y+h, SIZE,WINDOW_H, WINDOW_W-SIZE, WINDOW_H, null);
//左边
g.drawImage(WINDOW_IMG, x, y+SIZE, x, y+h-SIZE, 0,SIZE, 0, WINDOW_H-SIZE, null);
//右边
g.drawImage(WINDOW_IMG, x+w, y+SIZE, x+w, y+h-SIZE, WINDOW_W , SIZE , WINDOW_W ,WINDOW_H-SIZE, null);
}
abstract public void paint(Graphics g);
}
实现Lay类的子类代码我就不贴了...
问题主要在于:
已经在FrameGame类里头用了this.setContentPane(new PanelGame());
希望通过PanelGame里输出了一些图片(通过创建Lay对象输出图片)