Java Swing中的面板向左浮动并带有竖线

时间:2018-07-19 00:40:19

标签: java swing layout jpanel jbutton

如何创建一个JPanel并带有许多向左浮动的按钮,并且仅具有垂直滚动条?

按钮的排序应如下所示。

1  2  3  4
5  6  7  8
9 10 11 12

2 个答案:

答案 0 :(得分:1)

如果使用GridLayout,则将无法添加滚动窗格,因为它将自动调整大小以适合其中的所有组件。一种更简单的方法是使用FlowLayoutsetPreferredSize(...)来设置面板的大小。尽管不建议设置面板大小,但仍需要以某种方式使滚动条投入使用。这是MCVE

enter image description here

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 Example extends JFrame {

    private final int BUTTON_WIDTH = 100;
    private final int BUTTON_HEIGHT = 50;
    private final int BUTTON_ROWS = 3;
    private final int BUTTON_COLUMNS = 4;
    private final int OFFSET = 20;// the width of the actual scroll bar in pixels (approximately).
    private final int PANEL_WIDTH = BUTTON_WIDTH * BUTTON_COLUMNS + OFFSET;
    private final int PANEL_HEIGHT = BUTTON_HEIGHT * BUTTON_ROWS + OFFSET;
    private final int SCROLL_HEIGHT = 100;//or whatever you would like...
    private final JButton[] buttons = new JButton[BUTTON_ROWS * BUTTON_COLUMNS];

    public Example() {
        JPanel panel = new JPanel(new FlowLayout());
        JScrollPane scroll = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        panel.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
        scroll.setPreferredSize(new Dimension(PANEL_WIDTH + OFFSET, SCROLL_HEIGHT));
        for (int i = 0; i < buttons.length; i++) {
            JButton button = new JButton((i + 1) + "");
            buttons[i] = button;
            button.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
            panel.add(button);
        }

        //if you want the panel to resize when window is stretched.
        //setLayout(new FlowLayout(FlowLayout.CENTER));
        add(scroll);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

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

答案 1 :(得分:1)

将按钮添加到(带有a的面板)网格布局中,以将它们排列在行和列中。将该面板添加到滚动窗格,然后将滚动窗格添加到边框布局的行开始约束,它们将显示在左侧。

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class LeftAlignedButtonGrid {

    private JComponent ui = null;

    LeftAlignedButtonGrid() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        /* BorderLayout offers a LINE_START constraint that will put a 
        single child component on the left hand side of the GUI (in any
        locale that uses left-to-right text orientation) */
        ui = new JPanel(new BorderLayout(4,4));

        JPanel buttonPanel = new JPanel(new GridLayout(0,4,2,2));
        for (int ii=1; ii<13; ii++) {
            buttonPanel.add(new JButton("" + ii));
        }
        ui.add(new JScrollPane(buttonPanel, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
                BorderLayout.LINE_START);
        ui.setBorder(new EmptyBorder(4,4,4,4));
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                LeftAlignedButtonGrid o = new LeftAlignedButtonGrid();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                // comment this out to allow the height of the GUI to be reduced, 
                // thus making the vertical scroll bar to have a purpose!
                //f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}