Java Swing JTextField 如何去除边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2281937/
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-08-13 05:38:12  来源:igfitidea点击:

Swing JTextField how to remove the border?

javaswingjtextfield

提问by Markus V.

Is there anyway to remove a border in a JTextField?

无论如何要删除 a 中的边框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 JTextFieldbecause I want people to be able highlight it.

我真的希望它看起来像一个JLabel- 但我仍然需要它是一个JTextField因为我希望人们能够突出它。

采纳答案by Tom Hawtin - tackline

From an answer to your previous questionyou know that some PL&Fs may clobber the border.

从对您上一个问题的回答中,您知道某些 PL&F 可能会破坏边界。

The obvious solution is to therefore override the setBordermethod that the PL&F is calling, and discard the change.

因此,显而易见的解决方案是覆盖setBorderPL&F 正在调用的方法,并放弃更改。

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

回答by Uri

Try setting it to BorderFactory.createEmptyBorder() instead of null. Sometimes this "does the trick" because setting it to null actually has a different meaning.

尝试将其设置为 BorderFactory.createEmptyBorder() 而不是 null。有时这“行得通”,因为将其设置为 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?

如果这不起作用,则您使用的外观可能会覆盖某些内容。你是使用默认的还是自定义的?

回答by Bj?rn

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”时,实际上是在告诉外观使用(操作系统的)本机边框样式(如果有)。

回答by stuklist

No you can't remove the border. Especially over the display of the AWT components. They use the native widget set (are drawn outside Java).

不,您不能删除边框。特别是在 AWT 组件的显示上。他们使用本机小部件集(在 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);

回答by Tomek

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. (Simply make some tests for different border thickness and observe quite strange way the JTextField places the cursor when you activate it first time.)

否则(当父容器的背景为空时)您将看到“I”光标永远停留在 JTextField 的左边缘。(只需对不同的边框粗细进行一些测试,并观察 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. If you don't specify the border thickness, you will see the cursor BETWEEN this border and the field remaining forever.

但是您将在所有四个方向上获得 2 个像素的光学场。如果您不指定边框粗细,您将在此边框和永远保留的字段之间看到光标。

回答by gerardw

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

might work.

可能工作。