Java Swing JtextField 插入

发布于 2024-12-18 09:49:26 字数 383 浏览 5 评论 0原文

我正在使用 Netbeans GUI,我想在 jTextField 的开头添加 3 个像素的空间:

在此处输入图像描述

我已经尝试在 GUI 中使用 setMargin、setInset 但它没有改变任何东西。

我还有一个问题,为什么右下边框不是圆角的?这是我的代码:

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

非常感谢,

问候

I am working with Netbeans GUI and I would like to add 3 pixels of space at the beginning of my jTextField :

enter image description here

I have tryied with setMargin, setInset in the GUI but it doesn't change anything.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2024-12-25 09:49:26

使用 setMargin(...) 应该可以。

但是,如果您还使用边框,那么这可能就是问题所在。

尝试使用CompoundBorder,其中内部边框是EmptyBorder(),外部边框是您的其他边框。例如:

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);

阅读 Swing 教程中关于如何使用边框<的部分/a> 了解更多信息和示例。

为什么右下边框不是圆角的?

我不确定为什么你的底部/右侧不是圆形的。在 XP 上使用 Metal LAF 时,右边框(顶部和底部)显示为圆形,但左边框不是圆形的。当我使用 2 或更多的边框大小时,所有角都显示为同样的圆角。

Using setMargin(...) should work.

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. 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);

Read the section from the Swing tutorial on How to Use Borders for more information and examples.

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. When I use a border size of 2 or more all corners appear equally rounded.

郁金香雨 2024-12-25 09:49:26

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);
   }
}

setMargin(Inset myInset) worked for me:

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);
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文