Java JFrame显示不正确的内容

Java JFrame显示不正确的内容,java,swing,awt,jframe,layout-manager,Java,Swing,Awt,Jframe,Layout Manager,我正在写一个国际象棋程序,我正在处理提升一个棋子的问题。当棋子达到最后等级时,会弹出一个新的jframe,允许用户选择他想要提升棋子的位置。问题是,当我从我的主方法创建这个框架时,它显示得很好,但是当我在mouselistener代码中创建它时,它会在屏幕上准确地显示它后面的内容,我不知道为什么。这是我的相关代码: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.borde

我正在写一个国际象棋程序,我正在处理提升一个棋子的问题。当棋子达到最后等级时,会弹出一个新的jframe,允许用户选择他想要提升棋子的位置。问题是,当我从我的主方法创建这个框架时,它显示得很好,但是当我在mouselistener代码中创建它时,它会在屏幕上准确地显示它后面的内容,我不知道为什么。这是我的相关代码:

import java.awt.*;
import java.awt.event.*;
 import javax.swing.*;
import javax.swing.border.*;

public class Example extends JFrame {

private SquarePanel[][] squares;
private Colors colors;

public Example () {
    super();
    colors = new Colors();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container con = this.getContentPane();
    setSize(800,800);
    setLocation(0,0);
    con.setLayout(new GridLayout(8,8));
    SquarePanel pane = null;
    SquarePanel lastpanel = null;
    squares = new SquarePanel[8][8];
    int color = colors.WHITE;

    for(int i = 7; i >= 0; --i) {   

        if(color == colors.WHITE)
            color = colors.BLACK;
        else
            color = colors.WHITE;

        for(int j = 0; j <= 7; ++j) {

            if(color == colors.WHITE)
                color = colors.BLACK;
            else
                color = colors.WHITE;

            lastpanel = new SquarePanel(color,j,i);
            pane = lastpanel;               
            con.add(pane);
            squares[j][i] = pane;
        }
    }

    for(int i = 0; i < 8; ++i) {
        for(int j = 0; j < 8; ++j) {
            squares[j][i].setResident("pawn");
            //System.out.println(squares[j][i].getResident());
            //squares[j][i].repaint();
        }
    }

    MouseListener listener = new MoveListener(this, squares);
    for(int i = 0; i < 8; ++i)
        for(int j = 0; j < 8; ++j)
            squares[i][j].addMouseListener(listener);
    setVisible(true);
    PromotionFrame pf = new PromotionFrame(this, 1);
    String p = null;

    while(p == null) {
        p = pf.getPiece();
    }

    pf.dispose();
}

public class SquarePanel extends JPanel {
    private ImageIcon content;
    private int x;
    private int y;
    private int color;
    private String resident;
    private Colors colors;

    public SquarePanel(int color, int x, int y) {
        content = null;
        resident = null;
        this.color = color;
                    colors = new Colors();
        this.x = x;
        this.y = y;
        Border blackline = BorderFactory.createLineBorder(Color.black);
        setBorder(blackline);
        this.setLayout(new GridBagLayout());
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int width = getWidth();
        int height = getHeight();

        if(color == colors.WHITE)
            g.setColor(Color.white);
        else
            g.setColor(Color.green);

        g.fillRect(0, 0, width, height);
        g.setColor(Color.black);
        //System.out.println("here");
    }

    public String getResident() {
        return resident;
    }

    public void setResident(String p) {
        resident = p;

        if(p == null)
            removeAll();
        else
            add(new JLabel(p));
    }

    public int getXCoordinate() {
        return x;
    }

    public int getYCoordinate() {
        return y;
    }
}

private class Colors {
    public int BLACK = 0;
    public int WHITE = 1;
}

public class MoveListener implements MouseListener {
    private int first_click;
    private SquarePanel first_source;
    private JFrame frame;
    private int turn;
    private SquarePanel[][] squares;

    public MoveListener(JFrame f, SquarePanel[][] s) {
        first_click = 0;
        this.frame = f;
        turn = colors.WHITE;
        squares = s;
    }


    /* Empty method definition. */
    public void mousePressed(MouseEvent e) {
        SquarePanel p = (SquarePanel)e.getSource();
        ImageIcon content = null;

        if(first_click == 0) {
            first_source = p;
            ++first_click;
            p.setBorder(BorderFactory.createLineBorder(Color.cyan,4));
        }
        else {
            first_click = 0;
            first_source.setBorder(BorderFactory.createLineBorder(Color.black));
            //System.out.println(turn == colors.WHITE ? "White" : "Black");

            if(p.getResident().equals("pawn") && (p.getYCoordinate() == 7 || p.getYCoordinate() == 0)) {
                PromotionFrame pframe = new PromotionFrame(frame, colors.WHITE);
                String piece;

                do {
                    piece = pframe.getPiece();
                }
                while(piece == null);

                System.out.println(piece);
                pframe.dispose();
                p.setResident(piece);
            }

        }

    }

    /* Empty method definition. */
    public void mouseReleased(MouseEvent e) {
    }

    /* Empty method definition. */
    public void mouseEntered(MouseEvent e) {
    }

    /* Empty method definition. */
    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {

    }

}

public class PromotionFrame extends JFrame implements MouseListener {
    private JFrame master;
    private String piece;

    public PromotionFrame(JFrame master, int color) {
        super();
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        Container con = this.getContentPane();
        setSize(400,200);
        this.master = master;
        setLocation(master.getX()+master.getWidth()/2-200,master.getY()+master.getHeight()/2-100);
        setLocation(0,0);
        con.setLayout(new GridLayout(1,4));
        ImageIcon imgicon1;
        Image img1;
        Image newimg;
        ImageIcon img;
        ImageLabel label;
        piece = null;

        label = new ImageLabel("queen");
        label.addMouseListener(this);
        con.add(label);
        label = new ImageLabel("rook");
        label.addMouseListener(this);
        con.add(label);
        label = new ImageLabel("bishop");
        label.addMouseListener(this);
        con.add(label);
        label = new ImageLabel("knight");
        label.addMouseListener(this);
        con.add(label);

        this.show();
    }

    public String getPiece() {
        return piece;
    }

    /* Empty method definition. */
    public void mousePressed(MouseEvent e) {
        ImageLabel label = (ImageLabel)e.getSource();
        piece = label.getPiece();
    }

    /* Empty method definition. */
    public void mouseReleased(MouseEvent e) {
    }

    /* Empty method definition. */
    public void mouseEntered(MouseEvent e) {
    }

    /* Empty method definition. */
    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {

    }

    private class ImageLabel extends JLabel {
        private String piece;

        ImageLabel(String piece) {
            super(piece);
            this.piece = piece;
        }

        public String getPiece() {
            return piece;
        }
    }
}

public static void main(String[] args) {
    new Example();

}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.border.*;
公共类示例扩展了JFrame{
私人广场【】【】广场;
私人色彩;
公共示例(){
超级();
颜色=新颜色();
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con=this.getContentPane();
设置大小(800800);
设定位置(0,0);
con.setLayout(新网格布局(8,8));
SquarePanel窗格=空;
SquarePanel lastpanel=null;
正方形=新的方形面板[8][8];
int color=colors.WHITE;
对于(inti=7;i>=0;--i){
if(color==colors.WHITE)
颜色=颜色。黑色;
其他的
颜色=颜色。白色;
对于(int j=0;j1)更改主

public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            Example example = new Example();
        }
    });
}
(二)

您的弹出窗口
PromotionFrame
可能很容易冻结,因为在
MouseListener
中,您在每个MouseEvents上都创建了该弹出窗口,
MouseListener
生成了大量无法传递的
事件,然后生成了相同数量的
PromotionFrame

(a)

(b)

(c)

对于这个代码块,您是什么意思?第一个JLabel有四个不同的定义,最后一个对当前代码有效

        label = new ImageLabel("queen");
        label.addMouseListener(this);
        con.add(label);
        label = new ImageLabel("rook");
        label.addMouseListener(this);
        con.add(label);
        label = new ImageLabel("bishop");
        label.addMouseListener(this);
        con.add(label);
        label = new ImageLabel("knight");
        label.addMouseListener(this);
        con.add(label);
(d)

e) 仅创建一次
PromotionFrame
代码,将其用于其他操作或事件


3) 请再次阅读如何工作

为了更快地获得更好的帮助,请发布一篇文章。你确定所有这些代码都与问题严格相关吗?太多的代码让你无法查看和理解你在做什么。一条评论是,你的GUI主线中不应该有do/while循环。好的,这里有一个SSCCE。它不能正常工作,但它有same问题我遇到了,所以maaybe这将有助于诊断它。当它被创建时,它会在屏幕上准确地显示它后面的内容,而不是网格模式。我用SSCCE版本更改了原始帖子中的源代码。我不认为你的答案能解决问题。我的示例中确实有3个类,但它们都是内部类类。如果您将整个内容复制到一个名为Example.java的文件中,它应该编译并运行。如果您运行它,您将正确显示promotionframe,然后如果您按照我的说明单击一个方块,那么顶部行中的任何方块,promotionframe都将再次弹出,但不会正确显示它的结构。promotionframe应该每秒只能创建一次鼠标单击,然后它应该轮询一个答案,并在收到答案后立即离开
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);



setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        label = new ImageLabel("queen");
        label.addMouseListener(this);
        con.add(label);
        label = new ImageLabel("rook");
        label.addMouseListener(this);
        con.add(label);
        label = new ImageLabel("bishop");
        label.addMouseListener(this);
        con.add(label);
        label = new ImageLabel("knight");
        label.addMouseListener(this);
        con.add(label);
this.show();
this.setVisible(true);