2012-11-21 25 views
1

這個問題,Java Swing: Vertical Layout with fixed width and variable height,給了我一個機會去嘗試阿帕奇樞紐。我有一天的Pivot經驗。漿紗阿帕奇樞軸GUI組件到窗口

這裏的GUI我已經能夠創建。

Segments GUI

我的問題是:

  1. 怎樣大小的TextArea那是Expander的一部分,從而使ExpanderScrollPaneTextArea填滿窗口的寬度, TextArea高得足以容納任意數量的文本?

  2. 如何大小ExpanderScrollPane的高度,這樣,當所有的文字區域擴大,3個Expander小號適合窗口的高度?

這裏是創建GUI的源代碼。我正在使用Apache Pivot 2.0.2版。

import java.io.IOException; 
import java.io.Reader; 
import java.io.StringReader; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.pivot.collections.Map; 
import org.apache.pivot.wtk.Application; 
import org.apache.pivot.wtk.BoxPane; 
import org.apache.pivot.wtk.DesktopApplicationContext; 
import org.apache.pivot.wtk.Display; 
import org.apache.pivot.wtk.Expander; 
import org.apache.pivot.wtk.Orientation; 
import org.apache.pivot.wtk.ScrollPane; 
import org.apache.pivot.wtk.TextArea; 
import org.apache.pivot.wtk.Window; 

public class Segments implements Application { 

    protected SectionCollection collection; 

    protected Window window; 

    @Override 
    public void startup(Display display, Map<String, String> properties) { 
     collection = new SectionCollection(); 
     new SectionCollectionCreator(collection); 

     window = new Window(); 
     window.setTitle("Segments"); 
     window.setMaximized(true); 

     BoxPane boxPane = new BoxPane(); 
     boxPane.setOrientation(Orientation.VERTICAL); 

     for (int i = 0; i < collection.size(); i++) { 
      SectionText sectionText = collection.get(i); 

      TextArea textArea = new TextArea(); 
      textArea.setEditable(false); 
      textArea.setPreferredSize(400, 220); 
      try { 
       textArea.setText(sectionText.getTopic()); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      ScrollPane textScrollPane = new ScrollPane(); 
      textScrollPane.setPreferredSize(420, 100); 
      textScrollPane.setVerticalScrollBarPolicy(
        ScrollPane.ScrollBarPolicy.AUTO); 
      textScrollPane.setView(textArea); 

      Expander expander = new Expander(); 
      expander.setTitle(sectionText.getTitle()); 
      expander.setContent(textScrollPane); 
      expander.setExpanded(false); 

      boxPane.add(expander); 
     } 

     ScrollPane expanderScrollPane = new ScrollPane(); 
     expanderScrollPane.setHorizontalScrollBarPolicy(
       ScrollPane.ScrollBarPolicy.AUTO); 
     expanderScrollPane.setVerticalScrollBarPolicy(
       ScrollPane.ScrollBarPolicy.AUTO); 
     expanderScrollPane.setView(boxPane); 

     window.setContent(expanderScrollPane); 
     window.open(display); 
    } 

    @Override 
    public boolean shutdown(boolean optional) { 
     if (window != null) { 
      window.close(); 
     } 
     return false; 
    } 

    @Override 
    public void suspend() { 
    } 

    @Override 
    public void resume() { 
    } 

    public static void main(String[] args) { 
     DesktopApplicationContext.main(Segments.class, args); 
    } 

    public class SectionText { 
     protected String title; 
     protected Reader topic; 

     public SectionText(String title, Reader topic) { 
      this.title = title; 
      this.topic = topic; 
     } 

     public String getTitle() { 
      return title; 
     } 

     public Reader getTopic() { 
      return topic; 
     } 
    } 

    public class SectionCollection { 
     protected List<SectionText> collection; 

     public SectionCollection() { 
      this.collection = new ArrayList<SectionText>(); 
     } 

     public void add(SectionText sectionText) { 
      collection.add(sectionText); 
     } 

     public int size() { 
      return collection.size(); 
     } 

     public SectionText get(int index) { 
      return collection.get(index); 
     } 
    } 

    public class SectionCollectionCreator { 
     protected SectionCollection collection; 

     protected static final String text = "Attributes, Styles and Style Contexts\n\n" 
       + "The simple PlainDocument class that you saw in the previous " 
       + "chapter is only capable of holding text. The more complex text " 
       + "components use a more sophisticated model that implements the " 
       + "StyledDocument interface. StyledDocument is a sub-interface of " 
       + "Document that contains methods for manipulating attributes that " 
       + "control the way in which the text in the document is displayed. " 
       + "The Swing text package contains a concrete implementation of " 
       + "StyledDocument called DefaultStyledDocument that is used as the " 
       + "default model for JTextPane and is also the base class from which " 
       + "more specific models, such as the HTMLDocument class that handles " 
       + "input in HTML format, can be created. In order to make use of " 
       + "DefaultStyledDocument and JTextPane, you need to understand how " 
       + "Swing represents and uses attributes."; 

     public SectionCollectionCreator(SectionCollection collection) { 
      this.collection = collection; 

      String title = "Title "; 

      for (int i = 1; i <= 3; i++) { 
       SectionText sectionText = new SectionText(title + i, 
         new StringReader(text)); 
       collection.add(sectionText); 
      } 
     } 
    } 

} 

回答

0

您也可能要考慮把你的組件的FillPane,這使它們的大小以填充可用區域內。這似乎是Pivot做事的方式。

+0

謝謝。我仍然在學習樞軸。 –

0

我終於解決了這個難題。

通過設定文本區域的寬度,高度,計算以適合文本。

通過設置文本區域滾動窗格的高度,我可以使文本區滾動窗格出現,和膨脹的尺寸減小到標題欄加文本區域滾動窗格。

所以,現在,我不得不做的是找出文本區域的寬度,並根據窗口的寬度和高度的文本區域滾動窗格的高度。

這就是組件偵聽器發揮作用的地方。偵聽器調整文本區域的寬度和文本區域滾動窗格的高度。我欺騙並使用魔術常量來說明滾動條和邊距。如果我可以在創建組件時獲得這些值,那本來就不錯。

不管怎樣,下面是大小的GUI組件窗口中的代碼。

import java.io.IOException; 
import java.io.Reader; 
import java.io.StringReader; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Iterator; 
import java.util.List; 

import org.apache.pivot.collections.Map; 
import org.apache.pivot.wtk.Application; 
import org.apache.pivot.wtk.Bounds; 
import org.apache.pivot.wtk.BoxPane; 
import org.apache.pivot.wtk.Component; 
import org.apache.pivot.wtk.Component.StyleDictionary; 
import org.apache.pivot.wtk.ComponentListener; 
import org.apache.pivot.wtk.DesktopApplicationContext; 
import org.apache.pivot.wtk.Display; 
import org.apache.pivot.wtk.Expander; 
import org.apache.pivot.wtk.Orientation; 
import org.apache.pivot.wtk.ScrollPane; 
import org.apache.pivot.wtk.TextArea; 
import org.apache.pivot.wtk.Window; 

public class Segments implements Application { 

    protected List<SectionComponent> expanderComponents; 

    protected SectionCollection collection; 

    protected Window window; 

    @Override 
    public void startup(Display display, Map<String, String> properties) 
      throws IOException { 
     getStyles(display); 

     collection = new SectionCollection(); 
     new SectionCollectionCreator(collection); 
     expanderComponents = new ArrayList<SectionComponent>(); 

     window = new Window(); 
     window.setTitle("Segments"); 
     window.setMaximized(true); 

     ComponentSizeListener listener = new ComponentSizeListener(); 
     window.getComponentListeners().add(listener.getListener()); 

     BoxPane boxPane = new BoxPane(); 
     boxPane.setOrientation(Orientation.VERTICAL); 

     for (int i = 0; i < collection.size(); i++) { 
      SectionComponent sectionComponent = 
        new SectionComponent(collection.get(i), 400, 100); 
      expanderComponents.add(sectionComponent); 
      boxPane.add(sectionComponent.getExpander()); 
     } 

     listener.setComponents(expanderComponents); 

     ScrollPane expanderScrollPane = new ScrollPane(); 
     expanderScrollPane.setHorizontalScrollBarPolicy(
       ScrollPane.ScrollBarPolicy.AUTO); 
     expanderScrollPane.setVerticalScrollBarPolicy(
       ScrollPane.ScrollBarPolicy.AUTO); 
     expanderScrollPane.setView(boxPane); 

     window.setContent(expanderScrollPane); 
     window.open(display); 
    } 

    protected void getStyles(Component component) { 
     StyleDictionary dictionary = component.getStyles(); 
     System.out.println(component); 
     Iterator<String> iter = dictionary.iterator(); 
     List<String> list = new ArrayList<String>(); 
     while (iter.hasNext()) { 
      list.add(iter.next()); 
     } 
     Collections.sort(list); 
     for (String style : list) { 
      System.out.println(" " + style); 
     } 
    } 

    @Override 
    public boolean shutdown(boolean optional) { 
     if (window != null) { 
      window.close(); 
     } 
     return false; 
    } 

    @Override 
    public void suspend() { 
     Bounds bounds = window.getClientArea(); 
     System.out.println(bounds); 
    } 

    @Override 
    public void resume() { 
    } 

    public static void main(String[] args) { 
     DesktopApplicationContext.main(Segments.class, args); 
    } 

    public class ComponentSizeListener { 
     protected int newWidth; 
     protected int newHeight; 

     protected ComponentListener listener; 

     protected List<SectionComponent> components; 

     public ComponentSizeListener() { 
      this.newWidth = 425; 
      this.newHeight = 131; 

      this.listener = new ComponentListener.Adapter() { 
       @Override 
       public void sizeChanged(Component component, 
         int previousWidth, int previousHeight) { 
        newWidth = component.getWidth(); 
        newHeight = component.getHeight(); 
        for (SectionComponent sectionComponent : components) { 
         sectionComponent.setTextAreaWidth(newWidth - 25); 
         int paneHeight = (newHeight/components.size()) - 34; 
         sectionComponent.setTextScrollPaneHeight(paneHeight); 
        } 
       } 
      }; 
     } 

     public void setComponents(List<SectionComponent> components) { 
      this.components = components; 
     } 

     public ComponentListener getListener() { 
      return listener; 
     } 
    } 

    public class SectionComponent { 
     protected int textAreaWidth; 
     protected int textScrollPaneHeight; 

     protected Expander expander; 
     protected ScrollPane textScrollPane; 
     protected SectionText sectionText; 
     protected TextArea textArea; 

     public SectionComponent(SectionText sectionText, 
       int textAreaWidth, int textScrollPaneHeight) 
         throws IOException { 
      this.sectionText = sectionText; 
      this.textAreaWidth = textAreaWidth; 
      this.textScrollPaneHeight = textScrollPaneHeight; 
      createPartControl(); 
     } 

     protected void createPartControl() throws IOException { 
      textArea = new TextArea(); 
      textArea.setEditable(false); 
      textArea.setPreferredWidth(textAreaWidth); 
      textArea.setText(sectionText.getTopic()); 

      textScrollPane = new ScrollPane(); 
      textScrollPane.setPreferredHeight(textScrollPaneHeight); 
      textScrollPane.setVerticalScrollBarPolicy(
        ScrollPane.ScrollBarPolicy.AUTO); 
      textScrollPane.setView(textArea); 

      expander = new Expander(); 
      expander.setTitle(sectionText.getTitle()); 
      expander.setContent(textScrollPane); 
      expander.setExpanded(false); 
     } 

     public Expander getExpander() { 
      return expander; 
     } 

     public void setTextAreaWidth(int textAreaWidth) { 
      this.textAreaWidth = textAreaWidth; 
      textArea.setPreferredWidth(textAreaWidth); 
     } 

     public void setTextScrollPaneHeight(int textScrollPaneHeight) { 
      this.textScrollPaneHeight = textScrollPaneHeight; 
      textScrollPane.setPreferredHeight(textScrollPaneHeight); 
     } 

    } 

    public class SectionText { 
     protected String title; 
     protected Reader topic; 

     public SectionText(String title, Reader topic) { 
      this.title = title; 
      this.topic = topic; 
     } 

     public String getTitle() { 
      return title; 
     } 

     public Reader getTopic() { 
      return topic; 
     } 
    } 

    public class SectionCollection { 
     protected List<SectionText> collection; 

     public SectionCollection() { 
      this.collection = new ArrayList<SectionText>(); 
     } 

     public void add(SectionText sectionText) { 
      collection.add(sectionText); 
     } 

     public int size() { 
      return collection.size(); 
     } 

     public SectionText get(int index) { 
      return collection.get(index); 
     } 
    } 

    public class SectionCollectionCreator { 
     protected SectionCollection collection; 

     protected static final String text = "Attributes, Styles and Style Contexts\n\n" 
       + "The simple PlainDocument class that you saw in the previous " 
       + "chapter is only capable of holding text. The more complex text " 
       + "components use a more sophisticated model that implements the " 
       + "StyledDocument interface. StyledDocument is a sub-interface of " 
       + "Document that contains methods for manipulating attributes that " 
       + "control the way in which the text in the document is displayed. " 
       + "The Swing text package contains a concrete implementation of " 
       + "StyledDocument called DefaultStyledDocument that is used as the " 
       + "default model for JTextPane and is also the base class from which " 
       + "more specific models, such as the HTMLDocument class that handles " 
       + "input in HTML format, can be created. In order to make use of " 
       + "DefaultStyledDocument and JTextPane, you need to understand how " 
       + "Swing represents and uses attributes."; 

     public SectionCollectionCreator(SectionCollection collection) { 
      this.collection = collection; 

      String title = "Title "; 

      for (int i = 1; i <= 3; i++) { 
       SectionText sectionText = new SectionText(title + i, 
         new StringReader(text)); 
       collection.add(sectionText); 
      } 
     } 
    } 

}