JScrollPane问题

发布于 2024-11-07 12:49:13 字数 1012 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清眉祭 2024-11-14 12:49:13

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

JScrollPane scrollPane = new JScrollPane(panel1);

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

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

不要设置 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.

JScrollPane scrollPane = new JScrollPane(panel1);

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文