求助,我在一个JPanel中设置为网格布局(4,4)
每个空格加入一个JComponent
每个JComponent包含一张图片
当我16个空格全部都添加包含图片的JComponent时 显示出来的是一个小格子
但是当我在16个空格中加入一个按钮时 其他图片就能正常显示了 求助怎么让16张图片正常显示
目前效果如下:
源代码如下:
package com.main;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Tzfe {
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
EventQueue.invokeAndWait(new Runnable(){
public void run(){
new TzfeFrame();
System.out.print("test"+pub.a[0][0]);
}
});
}
}
/////////////////////////////////////////////////////////////////////
class TzfeFrame implements ActionListener{
JFrame jframe;
TzfePanel tzfepanel=new TzfePanel();
public TzfeFrame(){
jframe=new JFrame();
jframe.setLayout(new FlowLayout());
jframe.setTitle("2048");
jframe.setSize(500,500);
jframe.setVisible(true);
JPanel test1=new JPanel();
//TzfeComponent tzfecomponent=new TzfeComponent();
JButton test=new JButton("test");
test.addActionListener(this);
test1.add(test);
jframe.add(tzfepanel,FlowLayout.LEFT);
jframe.add(test1,FlowLayout.CENTER);
}
public void actionPerformed(ActionEvent event){
pub.a[0][0]=4;
System.out.print("test"+pub.a[0][0]);
jframe.remove(tzfepanel);
//jframe.removeAll();
jframe.add(tzfepanel=new TzfePanel(),FlowLayout.LEFT);
jframe.invalidate();
jframe.repaint();
jframe.setVisible(true);
jframe.pack();
}
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
class TzfePanel extends JPanel{
public TzfePanel(){
setLayout(new GridLayout(4,4));
TzfeComponent tzfecomponent1=new TzfeComponent(pub.a[0][0]);
add(tzfecomponent1);
TzfeComponent tzfecomponent2=new TzfeComponent(pub.a[0][1]);
add(tzfecomponent2);
TzfeComponent tzfecomponent3=new TzfeComponent(pub.a[0][1]);
add(tzfecomponent3);
TzfeComponent tzfecomponent4=new TzfeComponent(pub.a[0][1]);
add(tzfecomponent4);
TzfeComponent tzfecomponent5=new TzfeComponent(pub.a[0][1]);
add(tzfecomponent5);
TzfeComponent tzfecomponent6=new TzfeComponent(pub.a[0][1]);
add(tzfecomponent6);
TzfeComponent tzfecomponent7=new TzfeComponent(pub.a[0][1]);
add(tzfecomponent7);
TzfeComponent tzfecomponent8=new TzfeComponent(pub.a[0][1]);
add(tzfecomponent8);
TzfeComponent tzfecomponent9=new TzfeComponent(pub.a[0][1]);
add(tzfecomponent9);
TzfeComponent tzfecomponent10=new TzfeComponent(pub.a[0][1]);
add(tzfecomponent10);
TzfeComponent tzfecomponent11=new TzfeComponent(pub.a[0][1]);
add(tzfecomponent11);
TzfeComponent tzfecomponent12=new TzfeComponent(pub.a[0][1]);
add(tzfecomponent12);
TzfeComponent tzfecomponent13=new TzfeComponent(pub.a[0][1]);
add(tzfecomponent13);
TzfeComponent tzfecomponent14=new TzfeComponent(pub.a[0][1]);
add(tzfecomponent14);
//TzfeComponent tzfecomponent16=new TzfeComponent(pub.a[0][0]);
//panel16.add(tzfecomponent16);
JButton button16=new JButton("16");
//add(button16);
setVisible(true);
this.setVisible(true);
}
//定义监听事件
private class clicking implements ActionListener{
public void actionPerformed(ActionEvent event){
pub.a[0][0]=4;
pub.a[0][1]=1024;
System.out.print("test");
}
}
}
//////////////////////////////////////////////////////////////////////
class TzfeComponent extends JComponent{
public TzfeComponent(int i){
try{
if(i==0)
{image=ImageIO.read(new File("E://0.jpg"));}
if(i==2)
{image=ImageIO.read(new File("E://2.jpg"));}
if(i==4)
{image=ImageIO.read(new File("E://4.jpg"));}
if(i==8)
{image=ImageIO.read(new File("E://8.jpg"));}
if(i==16)
{image=ImageIO.read(new File("E://16.jpg"));}
if(i==32)
{image=ImageIO.read(new File("E://32.jpg"));}