来自火星的人31415 2023-03-22 20:03 采纳率: 0%
浏览 11

JTable的宽度调整问题

我正在使用java编写一个国际象棋gui,我希望在我的窗口中棋盘宽度与棋谱列表的宽度比约为8:2
但除非我把窗口变得很小,棋谱列表的宽度总是很大
就像这样

img


这是我现在使用的代码

//JD_situation_analysis
package window.SituationAnalysis;

import Setting.BoardSetting;
import chess.Chess;
import window.JF_window;

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

public class JD_situation_analysis extends JDialog {
    private final Chess chess = new Chess();
    private final JT_analysis_moves_table analysis_moves_table = new JT_analysis_moves_table();
    private final JP_analysis_board analysis_board;

    public JD_situation_analysis(JF_window window, BoardSetting setting) {
        super(window);
        Container container = getContentPane();

        container.setLayout(new GridBagLayout());
        analysis_board = new JP_analysis_board(chess,setting);
        GridBagConstraints boardGridBagConstraints = new GridBagConstraints();
        boardGridBagConstraints.gridx = 0;
        boardGridBagConstraints.gridy = 0;
        boardGridBagConstraints.fill = GridBagConstraints.BOTH;
        boardGridBagConstraints.weightx=80;
        boardGridBagConstraints.weighty=80;
        container.add(analysis_board,boardGridBagConstraints);

        GridBagConstraints movesTableGridBagConstraints = new GridBagConstraints();
        movesTableGridBagConstraints.gridx = 1;
        movesTableGridBagConstraints.gridy = 0;
        movesTableGridBagConstraints.fill = GridBagConstraints.BOTH;
        movesTableGridBagConstraints.weightx = 20;
        movesTableGridBagConstraints.weighty = 20;
        container.add(analysis_moves_table.crollPane,movesTableGridBagConstraints);

        setSize(800,500);
    }

    public void visible(Chess chess){
        this.chess.init(chess);
        analysis_moves_table.init(chess);
        analysis_board.repaint();
        analysis_moves_table.repaint();
        setVisible(true);
    }
}

//JT_analysis_moves_table

package window.SituationAnalysis;

import chess.Chess;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class JT_analysis_moves_table {
    private Chess chess;
    private final JTable table = new CTable();
    final Component crollPane = new JScrollPane(table);

    public JT_analysis_moves_table() {

    }

    public void init(Chess chess) {
        this.chess = chess;
        crollPane.revalidate();
    }

    public void repaint() {
        table.validate();
        table.updateUI();
    }
    public class CTable extends JTable{
        public CTable() {
            super(new model());
            getTableHeader().setReorderingAllowed(false);
            getTableHeader().setResizingAllowed(false);
        }
    }

    public class model extends DefaultTableModel {
        public model() {
            setColumnIdentifiers(new String[]{"步数", "白方", "黑方"});
        }

        @Override
        public int getRowCount() {
            return chess != null ? chess.moveList.size() : 0;
        }

        @Override
        public int getColumnCount() {
            return 3;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return chess != null ? chess.moveList.get(rowIndex).get(columnIndex) : null;
        }
    }
}

//JP_analysis_board

package window.SituationAnalysis;

import Setting.BoardSetting;
import chess.Chess;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;

public class JP_analysis_board extends JPanel {
    private final Chess chess;
    private final BoardSetting sitting;
    private Image black_king;
    private Image black_queen;
    private Image black_bishop;
    private Image black_knight;
    private Image black_rook;
    private Image black_pawn;

    private Image white_king;
    private Image white_queen;
    private Image white_bishop;
    private Image white_knight;
    private Image white_rook;
    private Image white_pawn;
    private Color black;
    private Color white;

    public JP_analysis_board(Chess chess, BoardSetting sitting) {
        this.chess = chess;
        this.sitting = sitting;
        init();
    }

    public void init() {
        black = new Color(sitting.black_block_color.get("r"), sitting.black_block_color.get("g"), sitting.black_block_color.get("b"));
        white = new Color(sitting.white_block_color.get("r"), sitting.white_block_color.get("g"), sitting.white_block_color.get("b"));
        try {
            black_king = ImageIO.read(new File(sitting.piece_image.get(sitting.piece).get("bk")));
            black_queen = ImageIO.read(new File(sitting.piece_image.get(sitting.piece).get("bq")));
            black_bishop = ImageIO.read(new File(sitting.piece_image.get(sitting.piece).get("bb")));
            black_knight = ImageIO.read(new File(sitting.piece_image.get(sitting.piece).get("bn")));
            black_rook = ImageIO.read(new File(sitting.piece_image.get(sitting.piece).get("br")));
            black_pawn = ImageIO.read(new File(sitting.piece_image.get(sitting.piece).get("bp")));

            white_king = ImageIO.read(new File(sitting.piece_image.get(sitting.piece).get("wk")));
            white_queen = ImageIO.read(new File(sitting.piece_image.get(sitting.piece).get("wq")));
            white_bishop = ImageIO.read(new File(sitting.piece_image.get(sitting.piece).get("wb")));
            white_knight = ImageIO.read(new File(sitting.piece_image.get(sitting.piece).get("wn")));
            white_rook = ImageIO.read(new File(sitting.piece_image.get(sitting.piece).get("wr")));
            white_pawn = ImageIO.read(new File(sitting.piece_image.get(sitting.piece).get("wp")));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        int block_width = getWidth() / 8;
        int block_height = getHeight() / 8;
        for (int x = 0; x < 8; x++) {
            for (int y = 0; y < 8; y++) {
                draw_block(g2d, x, y, block_width, block_height);
                if (chess.state != 0) {
                    switch (chess.piece_placement[y][x]) {
                        case 0:
                            break;

                        case 1:
                            draw_piece(g2d, white_pawn, x, y, block_width, block_height);
                            break;

                        case 2:
                            draw_piece(g2d, white_king, x, y, block_width, block_height);
                            break;

                        case 3:
                            draw_piece(g2d, white_queen, x, y, block_width, block_height);
                            break;

                        case 4:
                            draw_piece(g2d, white_bishop, x, y, block_width, block_height);
                            break;

                        case 5:
                            draw_piece(g2d, white_knight, x, y, block_width, block_height);
                            break;

                        case 6:
                            draw_piece(g2d, white_rook, x, y, block_width, block_height);
                            break;

                        case -1:
                            draw_piece(g2d, black_pawn, x, y, block_width, block_height);
                            break;

                        case -2:
                            draw_piece(g2d, black_king, x, y, block_width, block_height);
                            break;

                        case -3:
                            draw_piece(g2d, black_queen, x, y, block_width, block_height);
                            break;

                        case -4:
                            draw_piece(g2d, black_bishop, x, y, block_width, block_height);
                            break;

                        case -5:
                            draw_piece(g2d, black_knight, x, y, block_width, block_height);
                            break;

                        case -6:
                            draw_piece(g2d, black_rook, x, y, block_width, block_height);
                            break;
                    }
                }
            }
        }
    }

    private void draw_block(Graphics2D g2d, int x, int y, int width, int height) {
        g2d.setColor((x + y) % 2 == 0 ? white : black);
        g2d.fillRect(x * width, y * height, width, height);
    }

    private void draw_piece(Graphics2D g2d, Image image, int x, int y, int width, int height) {
        g2d.drawImage(image, x * width, y * height, width, height, this);
    }

}

我已经尝试过将列表的宽度调小,但没有效果
我应该怎么办?

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-03-23 06:42
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 3月22日

悬赏问题

  • ¥15 lvgl v8.2定时器提前到来
  • ¥15 qtcp 发送数据时偶尔会遇到发送数据失败?用的MSVC编译器(标签-qt|关键词-tcp)
  • ¥15 cam_lidar_calibration报错
  • ¥15 拓扑学,凸集,紧集。。
  • ¥15 如何扩大AIS数据容量
  • ¥15 单纯型python实现编译报错
  • ¥15 c++2013读写oracle
  • ¥15 c++ gmssl sm2验签demo
  • ¥15 关于模的完全剩余系(关键词-数学方法)
  • ¥15 有没有人懂这个博图程序怎么写,还要跟SFB连接,真的不会,求帮助