我有一個面板,我試圖從中刪除它添加到它的標籤在運行時。 但是當標籤成功移除時,我無法使用該標籤留下的空間,再次向其中添加任何標籤。如何從容器中刪除任何組件時重用容器中的區域?
感謝預期的解決方案。
這裏是有關的代碼片段:
標註添加到面板:
JLabel jl = new JLabel(); jl.setOpaque(true); jl.setIcon(new ImageIcon("D:/Project/router2.jpg")); jl.setBounds(x, y, jl.getPreferredSize().width, jl.getPreferredSize().height); for (Component c : lcomponent) { flag = true; Rectangle r4 = c.getBounds(); int x1 = (int) r4.getX(); int y1 = (int) r4.getY(); Rectangle r5 = new Rectangle( new Point(x1 - 60, y1 - 60), new Dimension(170, 170)); if (r5.contains(p)) { //To ensure that two labels do not overlap flag = false; //or are too close to each other break; } } if (flag) { p2.add(jl); //p2 is a panel Component c2 = p2.getComponentAt(x, y); p2.repaint(); lcomponent.add(c2); //lcomponent is an ArrayList<Component> to //store all the labels added to the panel }
去除標籤:
p2.remove(<label name>); p2.repaint();
我甲肝嘗試revalidate()也,但我不知道它爲什麼它自動將頂部的一行 中的組件對齊。
幫我這也
我已經重新格式化了您的代碼;如果不正確請回復。 – trashgod 2010-04-01 18:19:56