我正在尝试设计一个布局,其中框架顶部有一个 JTabbedPane,然后选项卡式 Pane 下方有一个 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);
但是,这样做的结果是文本区域被放置在选项卡式 Pane 后面:
有谁知道我做错了什么以及如何解决它?谢谢。
编辑:为了清楚起见,我正在寻找位于 JTabbedPane 下方的文本区域,而不是在选项卡本身中。
使用 BorderLayout.NORTH
和 BorderLayout.SOUTH
也没有帮助。我在选项卡的内容中添加了一个标签,只是为了看看这是否会产生影响,但文本区域仍然位于后面,这就是它的外观:
更多代码(该类扩展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);
}
最佳答案
更新
我认为您正在寻找这样的东西:
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();
}
});
}
}
关于java - 如何阻止 JTextArea 放置在 JTabbedPane 之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15688468/