在JFrame的JTabbedPane中调整JScrollPane的大小 [英] Sizing a JScrollPane in a JTabbedPane in a JFrame

查看:305
本文介绍了在JFrame的JTabbedPane中调整JScrollPane的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从此帖子中了解不要在JComponent上设置尺寸,您没有在任何组件上设置首选尺寸,而是让布局管理器为您完成设置.

I understand from this post Don't Set sizes on a JComponent that you dont set preferred sizes on any component and instead let the layout manager do this for you.

我的问题是,我有一个JPanel,我将它放入JTabbedPane到这样的JFrame中

My question is, I have a JPanel that I put into a JTabbedPane into a JFrame like this

JFrame frame=new JFrame(); 
JTabbedPane pane=new JTabbedPane();
pane.addTab("Tab 1",new JScrollPane(getJPanel1()));
pane.addTab("Tab 2",new JScrollPane(getJPanel2()));
frame.setContentPane(pane);

现在,在这种情况下,JTabbedPane将采用您添加到其中的最大大小的组件的大小.因此,我的JScrollPane根本不显示.我需要设置JScrollPane的首选大小,如果不设置它,滚动条将不会出现并且内容会被剪切.

Now in this case the JTabbedPane will take the size of the maximum sized component you add to it. Because of this my JScrollPane does not show up at all. I need to set the preferred size of the JScrollPane, if I dont set it, the scroll bars will not appear and content is getting cut.

如何使用布局管理器解决此问题.我想专门这样做:

How do I use a layout manager to solve this. I want to specifically do this:

使JFrame/JTabbedPane/JPanelInTab扩展到屏幕的高度(进入窗口的任务栏),如果要剪切选项卡的内容,则会出现滚动条.框架的宽度应恰好适合JTabbedPane.

Make the JFrame/JTabbedPane/JPanelInTab extend upto the height of the screen (taking into the taskbar of windows), if the tab content is going to get cut the scrollbars should appear. The width of the frame should fit exactly as much as the JTabbedPane.

编辑

这是一个MVCE,显示了我正在尝试做的事情.我已经按照peeskillet的建议包括了更改,但没有效果

Here's an MVCE that shows what I am trying to do. I have included the changes as per the suggestion by peeskillet but there is no effect

import javax.swing.*;
import java.awt.*;

public class ScrollPaneTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame=new JFrame();
                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

                JTabbedPane tabbedPane=new JTabbedPane();

                tabbedPane.addTab("Tab 1", wrapWithBorderLayoutPanel(getPanel()));
                tabbedPane.addTab("Tab 2", wrapWithBorderLayoutPanel(getPanel()));
//
//                tabbedPane.addTab("Tab 1", new JScrollPane(getPanel()));
//                tabbedPane.addTab("Tab 2", new JScrollPane(getPanel()));

                frame.setContentPane(tabbedPane);
                frame.pack();
                frame.setVisible(true);
            }

            private JPanel wrapWithBorderLayoutPanel(JPanel panel) {
                JPanel borderLayoutPanel=new JPanel(new BorderLayout());
                borderLayoutPanel.add(new JScrollPane(panel), BorderLayout.CENTER);
                return borderLayoutPanel;
            }

            private JPanel getPanel() {
                JPanel panel=new JPanel();
                Box box = Box.createVerticalBox();
                for (int i = 1; i <= 100; i++) {
                    box.add(new JLabel("This is Label #" + i));
                }
                panel.add(box);
                return panel;
            }
        });
    }

}

一旦我这样做,下面就是我得到的输出.框架没有在任务栏上结束.它在它后面延伸.因此,最后一个标签隐藏在任务栏的后面.我希望框架在任务栏开始之前结束.

Once I do that the below is the output I get. The frame does not end at the taskbar. It stretches behind it. So the last label is hidden behind the taskbar. I would like the frame to end before the taskbar begins.

PS:如果我用BorderLayout面板包裹面板或将滚动窗格直接添加到选项卡式窗格,则没有任何变化.两者导致同一件事.您可以通过注释掉行

PS: There is no change if I wrap the panel with a BorderLayout panel or add the scroll pane directly to the tabbed pane. Both results in the same thing. You can test the same by commenting out lines

           //tabbedPane.addTab("Tab 1", wrapWithBorderLayoutPanel(getPanel()));
            //tabbedPane.addTab("Tab 2", wrapWithBorderLayoutPanel(getPanel()));

            tabbedPane.addTab("Tab 1", new JScrollPane(getPanel()));
            tabbedPane.addTab("Tab 2", new JScrollPane(getPanel()));

推荐答案

我已再次发布此问题,因为该问题被投票为关闭.在那儿,我已经标记了正确的答案.请查看下面的链接以获取答案

I had posted this question again since this one was voted to be closed. Over there I have marked the right answer. Please check the link below for the answer

使用屏幕边界减去插图

这篇关于在JFrame的JTabbedPane中调整JScrollPane的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆