【发布时间】:2013-05-29 11:33:59
【问题描述】:
我正在尝试编写 java 程序,它允许我动态创建新控件,并通过鼠标移动它们。 现在我可以移动它们,但是当我创建新对象时,每个对象都会返回到其初始位置。原因很明显 - 我在大型机上使用 pack() 方法,但我不知道该怎么做。
说明:
- myPanel - 我的扩展 JPanel 的类
- PanelList - ArrayList 包含生成的面板
- addmyPanel - 将新面板添加到 PanelList 的方法
-
getmyPanel - 返回具有特定 id 的 myPanel
private void jButton0ActionActionPerformed(ActionEvent event) { myPanel temp = new myPanel(); addmyPanel(); for (int i =0 ;i < PanelList.size(); i++){ temp = PanelList.get(i); if (!temp.getIsActive()){ try{ add(getmyPanel(temp.getId()), new Constraints(new Leading(35, 100, 10, 10), new Leading(44, 100, 12, 12))); PanelList.get(i).setIsActive(); } catch (NoSuchElementException e){} } } frame.pack(); }
【问题讨论】:
-
不太清楚你在这里问什么。你试过 frame.revalidate() 吗?
-
你能提供截图吗?
-
听起来像是使用了不恰当的
LayoutManager。确保使用能够“保留”已添加组件位置的工具。 -
i39.tinypic.com/15x77kp.png - 程序窗口 i41.tinypic.com/2rf829i.png - 单击按钮后 i41.tinypic.com/9916ow.png - 面板被鼠标移动 i41.tinypic.com/2rf829i.jpg - 第二次点击 i41.tinypic.com/w0gwli.png - 两个面板都被鼠标移动我试过 frame.revalidate () 正如您所建议的那样,它看起来相同而哪个 LayoutManager 可以做这样的事情?我是这方面的新手