import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JPanel;public class Jigsaw {
public static void main(String args[]){
new JigsawFrame(600,600);
}
}class JigsawFrame{
private JFrame frame;
private JigsawMenu jm;
JigsawPanel jp;
public JigsawFrame(int width,int height){
frame = new JFrame("拼图");
frame.setResizable(false);
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

jp = new JigsawPanel();
frame.add(jp.southPanel,BorderLayout.CENTER);
}
}class JigsawPanel{
public JPanel southPanel = new JPanel();
private JLabel centerLabel = new JLabel();
private JLabel eastLabel = new JLabel();
private JLabel westLabel = new JLabel();

private GridLayout mainLayout = new GridLayout(3,3);
public MyJButton[][] button;
private PalyAction listener = new PalyAction();

public JigsawPanel(){
southPanel.setBackground(Color.BLUE);
southPanel.setLayout(mainLayout);


button = new MyJButton[5][5];
for(int i = 0;i<5;i++){
for(int j = 0;j<5;j++){
button[i][j] = new MyJButton(i,j);
}
}

for(int i = 1;i<4;i++){
for(int j = 1;j<4;j++){
button[i][j].addMouseListener(listener);
System.out.println(i); button[i][j].setBackground(Color.RED);
southPanel.add(button[i][j]);
}
}
}
}class MyJButton extends JButton{
/**
 * 
 */
private static final long serialVersionUID = 1L;
private int x;
private int y;
private boolean isRecption = false;
public boolean isRecption() {
return isRecption;
}
public void setRecption(boolean isRecption) {
this.isRecption = isRecption;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public MyJButton(int x,int y){
this.x = x;
this.y = y;
}
}首先执行以后JFrame可以显示出来 但是JPanel和JButton显示不出来 只有先最小化在最大化才能显示出来
其次当先最小化和最大化后JPanel和JButton显示出来了 但是只能看到第一行第一个的按钮 其他的都看不到 但是JButton都添加上去了 当把鼠标移动到其他地方时 那个唯一显示出来的按钮就会变为当前鼠标所在的按钮(JButton添加上去了但是看不到) 当不用我自己定义的MyJButton类 而把public MyJButton[][] button; 改成public JButton[][] button;后面的都用JButton实例化时是可以正确显示的 import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JPanel;public class Jigsaw {
public static void main(String args[]){
new JigsawFrame(600,600);
}
}class JigsawFrame{
private JFrame frame;
private JigsawMenu jm;
JigsawPanel jp;
public JigsawFrame(int width,int height){
frame = new JFrame("拼图");
frame.setResizable(false);
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

jp = new JigsawPanel();
frame.add(jp.southPanel,BorderLayout.CENTER);
}
}class JigsawPanel{
public JPanel southPanel = new JPanel();
private JLabel centerLabel = new JLabel();
private JLabel eastLabel = new JLabel();
private JLabel westLabel = new JLabel();

private GridLayout mainLayout = new GridLayout(3,3);
public JButton[][] button;
private PalyAction listener = new PalyAction();

public JigsawPanel(){
southPanel.setBackground(Color.BLUE);
southPanel.setLayout(mainLayout);


button = new JButton[5][5];
for(int i = 0;i<5;i++){
for(int j = 0;j<5;j++){
button[i][j] = new JButton();
}
}

for(int i = 1;i<4;i++){
for(int j = 1;j<4;j++){
button[i][j].addMouseListener(listener);
System.out.println(i); button[i][j].setBackground(Color.RED);
southPanel.add(button[i][j]);
}
}
}
}
改成这样就可以正确显示了 但是依然要先最小化在最大化才能看到JFrame谁知道是什么原因造成的 还是我太粗心 哪里写错了 检查了好多遍都没有收获
大家快点解释一下

解决方案 »

  1.   

            frame.setVisible(true);
            
            jp = new JigsawPanel();
            frame.add(jp.southPanel,BorderLayout.CENTER); 把frame显示放在添加组件的后面:
            jp = new JigsawPanel();
            frame.add(jp.southPanel,BorderLayout.CENTER); 
            frame.setVisible(true);
      

  2.   

    楼主的setX和setY方法覆盖了JComponent类里面的setX和setY方法,JComponent类里面的setX和setY方法肯定还做了其他的事了的,楼主不妨这么改一下:public void setX(int x) {
    super.setX(x)
    this.x = x;
    }public void setY(int y) {
    super.setX(y)
    this.y = y;
    }