Java, putting a JScrollList onto a JPanel

2019-03-05 04:29发布

问题:

I am wondering if there is a way to add a JList on a JFrame with a JPanel. I am making a program and I searched all over the interwebs for an answer but I can't find one. Anyways Preferably the JList with a scrollpane (that works) if it is possible

Here is my code:

public static void main(String args[]) {
  JScrollPane scrollpane;
  JFrame frame = new JFrame();
   JList list;


    String categories[] = { "1", "2", "3","4", "5", "6", "7","8", "9", "10", "11", "12" };

    list = new JList(categories);
    JScrollPane scrollpane1 = new JScrollPane(list);

    frame.getContentPane().add(scrollpane1, BorderLayout.CENTER);
  MyClass sl = new MyClass();
frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setSize(300, 200);
frame.setVisible(true);


}