1

JScrollpanebig を含む a がありJPanelJPanelそれ自体には 3 JPanels が含まれます。これらの 3 つのそれぞれJPanelsは、 と同じサイズJScrollpaneです。ユーザーはスクロールできません。彼はボタンをクリックして、次または前を見ることができますJPanel(一度に見ることができるのは 1 つのパネルだけであり、あるパネルの一部と他のパネルの一部を見ることはできません...)。

現在どのパネルが表示されているかを知るにはどうすればよいですか?

4

4 に答える 4

2

isShowing()このコンポーネントが画面に表示されているかどうかを判断するメソッドがあります。これは、コンポーネントが表示されている必要があり、表示されているコンテナー内にある必要があることを意味します。詳細はこちら

例:

JPanel p= new JPanel();

if(p.isShowing()) {

}

このディスカッションを見てください。ある意味で役立ちます。ComponentListenerを使用して、どれが表示されているかをリッスンすることもできます。詳しくはこちらをご覧ください。

于 2013-01-11T09:39:46.097 に答える
1

あなたの質問に対する私のコメントで概説されているセットアップを想定すると、基本的なアプローチは、親(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);

余談ですが、スクロール動作を微調整するために、次のようなScrollableを実装するカスタムパネルを検討することができます。

/**
 * 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;
    }

}
于 2013-01-11T13:46:02.047 に答える
1

現在表示されているコンポーネントを JScrollPane からリクエストできます。

scrollPaneObject.getViewPort().getView();
于 2013-01-11T10:45:09.477 に答える
0

ユーザーはスクロールできません。彼はボタンをクリックして次または前のJPanelを見ることができます(一度に1つのパネルしか見ることができず、1つのパネルの一部と他のパネルの一部を見ることができません)

JScrollPaneではなく、CardLayoutでJPanelを使用する必要があります。

http://docs.oracle.com/javase/tutorial/uiswing/layout/card.htmlを参照してください

于 2013-01-11T11:26:09.467 に答える