JList 添加到 JScrollPane 后消失

huangapple go评论66阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年4月7日 19:06:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61078583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定