当我将JList添加到框架时,我将其添加为滚动窗格,但是当我这样做时,框架变为空
这是我的代码

frame2 = new JFrame();
    frame2.setBounds(100, 100, 543, 432);
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.getContentPane().setLayout(null);

    JList list = new JList(names);
    list.setBounds(36, 11, 161, 345);
    list.setVisibleRowCount(10);



frame2.getContentPane().add(new JScrollPane(list));


    JList list_1 = new JList(access);
    list_1.setBounds(356, 11, 161, 345);
    list_1.setVisibleRowCount(10);

    frame2.getContentPane().add(new JScrollPane(list_1));

    frame2.setVisible(true);

最佳答案

首先,您应使用Layout Manager避免此类问题。当出于某些原因想要避免使用它们时,必须提供滚动窗格的大小。

frame2 = new JFrame();
frame2.setBounds(100, 100, 543, 432);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.getContentPane().setLayout(null);

JList list = new JList(names);
list.setVisibleRowCount(10);

JScrollPane scroller = new JScrollPane(list);
scroller.setBounds(36, 11, 161, 345);

frame2.getContentPane().add(scroller);


JList list_1 = new JList(access);

list_1.setVisibleRowCount(10);

scroller = new JScrollPane(list_1);
scroller.setBounds(356, 11, 161, 345);
frame2.getContentPane().add(scroller);

frame2.setVisible(true);

09-15 15:16