3

これは非常に単純なことかもしれませんが、私は完全に混乱しています。

ペインをスクロールすると、上のものJScrollPaneがまったく再描画されません。JLayeredPaneJLayeredPane

ここでは、青い四角がまったく再描画されないことを示す小さな例を示します。

レイヤード ペインの仕組みを完全に誤解していませんか?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class LayeredTest extends JPanel{

    public LayeredTest(){

        JPanel content = new JPanel();
        content.setBackground(Color.red);
        content.setPreferredSize(new Dimension(2048, 2048));
        content.setBounds(0, 0, 2048, 2048);
        JPanel control = new JPanel();
        control.setBackground(Color.blue);
        control.setPreferredSize(new Dimension(200, 50));
        control.setBounds(0, 0, 100, 50);

        JScrollPane scroll = new JScrollPane(content);
        scroll.setBounds(0, 0, 400, 400);

        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(400, 400));

        layeredPane.add(control, 0);
        layeredPane.add(scroll, 1);

        this.add(layeredPane, BorderLayout.CENTER);
    }


    public static void main(String[] args) {
        //Create and set up the window.
        JFrame frame = new JFrame("Test - Very lulz");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100, 100);
        frame.setSize(400, 400);

        //Create and set up the content pane.
        frame.setContentPane(new LayeredTest());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
}

何か案は?

4

1 に答える 1

2

あなたがやっていることは、コンポーネントcontrolscroll「デフォルト」レイヤーに追加することです。したがって、実際にはそれらはまだ同じレイヤーにあります。それらを別のレイヤーに配置するには、レイヤー番号も指定する必要があります。コンポーネントは、デフォルト レイヤー (インデックス 0 の一番下) と次のレイヤー (インデックス 100 の「パレット」レイヤー) の間のどこかに配置するのが最適です。

たとえば、レイヤー 50 と 51 にコンポーネントを配置するには、コンポーネントを追加する場所を次のように変更しますlayeredPane

    layeredPane.add(scroll, 50, 0);
    layeredPane.add(control, 51, 0);

これはscrollレイヤー 50 のcontrol位置 0 とレイヤー 51 の位置 0 に配置されます。

于 2011-08-28T23:38:14.063 に答える