重置/删除Swing中的边框 [英] Reset/remove a border in Swing

查看:231
本文介绍了重置/删除Swing中的边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常具体的编码问题:

Here's a very specific coding question:

我最近被要求在工作中维护一些旧的Java Swing GUI代码并遇到这个问题:

I've recently been asked to maintain some old-ish Java Swing GUI code at work and ran into this problem:

我已将我自己的InputVerifier子类MyFilenameVerifier附加到JTextField(但它也可能是用于这些目的的任何JComponent)。我重写了verify()方法,使其调用 super.verify(输入) (其中input是要验证的JComponent参数() )。如果 super.verify(输入) 返回false,我会这样做:

I've attached my own subclass of InputVerifier called MyFilenameVerifier to a JTextField (but it may as well be any JComponent for these purposes). I've overridden the verify() method such that it calls super.verify(input) (where input is the JComponent parameter to verify()). If super.verify(input) comes back false, I do:

input.setBorder(BorderFactory.createLineBorder(Color.RED));

这是在我之前很久就开始使用的整个应用程序用户界面的惯例,所以我不喜欢在使用其他方式吸引用户注意方面有很多选择(希望我这样做)。这就是它的工作方式。

This is a convention used throughout the UI of this application that started long before me, so I don't have a lot of choice as far as using other ways to get the users attention (wish I did). This is just the way it works.

问题是,一旦用户返回并在文本字段中输入有效内容,我需要一种方法将其设置回默认值border(而不仅仅是将其设置为Color.GRAY或其他任何颜色,与原始边框颜色不同)。我需要一种方式来说,删除额外的装饰并恢复正常或者只是将边框设置为默认值,换句话说。

Problem is, once the user goes back and types something valid into the text field, I need a way to set it back to default border (instead of just saying set it to Color.GRAY or whatever, which is a different color from its original border). I need a way to say, "remove the extra decoration and go back to normal" or just set the border to its default, in other words.

推荐答案

你不能只是打电话给 input.getBorder() 并在将边框设置为红色之前将其缓存到某处?

Couldn't you just call input.getBorder() and cache it somewhere before setting the border to red?

这篇关于重置/删除Swing中的边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆