简体   繁体   English

如何阻止JTextArea放在JTabbedPane顶部?

[英]How to stop JTextArea from being placed on top of JTabbedPane?

I am trying to design a layout with a JTabbedPane at the top of the frame and then a jLogArea below the tabbed pane. 我正在尝试设计一种布局,在框架顶部使用JTabbedPane,然后在选项卡式窗格的下面使用jLogArea。

I am using this code: 我正在使用此代码:

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

However, the result of this is that the text area is placed behind the tabbed pane: 但是,这样做的结果是将文本区域放置在选项卡式窗格的后面:

在此处输入图片说明

Does anyone know what I am doing wrong and how I can fix it? 有谁知道我在做什么错以及如何解决? Thanks. 谢谢。

EDIT: Just to be clear, I am looking for the text area to be below the JTabbedPane, not in the tab iself. 编辑:为了清楚起见,我正在寻找JTabbedPane下面的文本区域,而不是在制表符iself中。

Using BorderLayout.NORTH and BorderLayout.SOUTH does not help either. 使用BorderLayout.NORTHBorderLayout.SOUTH也无济于事。 I added a label into the tab's contents just to see if that would make a difference but the text area still goes behind, this is how it looks: 我在标签的内容中添加了一个标签,以查看是否会有所不同,但文本区域仍然落后,这是它的外观:

在此处输入图片说明

Further code (the class extends JFrame ): 进一步的代码(该类扩展了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);
}

UPDATE 更新
I think you are looking for something like this: 我认为您正在寻找这样的东西:
在此处输入图片说明

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

Your code should be like this: 您的代码应如下所示:

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

The problem in your code was that you added the JScrollPane at the same level as you added your JTabbedPane . 代码中的问题是,您添加的JScrollPane与添加JTabbedPane级别相同。 Actually your JScrollPane has to be a tab component: 实际上,您的JScrollPane必须是一个选项卡组件:

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

EDIT: to have the scroll pane below the tabbed pane: 编辑:在选项卡式窗格下面有滚动窗格:

... ...

add(tabbedPane, BorderLayout.NORTH);

... ...

add(scrollableTextArea, BorderLayout.SOUTH);

Hope this works for you. 希望这对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM