简体   繁体   English

Swing JTextField如何去掉边框?

[英]Swing JTextField how to remove the border?

Is there anyway to remove a border in a JTextField ?无论如何要删除JTextField中的边框吗?

txt = new JTextField();
txt.setBorder(null);   // <-- this has no effect.

I would really want it to look like a JLabel - but I still need it to be a JTextField because I want people to be able highlight it.我真的希望它看起来像一个JLabel - 但我仍然需要它是一个JTextField因为我希望人们能够突出它。

JTextField textField = new JTextField();
textField.setBorder(javax.swing.BorderFactory.createEmptyBorder());

http://java.sun.com/javase/6/docs/api/javax/swing/BorderFactory.html http://java.sun.com/javase/6/docs/api/javax/swing/BorderFactory.html

When setting the border to 'null', you're actually telling the look & feel to use the native border style (of the operating system) if there is one.将边框设置为“null”时,您实际上是在告诉外观使用(操作系统的)本机边框样式(如果有的话)。

From an answer to your previous question you know that some PL&Fs may clobber the border.从对上一个问题的回答中,您知道某些 PL&F 可能会破坏边界。

The obvious solution is to therefore override the setBorder method that the PL&F is calling, and discard the change.因此,显而易见的解决方案是覆盖 PL&F 正在调用的setBorder方法,并丢弃更改。

JTextField text = new JTextField() {
    @Override public void setBorder(Border border) {
        // No!
    }
};

Try setting it to BorderFactory.createEmptyBorder() instead of null.尝试将其设置为 BorderFactory.createEmptyBorder() 而不是 null。 Sometimes this "does the trick" because setting it to null actually has a different meaning.有时这“成功了”,因为将其设置为 null 实际上具有不同的含义。

If that does not work, it is possible that the look and feel you are using is overriding something.如果这不起作用,则您使用的外观可能会覆盖某些东西。 Are you using the default or something custom?你是使用默认的还是自定义的?

No you can't remove the border.不,您不能删除边框。 Especially over the display of the AWT components.尤其是 AWT 组件的显示。 They use the native widget set (are drawn outside Java).他们使用本机小部件集(在 Java 之外绘制)。

Try to make the line that is similar to your background... for example if your background is white then you have to:尝试制作与您的背景相似的线条......例如,如果您的背景是白色的,那么您必须:

setBorder(BorderFactory.createLineBorder(Color.white));

Then set the background to white:然后将背景设置为白色:

setBackground(Color.white);
txt.setBorder(new LineBorder(Color.BLACK,0));

可能会奏效。

You can simply你可以简单地

textField.setBorder(null);

or或者

textField.setBorder(new EmptyBorder(0, 0, 0, 0))

The only way to make it works in ALL circumstances is the following setting:使其适用于所有情况的唯一方法是以下设置:

setBorder (BorderFactory.createLineBorder (new Color (0, 0, 0, 0), 2));

otherwise (when you have null background of the parent container) you will see the "I" cursor remaining forever at the left edge of your JTextField.否则(当您有父容器的空背景时)您将看到“I”光标永远保留在您的 JTextField 的左边缘。 (Simply make some tests for different border thickness and observe quite strange way the JTextField places the cursor when you activate it first time.) (只需对不同的边框厚度进行一些测试,并观察 JTextField 在您第一次激活它时放置光标的非常奇怪的方式。)

Alternatively you can set:或者,您可以设置:

setBorder (BorderFactory.createLineBorder (getBackground (), 2));

but you will obtain the field opticaly larger by 2 pixels in all four directions.但是您将在所有四个方向上获得 2 个像素的光学场。 If you don't specify the border thickness, you will see the cursor BETWEEN this border and the field remaining forever.如果您不指定边框粗细,您将看到光标在此边框和字段之间永久保留。

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

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