java jtextfield 默认光标位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1351959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:13:27  来源:igfitidea点击:

jtextfield default cursor position

javaswing

提问by PROFESSOR

I'm working with JTextFieldand the code for the text field. I'm trying to make a textfield as big as the notepad but the cursor always appears in the middle. I want to put in the starting of the textfield.

我正在使用JTextField文本字段的代码。我正在尝试制作一个与记事本一样大的文本字段,但光标总是出现在中间。我想放在文本字段的开头。

tf=new JTextField(50);
tf.setCaretPosition(0);

f.setLayout(new BorderLayout());
f.add(tf,BorderLayout.CENTER);

回答by camickr

Not a very clear question. Since you are adding the text field to the center of a BorderLayout, the text field will automatically resize both vertically and horizontally to fill the space available in the frame.

不是很清楚的问题。由于您将文本字段添加到 BorderLayout 的中心,因此文本字段将自动调整垂直和水平方向的大小以填充框架中的可用空间。

By default the cursor will appear on the left horizontally and in the center vertically. So I guess the vertical alignment is what you are complaining about. Well, I don't know any way to solve the problem since this is the way the text field UI works.

默认情况下,光标将水平出现在左侧,垂直出现在中间。所以我想垂直对齐是你所抱怨的。好吧,我不知道有什么方法可以解决这个问题,因为这是文本字段 UI 的工作方式。

If you change the font size of the text then it will appear bigger and the caret will appear more to the top.

如果您更改文本的字体大小,那么它会显得更大,并且插入符号会更靠上。

I suggest you use try using a JTextArea. The Single Line Text Areamay work the way you want.

我建议您尝试使用 JTextArea。该单行文本区域可能工作,你想要的方式。

回答by OMG Ponies

Try setting the text alignment:

尝试设置文本对齐方式:

JTextField textfield = new JTextField("Initial Text");

// Left-justify the text
textfield.setHorizontalAlignment(JTextField.LEFT);

// Center the text
textfield.setHorizontalAlignment(JTextField.CENTER);

// Right-justify the text
textfield.setHorizontalAlignment(JTextField.RIGHT);

回答by David Moles

Since JTextFieldholds only a single line of text, you might be better off sizing the JTextFieldmore appropriately.

由于JTextField仅包含一行文本,因此您最好JTextField更合适地调整大小。

As far as I know there is no good way to do exactly what you asked for. Note that while there is setHorizontalAlignment(), there is no corresponding setVerticalAlignment(). setAlignmentY()doesn't do it either (that sets the alignment of components added to a container, which doesn't make sense for a JTextField).

据我所知,没有什么好方法可以完全满足您的要求。请注意,虽然有setHorizontalAlignment(),但没有相应的setVerticalAlignment()setAlignmentY()也不这样做(设置添加到容器的组件的对齐方式,这对 a 没有意义JTextField)。

You can fake it by placing the JTextFieldin the north of the BorderLayout, as follows:

您可以通过将JTextField放在 的北部来伪造它BorderLayout,如下所示:

tf.setBorder(null);
f.setBackground(Color.WHITE);
f.add(tf, BorderLayout.NORTH);  
f.add(Box.createVerticalGlue(), BorderLayout.CENTER);

But I suspect what you really want may be multiple lines, in which case you should be using a JTextArea.

但我怀疑您真正想要的可能是多行,在这种情况下,您应该使用JTextArea.

回答by NovoBook

You should use JScrollPane. It will provide a scroll bar, and will fulfill your needs too.

您应该使用 JScrollPane。它将提供一个滚动条,也将满足您的需求。

JTextArea area=new JTextArea();
JScrollPane scroll=new JScrollPane(area);
cnt.add(scroll);
area.setLineWrap(true);
scroll.setBounds(320, 100, 650, 300);