JScrollPane问题
我在 jtabbedpane 中添加了一个 jscrollpane,在 jscrollbar 窗格中我有一个 jpanel。在 jpanel 中,我有几个在运行时创建的按钮。我的想法是,当我在运行时动态添加的按钮大小增长时,它会得到一个滚动条。我无法让这种事发生。按钮尺寸增大,并且当尺寸超出 jpanel 视图时隐藏。我使用 setSize() 增加 jpanel 及其内部按钮的大小。我还使用过 jScrollPane2.setViewportView(Jpanel1);设置视口视图。
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 面板的高度,我也看不到正在运行的滚动条。
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);
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 JScrollPane 时需要注意一些事项。初始化 JScrollPanel 的最简单方法是在构造函数中传递要使其可滚动的面板。
然后,要设置大小,您必须使用 setPreferredSize,而不是 setSize()。
不要设置 JScrollPane 构造函数内给定的面板大小。 (即面板1)
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.
Then, to set the sizes, you have to use setPreferredSize, not setSize().
Do not set the size on the panel given inside the constructor of JScrollPane. (ie panel1)