简体   繁体   English

初始化后 JButtons 不显示 [Java Swing]

[英]JButtons not showing up after initialisation [Java Swing]

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.我知道这里有很多关于 JElements 没有显示的堆栈溢出问题,都是因为有人忘记在构造函数的末尾添加一个setVisible(true) 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 Piece 包中扩展 JButton 的抽象 Piece 类
  • and a class for Each Piece (Rook, Bishop, ...) each extending Piece and being located in the Pieces package以及每个 Piece(Rook、Bishop、...)的类,每个扩展 Piece 并位于 Pieces 包中

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:每个 Piece 扩展类的设置完全如下:

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)打开一个包含 64 个 JButton 的窗口,显示它们所代表的 Piece 的名称(确实有一个 Empty-class 用于未使用的字段)

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状态 1:状态 1

  • state 2: state 2状态 2:状态 2

You can't override getX and getY like this, these properties are used by the layout managers to layout the components.您不能像这样覆盖getXgetY ,布局管理器使用这些属性来布局组件。

Instead, maybe make use of Point to store the virtual or "cell" position相反,也许使用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;
    }
}

Runnable example...可运行的示例...

在此处输入图像描述

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");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM