简体   繁体   English

调整框架大小时,JLabel的位置会变为奇怪的位置

[英]JLabel's position changes to weird location when resizing the frame

I'm just doing my personal project that draws a basic map using Swing. 我正在做我的个人项目,使用Swing绘制基本地图。

I have many issues but the worst one is that the position of JLable s changes every time I 我有很多问题,但最糟糕的是JLable的位置每次都会改变

  1. Resize the frame 调整框架大小
  2. Set them invisible and visible again (setvisible(false)->setvisible(true)) 将它们设置为不可见并再次可见(setvisible(false) - > setvisible(true))
  3. Move the screen to another tab 'Map 2' and come back to Map 1 (The frame has JTabbedPane ) 将屏幕移动到另一个选项卡“地图2”并返回到地图1(框架具有JTabbedPane

It's sad that I don't have enough reputation to upload images :( 很遗憾我没有足够的声誉来上传图片:(

all the JLable s are located on the top of the JPanel . 所有JLable都位于JPanel的顶部。

It's like 就像是

Before 之前
.----------------------------------------------- .-----------------------------------------------
.----------------------------------------------- .-----------------------------------------------
.---------Label1----------------------------- .---------的Label1 -----------------------------
.----------------------------------------------- .-----------------------------------------------
.------------------Label2-------------------- .------------------ Label2的--------------------
.----------------------------------------------- .-----------------------------------------------
.-------------------------------Label3------- .------------------------------- LABEL3 -------
.----------------------------------------------- .-----------------------------------------------
.----------------------------------------------- .-----------------------------------------------
.----------------------------------------------- .-----------------------------------------------

After
.----------------------------------------------- .-----------------------------------------------
.---------Label1 Label2 Label3----------- .--------- Label1 Label2 Label3 -----------
.----------------------------------------------- .-----------------------------------------------
.----------------------------------------------- .-----------------------------------------------
.----------------------------------------------- .-----------------------------------------------
.----------------------------------------------- .-----------------------------------------------
.----------------------------------------------- .-----------------------------------------------
.----------------------------------------------- .-----------------------------------------------
.----------------------------------------------- .-----------------------------------------------
.----------------------------------------------- .-----------------------------------------------

I also tried using setLocation(new Point(x,y)); 我也尝试过使用setLocation(new Point(x,y)); after resizing happens but nothing came out. 调整大小后发生但没有任何结果。

Any tips will be appreciated. 任何提示将不胜感激。 :) :) 在此输入图像描述在此输入图像描述

You have used absolute positioning without setting the layout accordingly. 您已使用绝对定位而无需相应地设置布局。 Refreshing the display in the presence of FlowLayout , the default for JPanel , causes the layout you see in after . 在存在FlowLayoutJPanel的默认设置)的情况下刷新显示会导致您在之后看到的布局。 Using setLayout(null) may work, but the preferred approach is to use the intended layout manager . 使用setLayout(null)可能有效,但首选方法是使用预期的布局管理器

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

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