簡體   English   中英

填充水平但滾動垂直

[英]Fill horizontal but scroll vertical

我有一個看起來像這樣的JFrame

在此輸入圖像描述

當它被調整大小時,它將使JComboBoxes適應JFrame的大小,如下所示:

在此輸入圖像描述

這就是我想要的。 但是當我添加一個JScrollPaneJComboBoxes不再適應:

在此輸入圖像描述

任何想法如何使JComboBox仍然用添加的JScrollPane填充HORIZONTAL?(使得JScrollPane只影響VERTICAL滾動)

public class FillHorizonScrollVertical extends JFrame {

    private boolean withScroller = true;
    private JPanel content;

    public FillHorizonScrollVertical() {

        // FILL = BOTH for JFrame
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        this.setLayout(new GridBagLayout());

        // GridLayout = All content gets the same size
        content = new JPanel(new GridLayout());

        // ADD TO THE FRAME
        JScrollPane scroller = new JScrollPane(content);
        if (withScroller)
            this.add(scroller, c);
        else
            this.add(content, c);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        // ADDING THE BOXES AFTER SETTING VISIBLE
        addBoxes();
        this.pack();
    }

    /**
     * Create JComboBoxes and adding them to the JPanel
     */
    private void addBoxes() {

        // CREATE JCOMBOBOXES
        JComboBox cb1 = new JComboBox();
        cb1.addItem("");
        cb1.addItem("aaaaaaaaaaaaaaaaaa");

        JComboBox cb2 = new JComboBox();
        cb2.addItem("");

        JComboBox cb3 = new JComboBox();
        cb3.addItem("");

        content.add(cb1);
        content.add(cb2);
        content.add(cb3);
    }

    public static void main(String[] args) {
        new FillHorizonScrollVertical();
    }
}

您可以使用Scrollable接口,它允許您標記它應跟蹤視口的寬度或高度。 在這種情況下,您希望跟蹤寬度,這將強制視圖始終與視圖端口的大小相同...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Rectangle;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Scrollable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ScrollableExample {

    public static void main(String[] args) {
        new ScrollableExample();
    }

    public ScrollableExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(new TestPane()));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel implements Scrollable {

        public TestPane() {
            setLayout(new GridLayout());
            JComboBox cb1 = new JComboBox();
            cb1.addItem("");
            cb1.addItem("aaaaaaaaaaaaaaaaaa");

            JComboBox cb2 = new JComboBox();
            cb2.addItem("");

            JComboBox cb3 = new JComboBox();
            cb3.addItem("");

            add(cb1);
            add(cb2);
            add(cb3);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return getPreferredSize();
        }

        @Override
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 32;
        }

        @Override
        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 32;
        }

        @Override
        public boolean getScrollableTracksViewportWidth() {
            return true;
        }

        @Override
        public boolean getScrollableTracksViewportHeight() {
            return false;
        }

    }

}

請查看如何使用滾動窗格獲取更多詳細信息

您可以在JScrollPane上設置滾動策略

JScrollPane scroller = new JScrollPane(content, 
    ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

您可能還想查看Scrollable接口,它允許微調滾動行為。

通過添加解決:

cb1.setPreferredSize(new Dimension(0, 30));
cb2.setPreferredSize(new Dimension(0, 30));
cb3.setPreferredSize(new Dimension(0, 30));

暫無
暫無

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

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