Java JscrollPane不';当内容变大时,不要启用滚动条
我有一个JScrollPaneJava JscrollPane不';当内容变大时,不要启用滚动条,java,swing,jbutton,jscrollpane,Java,Swing,Jbutton,Jscrollpane,我有一个JScrollPaneprocessescrollpane,它有一个JPanelprocessButtonPanel作为组件。现在在这个组件中,我添加了jbutton,它来自于我在编程运行时填充的ArrayList 我已经设置了JScrollPane的一些设置,但是我遇到的问题是,当JPanel中有更多的按钮时,我可以显示,JScrollPane没有启用它的滚动条 下面是一些代码片段: processesScrollPane = new JScrollPane(); pr
processescrollpane
,它有一个JPanelprocessButtonPanel
作为组件。现在在这个组件中,我添加了jbutton,它来自于我在编程运行时填充的ArrayList
我已经设置了JScrollPane的一些设置,但是我遇到的问题是,当JPanel中有更多的按钮时,我可以显示,JScrollPane没有启用它的滚动条
下面是一些代码片段:
processesScrollPane = new JScrollPane();
processesScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
processesScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
processesScrollPane.setBounds(12, 42, 221, 380);
processPanel.add(processesScrollPane); //processPanel is the JPanel which holds the JScrollPane
processButtonPanel = new JPanel(); // here are the buttons added whenever they are made and placed in the buttonList
processesScrollPane.setViewportView(processButtonPanel);
processButtonPanel.setLayout(null);
processesScrollPane.setViewportView(processButtonPanel);
buttonList = new ArrayList<JButton>();
(是的,我使用的是无布局,但那是因为我不打算缩放整个窗口等)
有人能在正确的时间帮助我启用滚动条吗?1)
processescrollpane.setBounds(12,42,221,380)代码>这可能是问题的一部分。Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或者与布局填充和边框一起使用空白。2) 为了更快地获得更好的帮助,请发布(最小完整可验证示例)或(简短、独立、正确的示例)。当所包含面板的首选大小足够大时,将显示滚动条。确定首选大小通常是布局管理器的工作——只需使用一个,因为这是swing的设计使用方式null
layouts会导致无穷无尽的问题。欢迎来到不推荐使用null layouts的另一个原因。布局管理器是Swing的核心
JButton b = new JButton("Process " + i);
b.setBounds(12, 5 + (30*i), 174, 25);
processButtonPanel.add(b);
processButtonPanel.repaint();
buttonList.add(b);
processesScrollPane.revalidate();
processesScrollPane.repaint();