2014-12-05 83 views
0

除了只有一個區域:North以外,我想要一個佈局管理器,如邊界佈局管理器。Java Swing:需要BorderLayout中的「North」的佈局管理器

邊界佈局管理器的北部區域對我來說工作正常(即佔用正確的空間),但是中心正在佔用所有可用空間(請參見下面的藍色區域)。理想情況下,我希望中心佔據零空間。

這可能嗎?

謝謝。

實施例1的2(PIC)

example

實施例2 2(代碼)

import java.awt.*; 

import javax.swing.*; 
import javax.swing.text.html.HTMLEditorKit; 

public class test 
{ 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
      JFrame j = new JFrame("Test"); 
     WorkBench right = new WorkBench(); 

     JScrollPane sp 
      = new JScrollPane(
        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 

     sp.setAutoscrolls(false); 
     sp.setViewportView(right); 

     j.getContentPane().add(sp, BorderLayout.CENTER); 

     right.addPortal(getNewPortalPane()); 
     right.addPortal(getNewPortalPane()); 

     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() { 
       sp.getViewport().setViewPosition(new java.awt.Point(0, 0)); 
      } 
     }); 


     // center the jframe, then make it visible 
     j.setSize(800, 600); 
     j.setLocationRelativeTo(null); 
     j.setVisible(true); 
     } 
    }); 
    } 


public static JPanel getNewPortalPane() 
{ 
    JEditorPane html_box = new JEditorPane(); 
    html_box.setEditorKit(new HTMLEditorKit()); 
    html_box.setOpaque(true); 
    html_box.setBackground(Color.WHITE); 

    html_box.setContentType("text/html"); 

    String htmlString = "<html>\n" 
     + "<body>\n" 
     + "<h1>Welcomfve! sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h1>\n" 
     + "<h2>This is an H2 header. sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h2>\n" 
     + "<p>This is some sample text sdsdsdsdsd sdsdsd sdsd sdsd sdsd </p>\n" 
     + "<p><a href=\"http://dsfsdfdf.com/blog/\">sdfsdfsdf blog</a></p>\n" 
     + "</body>\n"; 

    html_box.setText(htmlString); 


    JPanel p = new JPanel(new BorderLayout()); 
    p.setBackground(Color.blue); 
    p.add(html_box, BorderLayout.NORTH); 

    return p; 
} 

public static class WorkBench extends JPanel implements Scrollable 
{ 
    private Box vertical_box = null; 

    public WorkBench() 
    { 
     setLayout(new BorderLayout()); 

     this.vertical_box = Box.createVerticalBox(); 
     add(this.vertical_box , BorderLayout.CENTER); 
    } 

    public void addPortal(JPanel portal) 
    { 
     this.vertical_box.add(portal); 

     validate(); 
    } 

    @Override 
    public Dimension getPreferredScrollableViewportSize() 
    { 
     return getPreferredSize(); 
    } 


    @Override 
    public int getScrollableUnitIncrement(
      Rectangle visibleRect, 
      int orientation, 
      int direction) 
    { 
     return 20; 
    } 


    @Override 
    public int getScrollableBlockIncrement(
      Rectangle visibleRect, 
      int orientation, 
      int direction) 
    { 
     return 60; 
    } 


    @Override 
    public boolean getScrollableTracksViewportWidth() 
    { 
     return true; 
    } 


    @Override 
    public boolean getScrollableTracksViewportHeight() 
    { 
     if (getParent() instanceof JViewport) 
     { 
      return (((JViewport)getParent()).getHeight() > getPreferredSize().height); 
     } 

     return false; 
    } 
} 

}

+0

好了...所以這將意味着'NORTH'位置的行爲將與'CENTER'位置... – MadProgrammer 2014-12-05 10:16:46

+0

我看看你但是北部地區目前正在佔用其內容所需的最小空間,所以如果我能獲得同樣的效果,如果沒有佔據所有空間的中心區域 - 請參閱上面的藍色區域 - 我想要的空間以填補其他的東西。 – SoupMonster 2014-12-05 10:22:04

+1

'GridBagLayout',使用'GridBagConstraints''weightx'設置爲'1','gridwidth'設置爲'REMAINDER'並且'fill'設置爲'HORIZONTAL' ... – MadProgrammer 2014-12-05 10:25:45

回答

1

有可能是一個更好的解決方案的某處,但什麼我發現作品正在使用BorderLayout.NORTH添加內部面板,使用將內部面板的佈局設置爲,然後將組件添加到內部面板。所以,在你的榜樣將它(編輯修復滾動條):

public class test { 

    public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 

     public void run() { 
     JFrame j = new JFrame("Test"); 
     WorkBench right = new WorkBench(); 

     final JScrollPane sp = new JScrollPane(
      JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
      JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 

     sp.setAutoscrolls(false); 
     sp.setViewportView(right); 

     j.getContentPane().add(sp, BorderLayout.CENTER); 

     right.addPortal(getNewPortalPane()); 
     right.addPortal(getNewPortalPane()); 

     SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 
      sp.getViewport().setViewPosition(new java.awt.Point(0, 0)); 
      } 
     }); 

     // center the jframe, then make it visible 
     j.setSize(800, 600); 
     j.setLocationRelativeTo(null); 
     j.setVisible(true); 
     } 
    }); 
    } 

    public static JPanel getNewPortalPane() { 
    JEditorPane html_box = new JEditorPane(); 
    html_box.setEditorKit(new HTMLEditorKit()); 
    html_box.setOpaque(true); 
    html_box.setBackground(Color.WHITE); 

    html_box.setContentType("text/html"); 

    String htmlString = "<html>\n" 
     + "<body>\n" 
     + "<h1>Welcomfve! sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h1>\n" 
     + "<h2>This is an H2 header. sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h2>\n" 
     + "<p>This is some sample text sdsdsdsdsd sdsdsd sdsd sdsd sdsd </p>\n" 
     + "<p><a href=\"http://dsfsdfdf.com/blog/\">sdfsdfsdf blog</a></p>\n" 
     + "</body>\n"; 

    html_box.setText(htmlString); 

    JPanel p = new JPanel(new BorderLayout()); 
    p.setBackground(Color.blue); 
    p.add(html_box, BorderLayout.NORTH); 

    return p; 
    } 

    public static class WorkBench extends JPanel implements Scrollable { 

    private final JPanel inner; 

    public WorkBench() { 
     setLayout(new BorderLayout()); 
     inner = new JPanel(); 
     inner.setLayout(new BoxLayout(inner, BoxLayout.PAGE_AXIS)); 
     add(inner, BorderLayout.NORTH); 
    } 

    public void addPortal(JPanel portal) { 
     inner.add(portal); 
     validate(); 
    } 

    @Override 
    public Dimension getPreferredScrollableViewportSize() { 
     return getPreferredSize(); 
    } 

    @Override 
    public int getScrollableUnitIncrement(Rectangle visibleRect, 
     int orientation, int direction) { 
     return 20; 
    } 

    @Override 
    public int getScrollableBlockIncrement(Rectangle visibleRect, 
     int orientation, int direction) { 
     return 60; 
    } 

    @Override 
    public boolean getScrollableTracksViewportWidth() { 
     return true; 
    } 

    @Override 
    public boolean getScrollableTracksViewportHeight() { 
     if (getParent() instanceof JViewport) { 
     return (((JViewport) getParent()).getHeight() > getPreferredSize().height); 
     } 

     return false; 
    } 
    } 
} 
+0

太棒了!工作得很好。謝謝! – SoupMonster 2014-12-05 14:38:21