简体   繁体   English

自定义布局Java Swing

[英]Custom layout Java Swing

I want to make board(map) like this in Java. 我想在Java中制作这样的板(地图)。

在此输入图像描述

Each small hexagon is image. 每个小六边形都是图像。

Suppose I have two Java classes. 假设我有两个Java类。 Canvas(big hexagon) and Hexagon. 帆布(大六边形)和六边形。 First is entire board from which I generate randomly all small hexagons. 首先是整板,我随机生成所有小六边形。 Both classes derived from JPanel. 这两个类都派生自JPanel。 Now I have GridLayout. 现在我有GridLayout。 How can I arrange layout like this? 我该如何安排这样的布局?

Why do you need the small hexagon panels? 为什么需要小六边形面板? I would rather just define List (list of Hexagons) each with desired position and just override paintComponent() method of main JPanel . 我宁愿只定义每个具有所需位置的List(六边形列表),只需覆盖主JPanel paintComponent()方法。 You can use this http://java-sl.com/shapes.html to create hexagon shapes. 您可以使用此http://java-sl.com/shapes.html创建六边形形状。

To track mouse click you can use contains() method of Shape. 要跟踪鼠标单击,可以使用Shape的contains()方法。

you can 您可以

1) common way 1)常用方法

  • by painting to the JPanel/JComponent by override paintComponent() (I asumed that there are Image/BufferedImage/Icon/ImageIco n) 通过覆盖paintComponent()绘制到JPanel/JComponent (我假设有Image/BufferedImage/Icon/ImageIco n)

2) by place Icon/ImageIcon to the JLabel 2)将Icon/ImageIcon放置到JLabel

The point of layout manages is to make it possible for the layout to auto-adjust when components change their size or the window does. 布局管理的目的是使布局能够在组件改变其大小或窗口时自动调整。

It looks like your hexagons will always be the same size, so you really don't need a layout manager, and positioning the hexagons absolutely should be fine. 看起来你的六边形总是大小相同,所以你真的不需要布局管理器,绝对定位六边形应该没问题。

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

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