jScrollPane无法添加组件 [英] jScrollPane can't add component

查看:115
本文介绍了jScrollPane无法添加组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 jScrollPane 和表单上的按钮。该按钮将组件添加到 jScrollPane 。我正在使用带有中心对齐的 FlowLayout 来排列 jScrollPane 中的组件。



第一个组件出现没有问题,并且完美对齐。当我再次按下按钮时,似乎没有任何事情发生。当我按照调试器进行操作时,它会显示所有内容都与以前完全一样。



单击按钮时执行的代码:

  jScrollPane.getViewport()。add(new Component()); 

这是我设置 FlowLayout 的方法 jScrollPane的视口

  jScrollPane.getViewport()。setLayout(new FlowLayout(FlowLayout.CENTER)); 


解决方案

你正在混合重量级(AWT)组件轻量级(Swing)组件,这是不可取的,因为它们不能很好地一起玩。



JScrollPane 包含一个 JViewPort ,您可以在其中添加子组件,AKA视图。





(图片来自所以调用 jScrollPane.getViewport()。setLayout(new FlowLayout(FlowLayout.CENTER)); 实际设置 JViewPort 的布局管理器,这是不可取的。



你应该做的是创建你想要添加到滚动窗格的组件,设置它的布局和将所有它的子组件添加到它,然后将其添加到scr oll窗格。如果需要,您可以在以后阶段向视图添加组件,但这取决于您...

  //将view声明为类变量... 
view = new JPanel(); // FlowLayout是默认的布局管理器
//将您现在需要的组件添加到view
JScrollPane scrollPane = new JScrollPane(view);

现在您可以根据需要向视图添加新组件...

  view.add(...); 

如果您不想维持对的引用视图,您可以通过调用 JViewport#getView 来访问它,它将返回由视口管理的组件。

  JPanel view =(JPanel)scrollPane.getViewPort()。getView(); 

工作示例



这对我来说很好......



nb - 我将 view.validate()添加到我的添加新组件后,您可能没有的代码......

  public class TestScrollPane01 {

public static void main(String [] args){
new TestScrollPane01();
}

public TestScrollPane01(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
尝试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception ex){
}

JFrame frame = new JFrame(Testing );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new MainPane());
frame。 pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

公共类MainPane扩展JPanel {

private JScrollPane scrollPane;
private int count;

public MainPane(){
setLayout(new BorderLayout());
scrollPane = new JScrollPane(new JPanel());
((JPanel)scrollPane.getViewport()。getView())。add(new JLabel(First));
add(scrollPane);

JButton add = new JButton(Add);
add.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
JPanel view =((JPanel)scrollPane.getViewport()。getView( ));
view.add(new JLabel(Added+(++ count)));
view.validate();
}
});

add(add,BorderLayout.SOUTH);
}

@Override
public Dimension getPreferredSize(){
return new Dimension(200,200);
}

}

}


I have a jScrollPane and a button on a form. The button adds a component to the jScrollPane. I'm using a FlowLayout with a center alignment to arrange the components within the jScrollPane.

The first component has no problems appearing and is aligned perfectly. When I then hit the button again, nothing seems too happen. When I follow the debugger it shows that everything happens precisely as before.

The code that's being executed when the button is clicked:

jScrollPane.getViewport().add(new Component());

This is how I setup the FlowLayout on the Viewport of the jScrollPane:

jScrollPane.getViewport().setLayout(new FlowLayout(FlowLayout.CENTER));

解决方案

You're mixing heavy weight (AWT) components with light weight (Swing) components, this is inadvisable as they don't tend to play well together.

JScrollPane contains a JViewPort onto which you can add a child component, AKA the view.

(image from the JavaDocs)

So the call jScrollPane.getViewport().setLayout(new FlowLayout(FlowLayout.CENTER)); is actually setting the JViewPort's layout manager, which really isn't advisable.

What you should do is create the component you want to add to the scrollpane, set it's layout and add all it's child components to it and then add it to the scroll pane. You can add components to the "view" at later stage if you want, but that's up to you...

// Declare "view" as a class variable...
view = new JPanel(); // FlowLayout is the default layout manager
// Add the components you need now to the "view"
JScrollPane scrollPane = new JScrollPane(view);

Now you can add new components to the view as you need...

view.add(...);

If you don't want to maintain a reference to view, you can access it by calling JViewport#getView which will return the component been managed by the view port.

JPanel view = (JPanel)scrollPane.getViewPort().getView();

Working Example

This works fine for me...

nb - I added view.validate() to my code, which you may not have had, after I added a new component...

public class TestScrollPane01 {

    public static void main(String[] args) {
        new TestScrollPane01();
    }

    public TestScrollPane01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MainPane extends JPanel {

        private JScrollPane scrollPane;
        private int count;

        public MainPane() {
            setLayout(new BorderLayout());
            scrollPane = new JScrollPane(new JPanel());
            ((JPanel)scrollPane.getViewport().getView()).add(new JLabel("First"));
            add(scrollPane);

            JButton add = new JButton("Add");
            add.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JPanel view = ((JPanel)scrollPane.getViewport().getView());
                    view.add(new JLabel("Added " + (++count)));
                    view.validate();
                }
            });

            add(add, BorderLayout.SOUTH);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

这篇关于jScrollPane无法添加组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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