简体   繁体   中英

Java swing scrollpane doesn't show the scroll bar

I was working on a java swing gui project for my course. When I was doing that, I found that I had too much information in a panel and it was not able to show everything. As a result, I wanted to add a Jscrollpane and I did some research about how to use this function but it didn't seem to work for my project, and I had no reason why even after I tried almost everything I could find on google. Here is my code:

JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(10, 1100));
musiclist.setBounds(0, 0, 190, 1000);
musiclist.setLayout(null);
listpanel.setLayout(null);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);
listpanel.add(selectButton);
c.add(musiclist, BorderLayout.WEST);
frame.setVisible(true);
musiclist.setVisible(true);

The problem now is that the scroll bar never shows up even I set the vertical to always show. I am just new to java, so any help will be helpful and thank you all for your time!

JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(100, 1100));
musiclist.setBounds(0, 0, 190, 1000);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);

JList mlist=new JList();
DefaultListModel lmodel=new DefaultListModel();
lmodel.addElement("Song 1");
lmodel.addElement("Song 2");
lmodel.addElement("Song 3");
mlist.setModel(lmodel);

listpanel.setLayout(new BorderLayout());
listpanel.add(mlist, BorderLayout.CENTER);
listpanel.add(selectButton, BorderLayout.SOUTH);
c.add(musiclist, BorderLayout.WEST);
musiclist.setVisible(true);
frame.setVisible(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM