Java:无法将 Gridlayout 应用于 Jscrollpane.获取获取 java.lang.ClassCastException
我使用 Gridlayout 将 4 个元素放在一行中.首先,我有一个 JPanel,一切正常.对于行数变大并且我必须能够向下滚动的情况,我对其进行了一些更改.现在我的 JPanel
添加了一个 JScrollPane
.我使用相同的代码,现在我只是将元素添加到 Jscrollpane
的视口,但现在我得到了这个异常 Get java.lang.ClassCastException: layout of JScrollPane must be a ScrollPaneLayout:在 javax.swing.JScrollPane.setLayout(Unknown Source)
我不知道具体原因.为什么 Jscrollpane
的 Gridlayout 不应该是未知的?
I use a Gridlayout to place 4 elements in one Line. First I had a JPanel and everything worked fine. For the case that the number of lines get to big and I have to be able to scroll down, I changed it a bit. Now I have my JPanel
with one JScrollPane
added on it. I used the same code, now I just add the elements to the viewport of the Jscrollpane
, but now I get this exception Get java.lang.ClassCastException: layout of JScrollPane must be a ScrollPaneLayout: at javax.swing.JScrollPane.setLayout(Unknown Source)
and I dont know exactly why. Why shouldnt be Gridlayout's be unknown for Jscrollpane
?
代码如下:
public objectDetails() {
setTitle("LLI_MC_Solver");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setLayout(new GridLayout());
setBounds(100, 100, 510, 401);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setVisible(true);
contentPane.setPreferredSize(new Dimension(500, 390));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0), 2));
scrollPane.setBounds(10, 10, 474, 342);
scrollPane.setLayout(new GridLayout(0,4)); //Line which causes the error
scrollPane.setPreferredSize(new Dimension(465, 330));
contentPane.add(scrollPane);
JPanel view = (JPanel)scrollPane.getViewport().getView();
for(Values v : colDetails)
{
JLabel lblC = new JLabel();
lblC.setText(k);
view.add(lblC);
view.validate();
JLabel lblN = new JLabel();
lblN.setText(v.getName());
view.add(lblN);
view.validate();
JLabel lblT = new JLabel();
lblT.setText(v.getType());
view.add(lblT);
view.validate();
JTextField valueBox = new JTextField();
valueBox.setText(v.getValue());
view.add(valueBox);
view.validate();
}
}
我根据编译器标记了导致问题的行.我不明白为什么,使用 JPanel
相同的代码可以正常工作.我发布的添加元素的for循环是为了完成目的,问题必须在 setLayout()
-Method.
I marked the line which causes the Problem according to the compiler. I dont understand why, with the JPanel
the same code worked fine. The for-loop where the elements are added I posted for completion purposes, the issue must be somewhere in the setLayout()
-Method.
提前致谢,感谢每一个帮助.
Thanks in advance, appreciate every help.
推荐答案
scrollPane.setLayout(new GridLayout(0,4));//导致错误的行
scrollPane.setLayout(new GridLayout(0,4)); //Line which causes the error
您不能更改滚动窗格的布局管理器.
You can't change the layout manager of a scrollpane.
JScrollPane 有自己的自定义布局管理器,因为它需要管理水平/垂直滚动条以及行/列标题等.
A JScrollPane has its own custom layout manager because it needs to manage the horizontal/vertical scrollbars as well as the row/column headers etc..
改为添加一个使用 GridLayout 的面板:
Instead you add a panel that uses a GridLayout:
JPanel panel = new JPanel( new GridLayout(0, 4) );
panel.add( component1 );
panel.add( component2 );
panel.add( component3 );
panel.add( component4 );
JScrollPane = new JScrollPane( panel );
相关文章