第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

JScrollPane 內(nèi)部的內(nèi)部 JScrollPane 無法正常工作

JScrollPane 內(nèi)部的內(nèi)部 JScrollPane 無法正常工作

喵喵時光機 2023-06-28 15:26:50
我正在嘗試創(chuàng)建一個帶有滾動條的容器,并且容器內(nèi)部有兩個內(nèi)部面板。頂部內(nèi)面板內(nèi)還有另一個 JScrollPane。但目前我面臨的問題是,當我的頂部內(nèi)面板太長(寬度)時,頂部內(nèi)面板內(nèi)的滾動條被禁用,我只能滾動容器的滾動條。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);    }}我希望得到的是,如果頂部內(nèi)面板的寬度太長,那么頂部內(nèi)面板內(nèi)的滾動條將可見并允許滾動。不是容器中的滾動條。
查看完整描述

1 回答

?
www說

TA貢獻1775條經(jīng)驗 獲得超8個贊

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


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


我無法找到一種方法讓內(nèi)部窗格動態(tài)收縮,而無需有效刪除外部窗格的功能,這是行不通的,因為您的問題是專門針對另一個功能內(nèi)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();

    }

}


查看完整回答
反對 回復(fù) 2023-06-28
  • 1 回答
  • 0 關(guān)注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號