Swing没有显示正确的字体集

时间:2014-09-04 02:32:51

标签: java swing fonts jlist

我在JList内有一个JScrollPane,并将字体设置为Tahoma,简单,20,由于某种原因,它仍然显示为非常小的文字,我不是确定这是为什么?

就这样,这不是一块巨大的代码,我尽可能地减少它 存储的帧使用GridBagLayout

ArrayUtil.ArrayToListModel会将List<T>;转换为ListMode<T>;

JFrame frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0};
gridBagLayout.rowHeights = new int[]{0};
gridBagLayout.columnWeights = new double[]{1.0};
gridBagLayout.rowWeights = new double[]{1.0};
frame.getContentPane().setLayout(gridBagLayout);

JScrollPane showlistScrollPane = new JScrollPane();
GridBagConstraints gbc_showlistScrollPane = new GridBagConstraints();
gbc_showlistScrollPane.insets = new Insets(0, 0, 0, 5);
gbc_showlistScrollPane.fill = GridBagConstraints.BOTH;
gbc_showlistScrollPane.gridx = 0;
gbc_showlistScrollPane.gridy = 0;
frame.getContentPane().add(showlistScrollPane, gbc_showlistScrollPane);

ArrayList<String> randomData = new ArrayList<String>();
randomData.add("Random drhgiudrhgiudhri");
randomData.add("Data dsirhgodhroih");
ListModel<String> pagesList = ArrayUtil.ArrayToListModel(randomData);

showlist = new JList<String>(pagesList);
showlist.setFont(new Font("Tahoma", Font.PLAIN, 20));
showlistScrollPane.setViewportView(showlist);

无论我为showlist制作字体有多大或多小,它都不会改变默认字体。

1 个答案:

答案 0 :(得分:3)

内容在JList中的显示方式并非由JList定义,而是由分配给ListCellRenderer的{​​{1}}定义,因此

JList

showlist.setFont(new Font("Tahoma", Font.PLAIN, 20));

中显示的内容无效

请查看Writing a Custom Cell Renderer了解详情

作为一个例子......

List of happy things

JList