初始化后未出现的jbutton [Java秋千]

发布于 2025-01-29 14:35:17 字数 3077 浏览 3 评论 0原文

我知道堆栈溢出上有很多问题,内容涉及jelements未出现,这都是因为有人在构造函数的末尾忘记忘记添加setVisibible(true)。但是至少我相信我的问题有所不同。我目前正在为大学创建国际象棋游戏,为此我有

  • 游戏课:在这里,这一切都汇集了
  • 抽象式的班级,将jbutton扩展到包装中
  • ,每件班级(Rook,Bishop,Bishop,...) 将碎片和位于零件包中

在我编写更多之前,

import javax.swing.*;
import Pieces.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Game extends JFrame {
    private static final int width = 8;
    private static final int height = 8;

    private static Piece clicked;

    private static Piece[][] fields = new Piece[width][height];

    private JPanel main = new JPanel();

    public static void init(JPanel g) {
        for (int y = 0; y < fields.length; y++) {
            for (int x = 0;  x < fields[y].length; x++) {

                if (y == 1) fields[y][x] = new Pawn(x, y, true); //2nd or 7th row is filled with pawns
                else if (y == 6) fields[y][x] = new Pawn(x, y, false);
                else {
                    fields[y][x] = new Empty(x,y,true);
                }

                fields[y][x].addActionListener(e -> {
                    var p = (Piece) e.getSource();
                    System.out.println(p.getX() + p.getY());
                });

                g.add(fields[y][x]);
            }
        }
    }

    public Game() {
        main.setBackground(Color.blue.darker());
        main.setLayout(new GridLayout(8,8));
        this.setSize(800,800);

        init(main);
        this.add(main);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        var g = new Game();
    }
}
package Pieces;

import javax.swing.*;

public abstract class Piece extends JButton {
    private int x;
    private int y;
    private final boolean isWhite;

    public Piece(int x, int y, boolean isWhite) {
        this.x = x;
        this.y = y;
        this.isWhite = isWhite;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public boolean isWhite() {
        return isWhite;
    }

    public boolean canMoveTo(int toX, int toY) {
        return true;
    }
}

,这是一个愚蠢的错误,这里的代码:每个片段 - 延伸类都与此设置完全一样:

package Pieces;

import java.awt.*;

public class Pawn extends Piece{
    public Pawn(int x, int y, boolean isWhite) {
        super(x, y, isWhite);
        this.setText(isWhite ? "Pawn" : "pawn");
    }
}

预期行为:

  • 打开一个带有64个jbuttons的窗口,显示它们代表的作品的名称(确实有一个非使用字段的空级)

实际行为:

  • 打开一个窗口,左上方一个按钮,但首先,当我使用光标浏览字段时,开始出现

  • 状态1:状态1

  • state 2:状态2

I know there are super many questions here on stack overflow about JElements not showing up, all because someone forgot to add a setVisible(true) at the end of the constructor. But at least I belive my problem is something different. I am currently creating a chess-game for college, and for that I have the

  • Game class: here it all comes together
  • the abstract Piece class extending JButton in package Pieces
  • and a class for Each Piece (Rook, Bishop, ...) each extending Piece and being located in the Pieces package

before I write much more and it is a dumb error again, here the code:

import javax.swing.*;
import Pieces.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Game extends JFrame {
    private static final int width = 8;
    private static final int height = 8;

    private static Piece clicked;

    private static Piece[][] fields = new Piece[width][height];

    private JPanel main = new JPanel();

    public static void init(JPanel g) {
        for (int y = 0; y < fields.length; y++) {
            for (int x = 0;  x < fields[y].length; x++) {

                if (y == 1) fields[y][x] = new Pawn(x, y, true); //2nd or 7th row is filled with pawns
                else if (y == 6) fields[y][x] = new Pawn(x, y, false);
                else {
                    fields[y][x] = new Empty(x,y,true);
                }

                fields[y][x].addActionListener(e -> {
                    var p = (Piece) e.getSource();
                    System.out.println(p.getX() + p.getY());
                });

                g.add(fields[y][x]);
            }
        }
    }

    public Game() {
        main.setBackground(Color.blue.darker());
        main.setLayout(new GridLayout(8,8));
        this.setSize(800,800);

        init(main);
        this.add(main);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        var g = new Game();
    }
}
package Pieces;

import javax.swing.*;

public abstract class Piece extends JButton {
    private int x;
    private int y;
    private final boolean isWhite;

    public Piece(int x, int y, boolean isWhite) {
        this.x = x;
        this.y = y;
        this.isWhite = isWhite;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public boolean isWhite() {
        return isWhite;
    }

    public boolean canMoveTo(int toX, int toY) {
        return true;
    }
}

each Piece-extending class is setup exactly like this:

package Pieces;

import java.awt.*;

public class Pawn extends Piece{
    public Pawn(int x, int y, boolean isWhite) {
        super(x, y, isWhite);
        this.setText(isWhite ? "Pawn" : "pawn");
    }
}

Expected behavior:

  • open a window with 64 JButtons in it, displaying the name of the Piece they represent (there is indeed an Empty-class for non-used fields)

Actual behavior:

  • opens a window with one button at the top left but first when I go over the fields with my cursor the buttons start appearing

  • state 1: state 1

  • state 2: state 2

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

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

发布评论

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

评论(1

能否归途做我良人 2025-02-05 14:35:17

您不能覆盖getxgety这样,这些属性由布局经理使用来布局。

相反,也许使用point存储虚拟或“单元”位置

public static abstract class Piece extends JButton {
    private final boolean isWhite;
    private Point cell;

    public Piece(int x, int y, boolean isWhite) {
        cell = new Point(x, y);
        this.isWhite = isWhite;
    }

    public Point getCell() {
        return cell;
    }

    public boolean isWhite() {
        return isWhite;
    }

    public boolean canMoveTo(int toX, int toY) {
        return true;
    }
}

可运行的示例...

“

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JPanel;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new BoardPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class BoardPane extends JPanel {
        private static final int width = 8;
        private static final int height = 8;

        private Piece clicked;

        private Piece[][] fields = new Piece[width][height];

        public BoardPane() {
            setBackground(Color.blue.darker());
            setLayout(new GridLayout(8, 8));
            buildBoard();
        }

        protected void buildBoard() {
            for (int y = 0; y < fields.length; y++) {
                for (int x = 0; x < fields[y].length; x++) {

                    if (y == 1) {
                        fields[y][x] = new Pawn(x, y, true); //2nd or 7th row is filled with pawns
                    } else if (y == 6) {
                        fields[y][x] = new Pawn(x, y, false);
                    } else {
                        fields[y][x] = new Empty(x,y,true);
                    }

                    fields[y][x].addActionListener(e -> {
                        var p = (Piece) e.getSource();
                        System.out.println(p.getCell());
                    });

                    add(fields[y][x]);
                }
            }
        }
    }

    public static abstract class Piece extends JButton {
        private final boolean isWhite;
        private Point cell;

        public Piece(int x, int y, boolean isWhite) {
            cell = new Point(x, y);
            this.isWhite = isWhite;
        }

        public Point getCell() {
            return cell;
        }

        public boolean isWhite() {
            return isWhite;
        }

        public boolean canMoveTo(int toX, int toY) {
            return true;
        }
    }

    public static class Pawn extends Piece {
        public Pawn(int x, int y, boolean isWhite) {
            super(x, y, isWhite);
            this.setText(isWhite ? "Pawn" : "pawn");
        }
    }

    public static class Empty extends Piece {
        public Empty(int x, int y, boolean isWhite) {
            super(x, y, isWhite);
            this.setText("X");
        }
    }
}

You can't override getX and getY like this, these properties are used by the layout managers to layout the components.

Instead, maybe make use of Point to store the virtual or "cell" position

public static abstract class Piece extends JButton {
    private final boolean isWhite;
    private Point cell;

    public Piece(int x, int y, boolean isWhite) {
        cell = new Point(x, y);
        this.isWhite = isWhite;
    }

    public Point getCell() {
        return cell;
    }

    public boolean isWhite() {
        return isWhite;
    }

    public boolean canMoveTo(int toX, int toY) {
        return true;
    }
}

Runnable example...

enter image description here

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JPanel;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new BoardPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class BoardPane extends JPanel {
        private static final int width = 8;
        private static final int height = 8;

        private Piece clicked;

        private Piece[][] fields = new Piece[width][height];

        public BoardPane() {
            setBackground(Color.blue.darker());
            setLayout(new GridLayout(8, 8));
            buildBoard();
        }

        protected void buildBoard() {
            for (int y = 0; y < fields.length; y++) {
                for (int x = 0; x < fields[y].length; x++) {

                    if (y == 1) {
                        fields[y][x] = new Pawn(x, y, true); //2nd or 7th row is filled with pawns
                    } else if (y == 6) {
                        fields[y][x] = new Pawn(x, y, false);
                    } else {
                        fields[y][x] = new Empty(x,y,true);
                    }

                    fields[y][x].addActionListener(e -> {
                        var p = (Piece) e.getSource();
                        System.out.println(p.getCell());
                    });

                    add(fields[y][x]);
                }
            }
        }
    }

    public static abstract class Piece extends JButton {
        private final boolean isWhite;
        private Point cell;

        public Piece(int x, int y, boolean isWhite) {
            cell = new Point(x, y);
            this.isWhite = isWhite;
        }

        public Point getCell() {
            return cell;
        }

        public boolean isWhite() {
            return isWhite;
        }

        public boolean canMoveTo(int toX, int toY) {
            return true;
        }
    }

    public static class Pawn extends Piece {
        public Pawn(int x, int y, boolean isWhite) {
            super(x, y, isWhite);
            this.setText(isWhite ? "Pawn" : "pawn");
        }
    }

    public static class Empty extends Piece {
        public Empty(int x, int y, boolean isWhite) {
            super(x, y, isWhite);
            this.setText("X");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文