有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java Swing float左侧面板,带竖条

我如何创建一个JPanel,其中有许多按钮左对齐,只有垂直滚动条

按钮的排序应如下所示

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

共 (2) 个答案

  1. # 1 楼答案

    如果使用GridLayout,则无法添加滚动窗格,因为它会自动调整大小以适应其中的所有组件。一种更简单的方法是使用FlowLayoutsetPreferredSize(...)来设置面板的大小。虽然不建议设置面板的大小,但您仍然需要以某种方式使用滚动条。下面是一个^{

    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();
        }
    }
    
  2. # 2 楼答案

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

    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);
        }
    }