当我将 JList 添加到框架中时,我将其添加为滚动 Pane ,但是当我这样做时框架会变成空 这是我的代码
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);
最佳答案
由于组件顺序不正确,那些 JList
没有启动,请尝试一下。
从 JList
中删除 setBounds()
并设置 JScrollPane
的边界。然后将列表添加到滚动 Pane 。
JFrame frame2 = new JFrame();
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 jScrollPane = new JScrollPane(list);
jScrollPane.setBounds(36, 11, 161, 345);
frame2.getContentPane().add(jScrollPane);
JList list_1 = new JList(access);
list_1.setVisibleRowCount(10);
JScrollPane jScrollPane1 = new JScrollPane(list_1);
jScrollPane1.setBounds(356, 11, 161, 345);
frame2.getContentPane().add(jScrollPane1);
frame2.setVisible(true);
关于java - Jlist 在框架上不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52202345/