英文:
JList disappears when added to JScrollPane
问题
JList mainlist = new JList();
JScrollPane listScroller = new JScrollPane(mainlist);
listScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
listScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
mainlist.setToolTipText("List of People");
mainlist.setFont(new Font("Consolas", Font.BOLD, 13));
int pos = mainlist.getModel().getSize();
DefaultListCellRenderer renderer = (DefaultListCellRenderer) mainlist.getCellRenderer();
renderer.setHorizontalAlignment(JLabel.CENTER);
mainlist.setModel(model);
mainlist.setForeground(Color.GREEN);
mainlist.setBackground(new Color(44, 47, 51));
mainlist.setBounds(10, 108, 780, 248);
mainlist.setFixedCellHeight(20);
mainlist.setFixedCellWidth(30);
mainlist.setBorder(new EmptyBorder(10, 10, 10, 10));
frm.getContentPane().add(listScroller);
英文:
I don't know why this is happening, I've looked up nearly every Stack Overflow question regarding this.
The error is, When I add a JScrollPane
to JList
, Nothing shows up. Even if I'm adding it to the frame.
JList mainlist = new JList();
JScrollPane listScroller = new JScrollPane(mainlist);
listScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
listScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
mainlist.setToolTipText("List of People");
mainlist.setFont(new Font("Consolas", Font.BOLD, 13));
int pos = mainlist.getModel().getSize();
DefaultListCellRenderer renderer = (DefaultListCellRenderer)mainlist.getCellRenderer();
renderer.setHorizontalAlignment(JLabel.CENTER);
mainlist.setModel(model);
mainlist.setForeground(Color.GREEN);
mainlist.setBackground(new Color(44, 47, 51));
mainlist.setBounds(10, 108, 780, 248);
mainlist.setFixedCellHeight(20);
mainlist.setFixedCellWidth(30);
mainlist.setBorder(new EmptyBorder(10,10, 10, 10));
frm.getContentPane().add(listScroller);
答案1
得分: 0
你应该对你的滚动窗格应用边界,而不是你的列表。
listScroller.setBounds(10, 108, 780, 248);
还要确保你的内容面板使用了 null
布局,以便使 setBounds()
生效,或者更好的方法是使用边界布局,并根据你的需要设置 preferred&maximum Size()
。
英文:
You should apply bounds to your scrollPane and not Your List
listScroller.setBounds(10, 108, 780, 248);
Also make sure Your Content Pane has null
layout for setBounds()
to work or better just use an border layout and set the preferred&maximum Size()
to what works for you
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论