JTextFields自动调整大小 [英] JTextFields automatically resizing

查看:501
本文介绍了JTextFields自动调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个程序,该程序应该允许您键入文本,并且它将以计算机上的每种字体显示该文本.这是加载字体时发生的屏幕截图: 现在,我意识到除了自动调整JTextField的大小以外,还有十亿其他问题,但是我想一次只关注一件事.无论如何,当我在JScrollPane中向下滚动时,会发生以下情况: 有人可以告诉我如何处理GridBagConstraints或JTextFields吗?这里有一些代码,希望对您有所帮助-

I am making a program that is supposed to allow you to type in text, and it will that text in every single font on the computer. Here is a screenshot of what happens just as the fonts are loaded: Now, I realize that there are a billion other problems besides the automatic resizing of the JTextFields, but I want to focus on one thing at a time. Anyway, whenenver I scroll down in the JScrollPane, here's what happens: Could someone please tell me what I have to do with the GridBagConstraints or the JTextFields to fix this problem? Here's a bit of code, hope it helps -

 gbc.insets = new Insets(2, 5, 2, 5);
 gbc.gridx = 0;
 gbc.gridwidth = gbc.gridheight = 1;
 gbc.weightx = gbc.weighty = 1;
 gbc.anchor = GridBagConstraints.LINE_START;

推荐答案

尝试添加gbc.fill = GridBagConstraints.HORIZONTAL以鼓励组件使用可用的水平空间.

Try adding gbc.fill = GridBagConstraints.HORIZONTAL to encourage the components to use the available horizontal space.

更好的解决方案是使用JList.

A better solution would be to use a JList.

我将每个Font添加到ListModel并使用主文本,允许ListCellRenderer呈现它.

I would add each Font to a ListModel and using the master text, allow a ListCellRenderer to render it.

但是就是我

这篇关于JTextFields自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆