亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JScrollPane 內部的內部 JScrollPane 無法正常工作

JScrollPane 內部的內部 JScrollPane 無法正常工作

喵喵時光機 2023-06-28 15:26:50
我正在嘗試創建一個帶有滾動條的容器,并且容器內部有兩個內部面板。頂部內面板內還有另一個 JScrollPane。但目前我面臨的問題是,當我的頂部內面板太長(寬度)時,頂部內面板內的滾動條被禁用,我只能滾動容器的滾動條。import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.FlowLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;public class TestFrame {    public static void main(String... args) {        JFrame frame = new JFrame();        JPanel panel = new JPanel();        for (int i = 0; i < 10; i++) {            panel.add(new JButton("Hello-" + i));        }        JScrollPane scrollPane = new JScrollPane(panel);        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);        JPanel contentPane = new JPanel(new BorderLayout());        JPanel contentPaneSub = new JPanel();        contentPaneSub.add(scrollPane);        contentPane.add(contentPaneSub, BorderLayout.NORTH);        JPanel centerPanel = new JPanel(new FlowLayout());        centerPanel.add(new JButton("Example"));        contentPane.add(centerPanel, BorderLayout.CENTER);        JScrollPane scrollPane1 = new JScrollPane(contentPane);        scrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);        scrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);        frame.setContentPane(scrollPane1);        //for demo purpose we set this using hard coded way        //in real life project the java will auto adjust it size based on windows resolution        frame.setSize(new Dimension(500, 160));        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);        frame.setVisible(true);    }}我希望得到的是,如果頂部內面板的寬度太長,那么頂部內面板內的滾動條將可見并允許滾動。不是容器中的滾動條。
查看完整描述

1 回答

?
www說

TA貢獻1775條經驗 獲得超8個贊

我在嘗試解決這個問題時遇到了兩個不同的問題。第一個是將JScrollPane包含在窗口內,第二個是JScrollPane動態調整 的大小。


我能夠解決第一個問題,但無法使用自定義類完全解決第二個問題。隨著窗口的增加動態JScrollPane增加其寬度,但不會隨著窗口大小動態縮小。這是因為當窗口尺寸減小時,outerJScrollPane會鎖定內部內容的寬度,包括inner JScrollPane。


我無法找到一種方法讓內部窗格動態收縮,而無需有效刪除外部窗格的功能,這是行不通的,因為您的問題是專門針對另一個功能內JScrollPane的JScrollPane。


public class TestFrame {


    public static void main(String... args) {

        JFrame frame = new JFrame();

        JPanel panel = new JPanel();

        for (int i = 0; i < 10; i++) {

            panel.add(new JButton("Hello-" + i));

        }


        MyCustomPane scrollPane = new MyCustomPane(panel); //changed this line


        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

        JPanel contentPane = new JPanel(new BorderLayout());


        JPanel contentPaneSub = new JPanel();

        contentPaneSub.add(scrollPane);


        scrollPane.setOuterContainer(contentPaneSub); //added this line


        contentPane.add(contentPaneSub, BorderLayout.NORTH);


        JPanel centerPanel = new JPanel(new FlowLayout());

        centerPanel.add(new JButton("Example"));

        contentPane.add(centerPanel, BorderLayout.CENTER);


        JScrollPane scrollPane1 = new JScrollPane(contentPane);

        scrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        scrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);


        frame.setContentPane(scrollPane1);

        //for demo purpose we set this using hard coded way

        //in real life project the java will auto adjust it size based on windows resolution

        frame.setSize(new Dimension(500, 160));

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        frame.setVisible(true);

    }

}

MyCustomPane 類的代碼:


public class MyCustomPane extends JScrollPane {

    Container outerContainer;


    public MyCustomPane(Component view) {

        super(view);

    }


    public void setOuterContainer(Container outerContainer) {

        this.outerContainer = outerContainer;

    }


    private Dimension getCustomDimensions() {

        if (outerContainer == null) {

            return new Dimension(0, 0);

        }

        return new Dimension(outerContainer.getWidth() - 10, 60); //10 pixels less than container width, arbitrary height

    }

    @Override

    public Dimension getMaximumSize() {

        return getCustomDimensions();

    }

    @Override

    public Dimension getMinimumSize() {

        return getCustomDimensions();

    }

    @Override

    public Dimension getPreferredSize() {

        return getCustomDimensions();

    }

}


查看完整回答
反對 回復 2023-06-28
  • 1 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號