简体   繁体   English

更改AWT TextField的边框颜色

[英]Changing Border Color of AWT TextField

In AWT application I need to set border color of TextField. 在AWT应用程序中,我需要设置TextField的边框颜色。

In JTextField, I know that we do can do the following 在JTextField中,我知道我们可以做到以下几点

JTextField tf = new JTextField();
tf.setBorder(BorderFactory.createLineBorder(Color.decode("#2C6791")));

But setBorder() method is not availiable in awt TextField. 但是在awt TextField中无法使用setBorder()方法。 Is there any workaround for this problem? 这个问题有解决方法吗?

The AWT TextField does not support borders, as you've found. 正如您所发现的,AWT TextField不支持边框。 You could emulate a border by putting the text field inside a Panel that's just slightly larger than the textfield and changing the background color of the panel. 您可以通过将文本字段放在一个稍微大于文本字段的Panel中并更改面板的背景颜色来模拟边框。

For compatibility with look & feel variations, the setBorder() API recommends the following: "In general, when you want to set a border on a standard Swing component other than JPanel or JLabel , we recommend that you put the component in a JPanel and set the border on the JPanel ." 为了兼容外观和感觉变化, setBorder() API建议如下:“通常,当您想在除JPanelJLabel之外的标准Swing组件上设置边框时,我们建议您将组件放在JPanel ,在JPanel上设置边框。“

Addendum: While this suggests an approach, it is irrelevant to a pure AWT application. 附录:虽然这表明了一种方法,但它与纯AWT应用程序无关。

tf.setBorder(new LineBorder(Color.red,1));
//new LineBorder(color,width);
tf.setBorder(new LineBorder(Color.red,1));
//new LineBorder(color,width);

Because the method is overloaded you can define the Color, and leave the rest to the default. 由于方法过载,您可以定义颜色,并将其余部分保留为默认值。 Alternatively, you can define the whole method and choose the Color, line Thickness, and type of corners; 或者,您可以定义整个方法并选择颜色,线条粗细和角点类型; rounded or not. 圆形与否。

    public LineBorder(Color color) {
        this(color, 1, false);
    }
    public LineBorder(Color color, int thickness)  {
        this(color, thickness, false);
    }
    @ConstructorProperties({"lineColor", "thickness", "roundedCorners"})
    public LineBorder(Color color, int thickness, boolean roundedCorners)  {
        lineColor = color;
        this.thickness = thickness;
        this.roundedCorners = roundedCorners;
    }

Create a line border with the specified color and width 创建具有指定颜色和宽度的线条边框

Border border = BorderFactory.createLineBorder(Color.BLUE, 5);

Set the border of this component 设置此组件的边框

 JTextField.setBorder(border);

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

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