Не получаеться обуздать JScrollPane - Java

Узнай цену своей работы

Формулировка задачи:

Создаю Frame, в нём JPanel (FlowLayout) а там 50 buttons, всё красиво, но как только добавляю JScrollPane а на неё JPanel с кнопками, то все выстраивается в одну линию, перепробовал JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED по всякому, не получается. Help me!!
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.ScrollPaneConstants;
import javax.swing.JDesktopPane;
import javax.swing.border.BevelBorder;
import javax.swing.JMenuBar;
import java.awt.CardLayout;
 
public class Frame2 extends JFrame {
 
    private JPanel contentPane;
 
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Frame2 frame = new Frame2();
                    frame.setMinimumSize(new Dimension(350, 700));
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
 
    /**
     * Create the frame.
     */
    public Frame2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        JPanel panel = new JPanel();
        panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
        FlowLayout flowLayout = (FlowLayout) panel.getLayout();
        flowLayout.setAlignOnBaseline(true);
        //contentPane.add(panel, BorderLayout.CENTER);
        
        JButton button = new JButton("New button");
        button.setPreferredSize(new Dimension(230, 100));
        panel.add(button);
        
        JButton button_1 = new JButton("New button");
        button_1.setPreferredSize(new Dimension(230, 100));
        panel.add(button_1);
        
        JButton button_2 = new JButton("New button");
        button_2.setPreferredSize(new Dimension(230, 100));
        panel.add(button_2);
        
        JButton button_3 = new JButton("New button");
        button_3.setPreferredSize(new Dimension(230, 100));
        panel.add(button_3);
        
        JButton button_4 = new JButton("New button");
        button_4.setPreferredSize(new Dimension(230, 100));
        panel.add(button_4);
        
        JButton button_5 = new JButton("New button");
        button_5.setPreferredSize(new Dimension(230, 100));
        panel.add(button_5);
        
        JButton button_6 = new JButton("New button");
        button_6.setPreferredSize(new Dimension(230, 100));
        panel.add(button_6);
        
        JButton button_7 = new JButton("New button");
        button_7.setPreferredSize(new Dimension(230, 100));
        panel.add(button_7);
        
        JButton button_8 = new JButton("New button");
        button_8.setPreferredSize(new Dimension(230, 100));
        panel.add(button_8);
        
        JButton button_9 = new JButton("New button");
        button_9.setPreferredSize(new Dimension(230, 100));
        panel.add(button_9);
        
        JButton button_10 = new JButton("New button");
        button_10.setPreferredSize(new Dimension(230, 100));
        panel.add(button_10);
        
        JButton button_11 = new JButton("New button");
        button_11.setPreferredSize(new Dimension(230, 100));
        panel.add(button_11);
        
        JButton button_12 = new JButton("New button");
        button_12.setPreferredSize(new Dimension(230, 100));
        panel.add(button_12);
        
        JButton button_13 = new JButton("New button");
        button_13.setPreferredSize(new Dimension(230, 100));
        panel.add(button_13);
        
        JButton button_14 = new JButton("New button");
        button_14.setPreferredSize(new Dimension(230, 100));
        panel.add(button_14);
        
        JButton button_15 = new JButton("New button");
        button_15.setPreferredSize(new Dimension(230, 100));
        panel.add(button_15);
        
        JButton button_16 = new JButton("New button");
        button_16.setPreferredSize(new Dimension(230, 100));
        panel.add(button_16);
        
        JButton button_17 = new JButton("New button");
        button_17.setPreferredSize(new Dimension(230, 100));
        panel.add(button_17);
        
        JButton button_18 = new JButton("New button");
        button_18.setPreferredSize(new Dimension(230, 100));
        panel.add(button_18);
        
        JButton button_19 = new JButton("New button");
        button_19.setPreferredSize(new Dimension(230, 100));
        panel.add(button_19);
        
        JButton button_20 = new JButton("New button");
        button_20.setPreferredSize(new Dimension(230, 100));
        panel.add(button_20);
        
        JButton button_21 = new JButton("New button");
        button_21.setPreferredSize(new Dimension(230, 100));
        panel.add(button_21);
        
        JButton button_22 = new JButton("New button");
        button_22.setPreferredSize(new Dimension(230, 100));
        panel.add(button_22);
        
        JButton button_23 = new JButton("New button");
        button_23.setPreferredSize(new Dimension(230, 100));
        panel.add(button_23);
        
        JButton button_24 = new JButton("New button");
        button_24.setPreferredSize(new Dimension(230, 100));
        panel.add(button_24);
        
        JButton button_25 = new JButton("New button");
        button_25.setPreferredSize(new Dimension(230, 100));
        panel.add(button_25);
        
        JButton button_26 = new JButton("New button");
        button_26.setPreferredSize(new Dimension(230, 100));
        panel.add(button_26);
        
        JButton button_27 = new JButton("New button");
        button_27.setPreferredSize(new Dimension(230, 100));
        panel.add(button_27);
        
        JButton button_28 = new JButton("New button");
        button_28.setPreferredSize(new Dimension(230, 100));
        panel.add(button_28);
        
        JButton button_29 = new JButton("New button");
        button_29.setPreferredSize(new Dimension(230, 100));
        panel.add(button_29);
        
        JButton button_30 = new JButton("New button");
        button_30.setPreferredSize(new Dimension(230, 100));
        panel.add(button_30);
        
        JButton button_31 = new JButton("New button");
        button_31.setPreferredSize(new Dimension(230, 100));
        panel.add(button_31);
        
        JButton button_32 = new JButton("New button");
        button_32.setPreferredSize(new Dimension(230, 100));
        panel.add(button_32);
        
        JButton button_33 = new JButton("New button");
        button_33.setPreferredSize(new Dimension(230, 100));
        panel.add(button_33);
        
        JButton button_34 = new JButton("New button");
        button_34.setPreferredSize(new Dimension(230, 100));
        panel.add(button_34);
        
        JButton button_35 = new JButton("New button");
        button_35.setPreferredSize(new Dimension(230, 100));
        panel.add(button_35);
        
        JButton button_36 = new JButton("New button");
        button_36.setPreferredSize(new Dimension(230, 100));
        panel.add(button_36);
        
        JButton button_37 = new JButton("New button");
        button_37.setPreferredSize(new Dimension(230, 100));
        panel.add(button_37);
        
        JButton button_38 = new JButton("New button");
        button_38.setPreferredSize(new Dimension(230, 100));
        panel.add(button_38);
        
        JButton button_39 = new JButton("New button");
        button_39.setPreferredSize(new Dimension(230, 100));
        panel.add(button_39);
        
        JButton button_40 = new JButton("New button");
        button_40.setPreferredSize(new Dimension(230, 100));
        panel.add(button_40);
        
        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.NORTH);
        
        JMenuBar menuBar = new JMenuBar();
        panel_1.add(menuBar);
        
        //JPanel panel_2 = new JPanel();
        
        //panel_2.setLayout(new CardLayout(0, 0));
        
        JScrollPane scrollPane = new JScrollPane(panel);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        contentPane.add(scrollPane, BorderLayout.CENTER);
        //panel_2.add(scrollPane, "name_6038829586666");

    }
 
}

Решение задачи: «Не получаеться обуздать JScrollPane»

textual
Листинг программы
panel.setPreferredSize(new Dimension(320, 1000));

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 3.875 из 5