JScrollPane问题 [英] JScrollPane problem

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

问题描述

我将jscrollpane添加到jtabbedpane中,并且在jscrollbar窗格中,我具有jpanel.在jpanel中,我只有几个在运行时创建的按钮.我的想法是,当我在运行时动态添加的按钮大小增加时,它会得到一个滚动条.我无法做到这一点.当按钮的大小超出jpanel的视图时,按钮的大小将增加并隐藏.我正在使用setSize()增加jpanel的大小和其中的按钮.我还使用了jScrollPane2.setViewportView(Jpanel1);设置视口视图.

i have a jscrollpane added to jtabbedpane and inside the jscrollbar pane i have a jpanel. In the jpanel i have few buttons which i am creating on runtime. My idea is it get a scroll bar when the button size that i am adding dynamically during runtime grows. I cant get that to happen. The button size is increased and it hides when the size goes beyond the jpanel's view. I am increasing the size of jpanel and the buttons inside it using setSize(). I have also used jScrollPane2.setViewportView(Jpanel1); to set the viewportview.


Dimension t_size = table_button.getSize(); 
table_button.setSize((int)t_size.getWidth(), (int) (t_size.getHeight() + 150));
       Rectangle bounds = global_variables.bottom_panel.getBounds();
       int y_new = (int) (bounds.getY() + 150);
       int x_new = (int) bounds.getX();
       bounds.setLocation(x_new, y_new);
       global_variables.bottom_panel.setBounds(bounds);
floor_plan_admin_single.table_base.setSize((int)floor_plan_admin_single.table_base.getWidth(), (int)floor_plan_admin_single.table_base.getHeight()+150);

在这里,table_button是我要动态添加的按钮,global_variables.bottom_panel是位于table_button下方的面板,随着我增加table_button的高度,我将bottom_panel向下移动. floor_plan_admin_single.table_base是添加到滚动条的jpanel.即使我更改tat table_base面板的高度,我也看不到滚动条的作用.

Here table_button is the button i am adding dynamically and global_variables.bottom_panel is the panel which stays below table_button and as i increase the height of table_button i am moving the bottom_panel down. floor_plan_admin_single.table_base is the jpanel added to scrollbar. even if i change the height of tat table_base panel i cant see the scrollbar in action.

推荐答案

使用JScrollPane时需要注意一些事项.初始化JScrollPanel的最简单方法是在构造函数中传递要使其可滚动的面板.

There are a few things to note when using a JScrollPane. The easiest way to initialize a JScrollPanel is by passing the panel you want to make scrollable in the constructor.

JScrollPane scrollPane = new JScrollPane(panel1);

然后,要设置大小,必须使用setPreferredSize,而不是setSize().

Then, to set the sizes, you have to use setPreferredSize, not setSize().

button.setPreferredSize(new Dimension(120, 120));

不要在JScrollPane的构造函数中给定的面板上设置大小. (即panel1)

Do not set the size on the panel given inside the constructor of JScrollPane. (ie panel1)

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

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