Scrollbars not appearing in JScrollPane

后端 未结 2 1222
旧巷少年郎
旧巷少年郎 2021-01-05 21:23

I want to make a window with two buttons with great heights and a scrollbar on a side. The problem is that no scrollbar appears. Here is my code

public class         


        
2条回答
  •  伪装坚强ぢ
    2021-01-05 21:39

    To make the scrollbar always appear, use:

    yourScrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    

    ...

    import javax.swing.*;
    import java.awt.*;
    public class Window {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            //namestanje teme
            try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (UnsupportedLookAndFeelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            JFrame frame = new JFrame("frame");
        //  frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            frame.setPreferredSize(new Dimension(100,95));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel panel = new JPanel();
    
            JButton but1 = new JButton();
            JButton but2 = new JButton();
    
            panel.add(but1);
            panel.add(but2);
    
            but1.setSize(50, 505);
            but2.setSize(50, 505);
    
            but1.setLocation(0, 0);
            but2.setLocation(400, 400);
    
            but1.setText("1");
            but2.setText("2");
    
    
            JScrollPane scroll = new JScrollPane(panel);
            scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    
            frame.setLayout(new FlowLayout());
            frame.pack();
            frame.add(scroll);
            frame.setVisible(true);         
        }
    
    }
    

提交回复
热议问题