1

I've got MyJPanel(extends JPanel). Each MyJPanel object has method GetID(). When I create it I set ID with constructor(but also there is method SetID()), set size and then create Jscrollpane and add it to JInternalFrame. All frames are in ArrayList<JInternalFrame> arr.

JInternalFrame frame = new JInternalFrame("Inner frame",true,true,true,true);
final MyJPanel panel = new MyJPanel(f.getAbsolutePath(),count);
panel.setSize(panel.getWidth()/6,panel.getHeight()/6);
JScrollPane pane = new JScrollPane(panel);
pane.setPreferredSize(new Dimension(theDesktop.getWidth() / 2, theDesktop.getHeight() / 2));
frame.getContentPane().add(pane, BorderLayout.CENTER);

To delete frame I add add FrameListener and method internalFrameClosing method

public void internalFrameClosing(InternalFrameEvent e) {
       int index = panel.GetID();//get index of panel окна
       if (index == arr.size())
          arr.remove(index);//remove last element
       else{
          //reset all indexes of JInternalFrames' MyJPanel
       }
}

But I don't know how to reset values for MyJPanels in array of JInternalFrames when one of the frames was deleted because 1)MyJPanel is in JScrollPane. method SetID 2)JScrollPane is in JInternalFrame 3)JInternalFrame is in the array. No method SetID() in arr.get(i).

4

1 回答 1

2

一个简单的解决方案是拥有一个 Map (例如 HashMap)HashMap<JInternalFrame, MyJPanel>——这将允许您轻松地将 MyJPanel 与保存它的内部框架相关联。然后,当您遍历 JInternalFrames 时,很容易检索每个持有的 MyJPanels。

就像是:

public void internalFrameClosing(InternalFrameEvent e) {
  int index = panel.GetID();
  arr.remove(index);
  if (index < arr.size()) {
     for (int i = 0; i < arr.size(); i++) {
        JInternalFrame internalFrame = arr.get(i);
        MyJPanel myPanel = framePanelMap.get(internalFrame);
        myPanel.setID(i);
     }
  }
}

顺便说一句,您确实知道这if (index == arr.size())永远不会是真的,因为如果您的索引是列表中的索引,那么 index 将保存一个介于 0 和arr.size() - 1永远不会 ==之间的值arr.size()

虽然这有点杂乱无章。在我看来,更好的解决方案可能是重新设计代码并将代码的模型逻辑与代码的视图部分分开,这样您就可以拥有模型的 ArrayList 而不是 JInternalFrames。

这样,如果以后您决定不想使用 JInternalFrames 来显示此信息,则不必更改模型的基本逻辑结构。

于 2012-04-29T13:39:55.683 回答