如何制作一个小程序滚动?
我一直在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本代码应该是:
The basic code should be:
您正在向面板添加组件,因此您不应期望看到滚动窗格而不显示滚动窗格。然后您要做的就是将该面板添加到滚动窗格中,该滚动窗格将添加到您的主容器中。
从您的代码来看,我认为您的问题是
您应该这样做,
这是因为您仅将面板添加到不包含任何滚动窗格的框架中。由于您已将面板添加到滚动窗格,因此您应该将滚动窗格而不是面板添加到主容器。
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
your should be doing this
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.
听起来您正在 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.