Java:无法将Gridlayout应用于Jscrollpane。获取java.lang.ClassCastException [英] Java: Can't apply Gridlayout to a Jscrollpane. Get Get java.lang.ClassCastException

查看:1014
本文介绍了Java:无法将Gridlayout应用于Jscrollpane。获取java.lang.ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Gridlayout将4个元素放在一行中。首先,我有一个JPanel,一切正常。对于行数变大而我必须能够向下滚动的情况,我改变了一点。现在我的 JPanel 上添加了一个 JScrollPane 。我使用了相同的代码,现在我只是将元素添加到 Jscrollpane 的视口中,但现在我得到了这个异常获取java.lang.ClassCastException :JScrollPane的布局必须是ScrollPaneLayout:在javax.swing.JScrollPane.setLayout(未知来源),我不确切知道为什么。为什么不应该为 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 );

这篇关于Java:无法将Gridlayout应用于Jscrollpane。获取java.lang.ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆