【问题标题】:Can't set the size of a JPanel inside of a JDialog无法在 JDialog 中设置 JPanel 的大小
【发布时间】:2014-05-04 21:14:01
【问题描述】:

我正在尝试创建 Atari GO 的小游戏,但界面有问题。我有一个名为 GameBoard 的类,它扩展了 JPanel 类。在主要方法中,我创建了这样一个GameBoard,然后创建了一个JDialog,将GameBoard 添加到其中,并在JDialog 对象上调用pack() 方法。而不是得到这个:

我明白了:

这是我的代码:

    package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

public class GameBoard extends JPanel{
    BufferedImage backgroundImage;
    int boardSize;
    public GameBoard(){
        super();
    }
    public void initUi(){
        this.setBackground(Color.white);
    }
    public boolean setBoardSize(int nr) {
        //setting the dimension of the board
        this.boardSize = nr * 30;
        this.setSize(boardSize, boardSize);
        //i do some drawing next on a BufferedImage
        backgroundImage = new BufferedImage(this.boardSize, this.boardSize, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D)backgroundImage.getGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2.setColor(new Color(153,102,51));
        g2.fillRect(0, 0,this.boardSize, this.boardSize);

        g2.setColor(new Color(255,255,51));
        for (int i = 0; i < nr; i++) {
            g2.drawLine(0, 15 + i * 30, this.boardSize, 15 + i * 30);
            g2.drawLine(15 + i * 30, 0, 15 + i * 30, this.boardSize);

        }

        g2.setColor(new Color(255,51,102));
        g2.drawRect(2, 2, this.boardSize-4, this.boardSize-4);

        return true;
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.drawImage(backgroundImage, 0, 0, null);
    }
    public static void main(String []args){
        //i create here a GameBoard
        GameBoard board = new GameBoard();
        board.initUi();
        board.setBoardSize(10);

        JDialog gameDialog = new JDialog();
        gameDialog.setLocationRelativeTo(null);
        gameDialog.setModal(true);
        gameDialog.add(board);
        gameDialog.pack();
        gameDialog.setVisible(true);
    }

}

我尝试在 JDialog 组件上使用不同的布局管理器,但没有结果。我应该怎么做才能得到我想要的输出?谢谢。

【问题讨论】:

    标签: java swing jpanel layout-manager jdialog


    【解决方案1】:

    覆盖面板getPreferredSize 方法并返回您想要的首选大小。

    调用时,pack 将询问布局管理器内容窗格的首选大小,并确保可视区域尽可能满足这些要求

    Ps:不要忘记从BufferedImage 中处理Graphics 上下文

    【讨论】:

      【解决方案2】:

      在您的类GameBoard 中重写方法getPreferredSize(),如下所示:

      @Override
      public Dimension getPreferredSize() {
          return new Dimension(boardSize, boardSize);
      }
      

      并删除this.setSize(boardSize, boardSize);

      【讨论】:

        猜你喜欢
        • 2011-08-11
        • 1970-01-01
        • 1970-01-01
        • 2013-08-15
        • 1970-01-01
        • 2014-04-29
        • 2021-01-16
        • 2012-09-01
        • 1970-01-01
        相关资源
        最近更新 更多