Java 如何确定在JScrollpane中看到的是哪个JPanel?

Java 如何确定在JScrollpane中看到的是哪个JPanel?,java,swing,jscrollpane,Java,Swing,Jscrollpane,我有一个JScrollpane,其中包含一个大JPanel,JPanel本身包含3个JPanel。这3个JPanel中的每一个都具有与JScrollpane相同的大小。用户无法滚动。他可以点击一个按钮,看到下一个或上一个JPanel,一刻只能看到一个面板,他看不到一个面板的一部分和另一个面板的一部分 我怎样才能知道现在看到的是哪个面板?有一种方法是显示,它确定该组件是否显示在屏幕上。这意味着组件必须是可见的,并且必须位于可见并显示的容器中。更多 例如: JPanel p= new JPanel(

我有一个JScrollpane,其中包含一个大JPanel,JPanel本身包含3个JPanel。这3个JPanel中的每一个都具有与JScrollpane相同的大小。用户无法滚动。他可以点击一个按钮,看到下一个或上一个JPanel,一刻只能看到一个面板,他看不到一个面板的一部分和另一个面板的一部分

我怎样才能知道现在看到的是哪个面板?

有一种方法是显示,它确定该组件是否显示在屏幕上。这意味着组件必须是可见的,并且必须位于可见并显示的容器中。更多

例如:

JPanel p= new JPanel();

if(p.isShowing()) {

}
看一看讨论,它会在某种程度上帮助你。你甚至可以用一个声音来听哪些是可见的。了解更多信息。

有一种方法是显示,用于确定此组件是否显示在屏幕上。这意味着组件必须是可见的,并且必须位于可见并显示的容器中。更多

例如:

JPanel p= new JPanel();

if(p.isShowing()) {

}

看一看讨论,它会在某种程度上帮助你。你甚至可以用一个声音来听哪些是可见的。了解更多信息。

您可以从JScrollPane请求当前显示的组件

scrollPaneObject.getViewPort().getView();

您可以从JScrollPane请求当前显示的组件

scrollPaneObject.getViewPort().getView();
用户无法滚动。他可以点击一个按钮,看到下一个或上一个JPanel,一刻只能看到一个面板,他看不到一个面板的一部分和另一个面板的一部分

您应该使用带有CardLayout的JPanel,而不是JScrollPane

scrollPaneObject.getViewPort().getView();

用户无法滚动。他可以点击一个按钮,看到下一个或上一个JPanel,一刻只能看到一个面板,他看不到一个面板的一部分和另一个面板的一部分

您应该使用带有CardLayout的JPanel,而不是JScrollPane

scrollPaneObject.getViewPort().getView();

请参见

假设设置如我对您的问题的评论中所述,基本方法是将包含3个面板visibleRect的父组件与其子组件的边界进行比较。比如:

final JComponent parent = new JPanel(); // new PageScrollable();
parent.setLayout(new BoxLayout(parent, BoxLayout.PAGE_AXIS));
Color[] color = new Color[] {Color.YELLOW, Color.RED, Color.GREEN}; 
for (int i = 0; i < color.length; i++) {
    JTable table = new JTable(10, 5);
    // color it to see some difference
    table.setBackground(color[i]);
    // set a name for logging
    table.setName("table at: " + i);
    parent.add(table);
}

JScrollPane scrollPane = new JScrollPane(parent);
Action visible = new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent e) {
        Rectangle rect = parent.getVisibleRect();
        for (int i = 0; i < parent.getComponentCount(); i++) {
            // implement logic as needed to compare the parent's visible rect
            // with the children's bounds
            if (rect.intersects(parent.getComponent(i).getBounds())) {
                System.out.println("found: " + parent.getComponent(i).getName());
            }
        }
    }

};
frame.add(scrollPane); 
frame.add(new JButton(visible), BorderLayout.SOUTH);
/**
 * Implement a panel of type Scrollable to fine-tune its scrolling behaviour.
 * This implements the prefScrollableSize to the prefSize of the first child
 * and both block/unit increment to the height of the prefScrollable.
 */
public static class PageScrollable extends JPanel implements Scrollable {

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        if (getComponentCount() > 0) {
            return getComponent(0).getPreferredSize();
        }
        return super.getPreferredSize();
    }

    @Override
    public int getScrollableUnitIncrement(Rectangle visibleRect,
            int orientation, int direction) {
        return getPreferredScrollableViewportSize().height;
    }

    @Override
    public int getScrollableBlockIncrement(Rectangle visibleRect,
            int orientation, int direction) {
        return getPreferredScrollableViewportSize().height;
    }

    @Override
    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    @Override
    public boolean getScrollableTracksViewportHeight() {
        return false;
    }

}

假设设置如我对您的问题的评论中所述,基本方法是将包含3个面板visibleRect的父级组件与其子级边界进行比较。比如:

final JComponent parent = new JPanel(); // new PageScrollable();
parent.setLayout(new BoxLayout(parent, BoxLayout.PAGE_AXIS));
Color[] color = new Color[] {Color.YELLOW, Color.RED, Color.GREEN}; 
for (int i = 0; i < color.length; i++) {
    JTable table = new JTable(10, 5);
    // color it to see some difference
    table.setBackground(color[i]);
    // set a name for logging
    table.setName("table at: " + i);
    parent.add(table);
}

JScrollPane scrollPane = new JScrollPane(parent);
Action visible = new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent e) {
        Rectangle rect = parent.getVisibleRect();
        for (int i = 0; i < parent.getComponentCount(); i++) {
            // implement logic as needed to compare the parent's visible rect
            // with the children's bounds
            if (rect.intersects(parent.getComponent(i).getBounds())) {
                System.out.println("found: " + parent.getComponent(i).getName());
            }
        }
    }

};
frame.add(scrollPane); 
frame.add(new JButton(visible), BorderLayout.SOUTH);
/**
 * Implement a panel of type Scrollable to fine-tune its scrolling behaviour.
 * This implements the prefScrollableSize to the prefSize of the first child
 * and both block/unit increment to the height of the prefScrollable.
 */
public static class PageScrollable extends JPanel implements Scrollable {

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        if (getComponentCount() > 0) {
            return getComponent(0).getPreferredSize();
        }
        return super.getPreferredSize();
    }

    @Override
    public int getScrollableUnitIncrement(Rectangle visibleRect,
            int orientation, int direction) {
        return getPreferredScrollableViewportSize().height;
    }

    @Override
    public int getScrollableBlockIncrement(Rectangle visibleRect,
            int orientation, int direction) {
        return getPreferredScrollableViewportSize().height;
    }

    @Override
    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    @Override
    public boolean getScrollableTracksViewportHeight() {
        return false;
    }

}

jScrollpane包含3个JPanel,即jScrollpane和JPanel,请注意大写字母J。除非您指的是名称不正确的第三方类,否则请使用正确的大小写。.jScrollpane.getViewPortView?@Andrew Thompson:是的,您是对的,thanx:jScrollpane.getViewPortView.getName;总是返回null…为什么?我仍然不知道你的设置是什么:一个JScrollPane可以有一个viewportView-所以你的三个面板被添加到另一个面板,即单个viewportView?它的单位增量对于ChildPanel的所有3个高度都是相同的?jScrollpane包含3个JPanel,即jScrollpane和JPanel。注意大写字母J。除非你指的是一个命名不好的第三方类,否则请使用正确的大小写。..jScrollpane.getViewPortView?@Andrew Thompson:是的,你是对的,thanx:JScrollPane.getViewPortView.getName;总是返回null…为什么?我仍然不知道你的设置是什么:一个JScrollPane可以有一个viewportView-所以你的三个面板被添加到另一个面板,即单个viewportView?它的单位增量对于所有3个子面板的高度都是相同的?+1,请将链接Java1.4更改为Java7,并且在我的情况下这不起作用。这个方法对我来说总是返回true,因为面板都是可见的,但是只有一个真正可以看到,但是当它们是可见的并且被添加到JScrollpane中时,它自身也是可见的,它返回true…-1这是完全错误的,至少我理解OP的设置;-。。。请阅读isShowing的api文档以理解为什么…+1,请将链接Java1.4更改为Java7,并补充这在我的情况下不起作用。这个方法对我来说总是返回true,因为面板都是可见的,但是只有一个真正可以看到,但是当它们是可见的并且被添加到JScrollpane中时,它自身也是可见的,它返回true…-1这是完全错误的,至少我理解OP的设置;-。。。请阅读isShowing的api文档以了解原因…我真的不明白它返回了什么!scrollPaneObject.getViewPort.getView.getName;总是返回空值…为什么?我真的不明白它返回了什么!scrollPaneObject.getViewPort.getView.getName;总是返回null…为什么?事实上我使用JScrollpane而不是JPanel有一些原因…事实上我使用JScrollpane而不是JPanel有一些原因…+1为我指明了正确的方向,回答是:谢谢+1为我指明了正确的方向,回答是:谢谢