JFrame 显示不正确的内容

发布于 2024-12-10 23:52:43 字数 6953 浏览 0 评论 0原文

我正在编写一个国际象棋程序,现在我正在处理推广棋子。当一个 pawn 达到最终排名时,应该会弹出一个新的 jframe,允许用户选择他想要将 pawn 升级到哪个棋子。问题是,当我从主方法创建这个框架时,它显示得很好,但是当我在鼠标监听器代码中创建它时,它在屏幕上准确显示其后面的内容,我不知道为什么。这是我的相关代码:

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();

}
}

I am writing a chess program, and I am at the point where I am dealing with promoting a pawn. When a pawn reaches the final rank, a new jframe is supposed to pop up allowing the user to select what peice he wants the pawn to be promoted to. The problem is, when I create this frame from my main method, it displays just fine, but when I create it within my mouselistener code, it displays exactly whats behind it on the screen, and i have no idea why. Here is my relevant code:

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();

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭白衣梦中忆 2024-12-17 23:52:43

1) 更改 main

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

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

2)

您的弹出窗口 PromotionFrame 可能很容易冻结,因为在 MouseListener 中您在每个 MouseEvents 上创建了它,请小心地使用,MouseListener生成了令人难以置信的大量 Events,然后是相同数量的 PromotionFrame_s

a)

class PromotionFrame extends JDialog implements MouseListener {

b)

this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);



setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

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)

this.show();

this.setVisible(true);

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

3) 请再次阅读如何LayoutManagers 有效

1) change main

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

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

2)

your popup PromotionFrame probably simple to freeze, because inside MouseListener you created that on every MouseEvents, carefully with that, MouseListener generated unbeliveable amount of Events, then same amout of PromotionFrame_s

a)

class PromotionFrame extends JDialog implements MouseListener {

b)

this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);



setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

c)

what did you means with thi code block one JLabel with four different definitions, then last is valid for current code

        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)

this.show();

to

this.setVisible(true);

e) create PromotionFrame code only once, re-use that for another Actions or Events

3) please reads again how LayoutManagers works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文