簡體   English   中英

Java ScrollPane和Canvas-將Canvas鎖定為JScrollPane大小

[英]Java ScrollPane and Canvas - Lock Canvas to JScrollPane size

我目前在JScrollPane和子組件方面陷入困境。 本質上,我需要嚴格控制JScrollPane中子組件的大小,以使其鎖定到JScrollPane的大小(這樣就不會出現滾動條)或保持固定到預定義的大小(並讓JScrollPane在適當的時候顯示滾動條) 。 該控件必須能夠動態切換(特別是通過另一個JFrame窗口中的切換框)。 JScrollPane定向鎖定到父JFrame窗口(將其完全填充並通過BorderLayout鎖定其調整大小)。

目前,由於其三重緩沖功能(createBufferStrategy(3);),我將Canvas對象用作JScrollPane的子組件。 我在很多地方都看到Canvas和JScrollPane融合得不是很好,因此可以解決以上問題並取消使用Canvas的答案將受到高度贊賞。

我的組件的布局如下:

JFrame(自訂類別)-> JScrollPane->畫布

不確定是否有幫助,但是Canvas渲染代碼如下:

//This is a method from a nested class inside the JFrame class.
public void run() {
    long MaxFrameTime;
    long Time;
            //This is the Canvas Object
    RXDisplayCanvas.createBufferStrategy(3);
    BufferStrategy BS = RXDisplayCanvas.getBufferStrategy();
    Graphics2D G2D;

    while(isVisible()){
        MaxFrameTime = Math.round(1000000000.0 / FPSLimit);
        Time = System.nanoTime();

        //Render Frame from a source from another thread via a AtomicReference<BufferedImage> named 'Ref'
        BufferedImage Frame = Ref.get();
        if(Frame != null){
            G2D = (Graphics2D)BS.getDrawGraphics();

            int X0 = 0;
            int Y0 = 0;
            int W = RXDisplayCanvas.getWidth();
            int H = RXDisplayCanvas.getHeight();
            double Width = Frame.getWidth();
            double Height = Frame.getHeight();
            double ImgW = Width;
            double ImgH = Height;

            if(ImgW > W){
                ImgW = W;
                ImgH = ImgW / (Width / Height);
            }

            if(ImgH > H){
                ImgH = H;
                ImgW = ImgH * (Width / Height);
            }

            int CenterX = (int)Math.round((W / 2.0) - (ImgW / 2.0)) + X0;
            int CenterY = (int)Math.round((H / 2.0) - (ImgH / 2.0)) + Y0;
            G2D.setBackground(Color.BLACK);
            G2D.clearRect(0, 0, W, H);
            G2D.drawImage(Frame, CenterX, CenterY, (int)Math.round(ImgW), (int)Math.round(ImgH), null);

            //Additional Drawing Stuff Here


            G2D.dispose();

            if(!BS.contentsLost()){
                BS.show();
            }
        }

        Time = System.nanoTime() - Time;
        if(Time < MaxFrameTime){
            try{
                Thread.sleep(Math.round((MaxFrameTime - Time)/1000000.0));
            }catch(InterruptedException N){}
        }
    }
}

我當前對問題的解決方案效果不佳(不會與父級JScrollPane進行“重新鎖定”;“ FixedDim”是先前設置的尺寸對象):

/**
 * Sets whether to lock the size of the canvas object to a predefined dimension.
 * @param b If true, the canvas becomes non-resizable and scrollbars will appear when appropriate. 
 * If false, the canvas will resize with the enclosing scrollpane.
 */
public void setLockResize(boolean b){
    CurrentlyLocked = b;
    if(b){
        RXDisplayCanvas.setMinimumSize(FixedDim);
        RXDisplayCanvas.setMaximumSize(FixedDim);
        RXDisplayCanvas.setPreferredSize(FixedDim);
    }else{
        RXDisplayCanvas.setMinimumSize(Min);
        RXDisplayCanvas.setMaximumSize(Max);
        RXDisplayCanvas.setPreferredSize(FixedDim);
    }
}

並取消使用畫布

默認情況下,Swing是雙緩沖的。 嘗試僅使用JPanel。

我需要嚴格控制JScrollPane中子組件的大小,以使其鎖定到JScrollPane的大小(這樣就不會出現滾動條)或保持固定到預定義的大小(並讓JScrollPane在適當的時候顯示滾動條)

您也許可以使用Scrollable Panel 您也許可以在ScrollableSizeHint的NONE和FIT之間切換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM