如何制作一个小程序滚动?

发布于 2024-11-25 00:04:26 字数 806 浏览 1 评论 0原文

我一直在尝试将 JScrollPane 与我的小程序一起使用,但它不起作用。我有一个 JPanel,向其中添加了 20 个按钮,并且我希望能够向上和向下滚动这个 JPanel。相反,滚动条不会出现。当我使用 setPreferredSize 时,即使只有大约 3 个按钮被显示并且其余按钮被切断,它们仍然没有出现。如果我不使用 setPreferredSize,则可能没有任何滚动条,因为我必须使窗口足够大才能看到所有按钮。如果我尝试使滚动条始终可见,它们会出现但不执行任何操作。我用 JFrame 而不是 Applet 尝试了完全相同的代码,它工作正常,但我需要它是一个小程序。 JScrollPane 与小程序不兼容吗? (注意:我尝试使用外部 JPanel 并向其中添加可滚动面板,但它没有任何改变)。更改布局也不能解决问题。我附上了代码的简化版本,但它显示了相同的错误。

这是我的代码:

JPanel scrollPanel = new JPanel();
scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.PAGE_AXIS));
JScrollPane scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
for (int i = 0; i < 20; i++) scrollPanel.add(new JButton("Button " + i));
add(scrollPanel);
validate();

I have been trying to use JScrollPane with my applet, but it doesn't work. I have a JPanel to which I add 20 buttons, and I want to be able to scroll up and down this JPanel. Instead, the scrollbars do not appear. When I use setPreferredSize they still did not appear even though only about 3 of the buttons are being displayed and the rest are cut off. If I do not use setPreferredSize, there might as well not be any scrollbars because I have to make the window big enough to see all of the buttons. If I try to make the scrollbars always visible, they appear but do nothing. I tried the exact same code with JFrame instead of Applet, and it works fine, but I need it to be an applet. Is JScrollPane incompatible with applets? (Note: I tried to use an outer JPanel and add the scrollable panel to it, but it changed nothing). Changing the layouts also doesn't fix the problem. I have attached a simplified version of my code, but it displays the same errors.

Here is the code I have:

JPanel scrollPanel = new JPanel();
scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.PAGE_AXIS));
JScrollPane scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
for (int i = 0; i < 20; i++) scrollPanel.add(new JButton("Button " + i));
add(scrollPanel);
validate();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

沙沙粒小 2024-12-02 00:04:26
  1. 您永远不会将所有面板添加到滚动窗格中
  2. 您永远不会将滚动窗格添加到小程序中

基本代码应该是:

JScrollPane scrollPane = new JScrollPane(...);
scrollPane.setViewportView( scrollPanel );
add( scrollPane );
  1. You never all the panel to the scroll pane
  2. You never add the scroll pane to the applet

The basic code should be:

JScrollPane scrollPane = new JScrollPane(...);
scrollPane.setViewportView( scrollPanel );
add( scrollPane );
我的影子我的梦 2024-12-02 00:04:26

您正在向面板添加组件,因此您不应期望看到滚动窗格而不显示滚动窗格。然后您要做的就是将该面板添加到滚动窗格中,该滚动窗格将添加到您的主容器中。

从您的代码来看,我认为您的问题是

add(scrollPanel);

您应该这样做,

add(scroll);`

这是因为您仅将面板添加到不包含任何滚动窗格的框架中。由于您已将面板添加到滚动窗格,因此您应该将滚动窗格而不是面板添加到主容器。

You are adding components to a Panel so you shouldn't expect to see a scroll pane wihout showing the scrollpane. What you want to do is then add that panel to a scrollpane which would be added to ur main container.

From your code, i think your problem is

add(scrollPanel);

your should be doing this

add(scroll);`

This is because you only added the panel to the frame which does not contain any scrollpane. Since you have added the panel unto the scrollpane, you should add the scrollpane and not the panel to the main container.

裂开嘴轻声笑有多痛 2024-12-02 00:04:26

听起来您正在 AWT 容器(Applet)中使用 Swing 组件(JScrollPane、JPanel...)。尝试使用 JApplet 代替。

It sounds like you are using Swing components (JScrollPane, JPanel, ...) in an AWT container (Applet). Try using JApplet instead.

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