如何使用空布局将JScrollPane添加到JTabbedPane上? [英] How to add a JScrollPane onto a JTabbedPane using a null layout?
问题描述
我想在我的Tab上实现滚动条.但是没有任何显示,也没有例外.
I want to implement a Scrollbar onto my Tab. However nothing is showing and there are no exceptions.
我认为我需要一个:
scrollPane.setViewportView(scrollPanel);
scrollPane.setViewportView(scrollPanel);
但是效果不佳.
我想知道在将Jscrollpane添加到JTab时如何在不使用显式框架的情况下将其设置为可见.如果我使用框架并将其添加到框架上,则会创建一个新窗口. 但是,我认为框架是如何构建的,使我的程序看起来很复杂.
I am wondering when adding a Jscrollpane onto a JTab how do you set it visible without using an explicit frame. If I use a frame and add it on the frame it creates a new window. However how I got this program the Frame looks built I assume and this complicates everything.
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame {
private JTabbedPane tabbedPane;
private JPanel panel; // Page where I want JScrollPane intisialized
public Test()
{
setTitle( "Program" );
setSize( 400, 200 ); // I want the JScrollPane to extend to 400 vertically
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create the tab pages
createPage1();
tabbedPane = new JTabbedPane();
tabbedPane.addTab( "Welcome", panel );
topPanel.add( tabbedPane, BorderLayout.CENTER );
}
public void createPage1()
{
panel = new JPanel();
panel.setLayout( null ); // sets layout to null
////////////////////////
JPanel scrollPanel = new JPanel();
scrollPanel.setLayout(null);
scrollPanel.setPreferredSize(new Dimension(400,400));
///////////////////////
panel.add(scrollPanel);
scrollPanel.setVisible (true);
}
public static void main( String args[] )
{
// Create an instance of the test application
Test mainFrame = new Test();
mainFrame.setVisible( true );
}
}
如果您有任何疑问,请随时提出.
If you have any questions don't hesitate to ask.
推荐答案
What you want is to use a JScrollPane
. Change the createPage1()
method to something like this:
public void createPage1()
{
panel = new JPanel();
panel.setLayout( new BorderLayout() );
////////////////////////
JScrollPane scrollPanel = new JScrollPane();
scrollPanel.setViewportView(new JLabel("hellossssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"));
scrollPanel.setPreferredSize(new Dimension(400,400));
///////////////////////
panel.add(scrollPanel,BorderLayout.CENTER);
}
您将看到一个滚动条.请注意,此更改包含四件事:
And you will see a scrollbar. Note this change encompasses four things:
- 将
null
布局调用替换为BorderLayout
- 创建一个
JScrollPane
而不是JPanel
- 将一些东西添加到窗格中以进行演示
- 删除不必要的
setVisible(true)
呼叫.
- replace the
null
layout call with aBorderLayout
- make a
JScrollPane
instead of aJPanel
- add something to the pane for demo purposes
- remove the unnecessary
setVisible(true)
call.
这篇关于如何使用空布局将JScrollPane添加到JTabbedPane上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!