简体   繁体   English

Java Swing JtextField 插图

[英]Java Swing JtextField inset

I am working with Netbeans GUI and I would like to add 3 pixels of space at the beginning of my jTextField :我正在使用 Netbeans GUI,我想在 jTextField 的开头添加 3 个像素的空间:

在此处输入图片说明

I have tryied with setMargin, setInset in the GUI but it doesn't change anything.我在 GUI 中尝试了 setMargin、setInset,但它没有改变任何东西。

I have another question, why the bottom right border is not rounded ?我还有一个问题,为什么右下角的边框没有圆角? here is my code :这是我的代码:

Border roundedBorder = new LineBorder(new Color(210,210,210), 1, true);
researchTextField.setBorder(roundedBorder);

thank you very much,非常感谢您,

Regards问候

Using setMargin(...) should work.使用setMargin(...)应该可以工作。

However, if you are also using a Border then that may be the problem.但是,如果您还使用边框,那么这可能是问题所在。

Try using a CompoundBorder where the inner border is an EmptyBorder() and the outer border is your other border.尝试使用CompoundBorder ,其中内边框是 EmptyBorder(),外边框是您的另一个边框。 For example:例如:

Border rounded = new LineBorder(new Color(210,210,210), 1, true);
Border empty = new EmptyBorder(0, 3, 0, 0);
Border border = new CompoundBorder(rounded, empty);
textField.setBorder(border);

why the bottom right border is not rounded ?为什么右下角的边框不圆?

I'm not sure why your bottom/right is not rounded.我不确定为什么你的底部/右边没有圆角。 Using the Metal LAF on XP the right borders (top and bottom) appear rounded but the left borders are not rounded.在 XP 上使用 Metal LAF,右边框(顶部和底部)看起来是圆形的,但左边框不是圆形的。 When I use a border size of 2 or more all corners appear equally rounded.当我使用 2 个或更多的边框大小时,所有角都显得同样圆润。

setMargin(Inset myInset) worked for me: setMargin(Inset myInset)为我工作:

import java.awt.Insets;
import javax.swing.*;

public class TextFieldFun {
   public static void main(String[] args) {
      JTextField textfield = new JTextField(20);
      JPanel panel = new JPanel();
      panel.add(textfield);

      textfield.setMargin(new Insets(0, 10, 0, 0));

      JOptionPane.showMessageDialog(null, panel);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM