2016-12-14 60 views
1

GUI example數據模型


我想介紹類似於前面示例的GUI。

  • 在左側,有段落(在段落類的實例)包含一個 集合(絃樂)的集合。正如你所看到的 - 在文字中,有高亮文本片段(綠色,橙色,藍色)
  • 文本片段是來自具體段落的字的子集合。
  • 在右側,也有事實(在事實類的實例)的集合。
  • 每個事實與至少一個文本片段(可能更多 - 例如事實#2)相關聯。

代碼片段:

public class Paragraph { 
    private List<String> words; 
    … 
} 

public class Fact { 
    private String text; 
    … 
} 

我希望,這將是一個交互GUI(用戶可以點擊該文本片段=>例如對應實際上將是彈出窗口/用戶可以點擊事實=>例如只有相應的文本片段保持突出顯示)。

由於這個原因,每個文本片段必須知道相應的事實,反之亦然。

如何在Java中實現文本片段與相應事實之間的聯繫以及什麼是合適的數據結構?

我的意思是這個連接的數據模型,而不是GUI的實現。我認爲單詞數量遠遠高於文本片段的數量。

+0

http:// stackoverflow。com/questions/9783020 /雙向地圖 –

+0

@ PM77-1是的,類似於我需要的雙向地圖。實際上,我對**文本片段的數據結構提議存在問題。如何處理這些信息?我爲每個String **單詞**考慮特殊**類**,但大部分單詞不是某些文本片段的一部分。 – David

+0

@ user3437460是的,如果你是指從具體的**文本片段**返回到** fact **。 – David

回答

1

假設1個事實可以有多個段落,但1個段落只能屬於1個事實。我將實現類,就像這樣:

class Fact{ 

    private ArrayList<ParagraphText> paraTexts; 
    private String text;   
    //any other attributes 

    public Fact(){ 
     //initialization.. 
    } 

    public void addParaText(ParagraphText p){ 
     paraTexts.add(p); 
     p.linkWithFact(this); //remember which fact p belongs to 
    } 

    public void removeParaText(ParagraphText p){ 
     if(paraTexts.contains(p)){ 
      p.unlinkFact(); 
      paraTexts.remove(p); 
     } 
    } 
} 

只要添加一個段落文本的事實,該段的文字本身記住它所屬的事實。

class ParagraphText{ 

    private int startPos; 
    private int endPos; 
    private Fact fact; 
    //any other attributes 

    public ParagraphText(int startPos, int endPos){ 
     this.startPos = startPos; 
     this.endPos = endPos; 
     //any other initializations 
    } 

    public void linkWithFact(Fact fact){ 
     this.fact = fact; 
    } 

    public void unlinkFact(){ 
     this.fact = null; 
    } 
} 
+0

@David讓我知道它是否有幫助。 – user3437460

+0

謝謝,但我認爲我們對**段**有不同的看法。在這個上下文中的**段落**只是像**字樣**的包裝的包裝物。 **事實**與整個**段**無關,但僅與段落的某些詞相關。我的例子中的*事實#1 *與** 7有關。和8.第**段的第**段**。對不起,也許從我的介紹描述中看不清楚。 – David

+0

@David我試圖使用較短的名稱來提高可讀性。這並不意味着它是整個段落,否則我不會包括開始和結束位置。爲了清晰起見,我編輯了命名。 – user3437460