简体   繁体   English

从UI调整Java Swing组件的大小

[英]Resize java swing component from UI

I need to make resizable java swing component (JPanel), so when I click on the border of that component and drag with cursor to somewhere else, the component will change size by mouse position. 我需要制作可调整大小的Java Swing组件(JPanel),所以当我单击该组件的边框并将光标拖动到其他位置时,该组件将通过鼠标位置更改大小。 Has this any easy solution? 有什么简单的解决方案吗?

Attach a MouseListener to the component, record mouse position at start of dragging in mouse pressed method after checking whether the mouse is pressed on what you have defined as border, then resize the component in mouse released method calculating the size difference by subtracting mouse positions at start and end of dragging. MouseListener附加到组件上,在检查是否在定义的边框上按下了鼠标之后,在按鼠标按下的方法开始拖动时记录鼠标的位置,然后在鼠标释放方法中调整组件的大小,通过减去以下位置来计算大小差拖动的开始和结束。

If you need / want continuous resizing while you drag, use a MouseInputListener instead, record the size of the component too in the mouse pressed method and do the resizing in the mouse moved method. 如果您需要/想要在拖动时连续调整大小,请改用MouseInputListener ,同时在鼠标按下的方法中记录组件的大小,并在鼠标移动的方法中进行大小调整。

If you also want to have a visible border, you can maybe add that border to the component and add the mouse listener to the border, so that you know you are on the border when you get the pressed event. 如果您还希望有一个可见的边框,则可以将该边框添加到组件中,然后将鼠标侦听器添加到该边框,以便在收到按下事件时知道自己在边框上。

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

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