【问题标题】:JPanel size not changingJPanel 大小不变
【发布时间】:2013-11-16 16:12:44
【问题描述】:

大家好,我正在尝试用 Java 创建一个草稿游戏,并且正在使用 JPanel 来表示方块,如果我要更改面板的大小,我会怎么做?如果我使用布局管理器,方块就不够大。目前我没有使用布局管理器来尝试更改大小,但大小似乎没有改变 - 只是保持在 1,1 像素。

 private void createSquares(){
    for(int i = 0; i < 65; i++){
        squares[i] = new JPanel();
        squares[i].setLayout(null);
        squares[i].setSize(20,20);
        board.add(squares[i]);
    }
}

【问题讨论】:

  • 是的,使用布局管理器。不,永远不要使用 null 布局。
  • Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。 (@HovercraftFullOfEels 所说的,但更长。)
  • 另见examplevariation
  • 请阅读布局管理器教程。如果你这样做,你会发现 JPanel 默认使用 FlowLayout,而顶层窗口使用 BorderLayout,这意味着什么。
  • 覆盖 getPreferredSize() 以建立初始大小,并使用 GridLayout 跟踪封闭容器大小的变化。

标签: java swing jpanel layout-manager


【解决方案1】:

您总是可以在线“借用”一两张图片,然后将其放入您的程序中。例如这段代码:

import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class GetChessSquareImages {
   public static final String PATH_TO_SQUARES = "http://www.colourbox.com/preview/" +
        "4578561-622234-seamless-oak-square-chess-like-parquet-texture.jpg";
   private static final int IMG_SIDE_COUNT = 4;
   private static final double SCALE = 0.8;
   private Map<SquareColor, List<Icon>> squareColorMap = new HashMap<SquareColor, List<Icon>>(); 
   private Random random = new Random();

   public void downloadImages() throws IOException {
      URL lrgImgUrl = new URL(PATH_TO_SQUARES);
      BufferedImage largeImg = ImageIO.read(lrgImgUrl);

      int w = largeImg.getWidth() / IMG_SIDE_COUNT;
      int h = largeImg.getHeight() / IMG_SIDE_COUNT;
      for (int i = 0; i < IMG_SIDE_COUNT; i++) {
         int x = (i * largeImg.getWidth()) / IMG_SIDE_COUNT;
         for (int j = 0; j < IMG_SIDE_COUNT; j++) {
            if (j != 1 && j != 2) {
               int y = (j * largeImg.getHeight()) / IMG_SIDE_COUNT;
               extractSubImg(largeImg, i, j, x, y, w, h);
            }
         }
      }
   }

   private void extractSubImg(BufferedImage largeImg, 
         int i, int j, int x, int y, int w, int h) {
      Image subImg = largeImg.getSubimage(x, y, w, h);
      int width = (int) (w * SCALE);
      int height = (int) (h * SCALE);
      subImg = subImg.getScaledInstance(width, height, Image.SCALE_SMOOTH);
      List<Icon> iconList = null;
      if (i % 2 == j % 2) {
         iconList = squareColorMap.get(SquareColor.LIGHT);
         if (iconList == null) {
            iconList = new ArrayList<Icon>();
            squareColorMap.put(SquareColor.LIGHT, iconList);
         }
      } else {
         iconList = squareColorMap.get(SquareColor.DARK);
         if (iconList == null) {
            iconList = new ArrayList<Icon>();
            squareColorMap.put(SquareColor.DARK, iconList);
         }
      }
      iconList.add(new ImageIcon(subImg));
   }

   public Icon getRandomIcon(SquareColor sqrColor) {
      List<Icon> iconList = squareColorMap.get(sqrColor);
      if (iconList == null) {
         return null;
      } else {
         return iconList.get(random.nextInt(iconList.size()));
      }
   }

   public static void main(String[] args) {
      GetChessSquareImages getImages = new GetChessSquareImages();
      try {
         getImages.downloadImages();
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }

      int side = 8;;
      JPanel panel = new JPanel(new GridLayout(side , side));
      for (int i = 0; i < side; i++) {
         for (int j = 0; j < side; j++) {
            SquareColor sqrColor = (i % 2 == j % 2) ? SquareColor.LIGHT : SquareColor.DARK;
            Icon icon = getImages.getRandomIcon(sqrColor);
            panel.add(new JLabel(icon));
         }
      }
      JOptionPane.showMessageDialog(null, panel);
   }
}

enum SquareColor {
   DARK, LIGHT
}

返回这个 JPanel:

那么您的正方形大小将基于您的 ImageIcons 的大小。例如,我用 0.8 的比例因子(上面的 SCALE 常数)缩小了我的正方形,以使网格的大小更合理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 2021-05-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多