我只希望有一个边框管理器之类的布局管理器,只是只有一个区域:北。

Border Layout Manager的北部区域对我来说工作正常(即占用正确的空间),但是中央占据了所有可用空间(请参见下面的蓝色区域)。理想情况下,我希望中心占据零空间。

这可能吗?

谢谢。

示例2之1(图片)



示例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;
    }
}


}

最佳答案

某处可能有一个更优雅的解决方案,但是我发现可以使用BorderLayout.NORTH添加内部面板,使用BoxLayout将内部面板的布局设置为PAGE_AXIS,然后将组件添加到内部面板。因此,在您的示例中,它将被(编辑以修复滚动条):

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;
    }
  }
}

关于java - Java Swing:需要BorderLayout中的布局管理器(如“North”),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27313545/

10-10 15:33