如何阻止JTextArea置于JTabbedPane之上?

时间:2013-03-28 17:34:17

标签: java swing jscrollpane jtabbedpane border-layout

我正在设计一个布局,框架顶部有一个JTabbedPane,然后是选项卡窗格下面的一个jLogArea。

我正在使用此代码:

setLayout(new BorderLayout());

tabbedPane.setSize(WIDTH, HEIGHT);
add(tabbedPane, BorderLayout.PAGE_START);
tabbedPane.add("Tab 0", null);

scrollableTextArea = new JScrollPane(jTextArea);

jTextArea.setEditable(false);
jTextArea.setLineWrap(true);
scrollableTextArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollableTextArea.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollableTextArea, BorderLayout.PAGE_END);

但是,结果是文本区域位于选项卡式窗格的后面:

enter image description here

有谁知道我做错了什么以及如何解决它?感谢。

编辑:为了清楚起见,我正在寻找文本区域在JTabbedPane下方,而不是在标签iself中。

使用BorderLayout.NORTHBorderLayout.SOUTH也无济于事。我在标签的内容中添加了一个标签,看看是否会产生影响,但文字区域仍然落后,这就是它的外观:

enter image description here

进一步的代码(类扩展JFrame):

public MainGUI() {
    init();
    pack();
    super.setTitle("test");
}

public void init() {
    setLayout(new BorderLayout());
    setPreferredSize(new Dimension(WIDTH, HEIGHT + TEXT_AREA_HEIGHT));
    setMaximumSize(new Dimension(WIDTH, HEIGHT + TEXT_AREA_HEIGHT));
    setMinimumSize(new Dimension(WIDTH, HEIGHT + TEXT_AREA_HEIGHT));

    tabbedPane = new JTabbedPane();
    textArea = new JTextArea(WIDTH, TEXT_AREA_HEIGHT);
    scrollableTextArea = new JScrollPane(textArea);

    JLabel testLabel = new JLabel("Test!");
    tabbedPane.add("Tab 0", testLabel);

    tabbedPane.setBorder(null);
    tabbedPane.setSize(WIDTH, HEIGHT);
    add(tabbedPane, BorderLayout.NORTH);

    textArea.setEditable(false);
    textArea.setLineWrap(true);
    scrollableTextArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scrollableTextArea.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    add(scrollableTextArea, BorderLayout.SOUTH);

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setResizable(false);
    setVisible(true);
}

2 个答案:

答案 0 :(得分:1)

更新
我认为您正在寻找以下内容:
enter image description here

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;

public class TabSample extends JFrame{
  public void createAndShowGUI() {
    JPanel panel = new JPanel();
    JTextArea ta = new JTextArea(100,50);
    JScrollPane jsp = new JScrollPane(ta);
    JTabbedPane tabbedPane = new JTabbedPane();
    panel.setLayout(new BorderLayout());
    tabbedPane.addTab("Tab one", panel);
    JSplitPane vPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tabbedPane, jsp);
    getContentPane().add(vPane);
    setSize(400,500);
    vPane.setDividerLocation(getHeight()/2);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }

  public static void main(String args[]) 
  {
      javax.swing.SwingUtilities.invokeLater(new Runnable()
      {
          @Override
          public void run()
          {
              TabSample ts = new TabSample();
              ts.createAndShowGUI();
          }
      });
  }
}

答案 1 :(得分:0)

您的代码应该是这样的:

setLayout(new BorderLayout());

tabbedPane.setSize(WIDTH, HEIGHT);
add(tabbedPane, BorderLayout.PAGE_START);

scrollableTextArea = new JScrollPane(jTextArea);

jTextArea.setEditable(false);
jTextArea.setLineWrap(true);
scrollableTextArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollableTextArea.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
tabbedPane.addTab("Tab 0", scrollableTextArea);

您的代码中的问题是您添加的JScrollPane与添加JTabbedPane的级别相同。实际上,您的JScrollPane必须是标签组件:

tabbedPane.addTab("Tab 0", scrollableTextArea);

编辑:在选项卡式窗格下方显示滚动窗格:

...

add(tabbedPane, BorderLayout.NORTH);

...

add(scrollableTextArea, BorderLayout.SOUTH);

希望这适合你。