使用 Swing 显示文件状态的好方法是什么

我想做以下事情,我需要一些关于什么是最好的方法的建议。

我有一个 JList,它显示用户通过单击添加 (+) 按钮添加的文件,并在用户单击删除 (-) 按钮时删除文件。对于每个添加的文件,我想关联一个图标,该图标应指示文件的状态。例如,如果用户只添加了文件而没有运行文件(我有另一个 JButton 用于使用选定的文件运行应用程序),那么这个图标应该是红色的,一旦用户运行它,这个图标应该变成绿色. 此外,如果用户通过单击 (-) 按钮删除文件,它也应该删除与该特定文件关联的图标。下面是我想要的图片表示。

我正在考虑将 ImageIcon 与每个添加的文件相关联,但我不确定如何更改其外观以显示状态。我也不确定在删除文件时如何删除 ImageIcon。有没有其他方法(除了 ImageIcon)来做到这一点?任何帮助/建议表示赞赏。

http://img3.mukewang.com/61419ad400010e8a09100947.jpg

ITMISS
浏览 146回答 1
1回答

慕村9548890

在编程中,数据为王。数据如何表示不应该是数据的考虑因素,这是 UI/视图层的领域/职责。这通常由模型-视图-控制器模式表示在您的示例中,您有两条(基本)信息。一个文件和一个状态(未运行、已运行、已删除),您要将此信息组合为“数据”。在 Java 中,这通常意味着一个普通的旧 Java 对象(或 Pojo)因为状态只有有限的可能性,我们可以用 aenum来表示它,从而限制有效值public enum FileStatus {&nbsp; &nbsp; NOT_RUN, RUN, DELETED;}然后我们可以创建我们自己的 pojo ...public class FileOperation {&nbsp; &nbsp; private File file;&nbsp; &nbsp; private FileStatus status;&nbsp; &nbsp; public FileOperation(File file, FileStatus status) {&nbsp; &nbsp; &nbsp; &nbsp; this.file = file;&nbsp; &nbsp; &nbsp; &nbsp; this.status = status;&nbsp; &nbsp; }&nbsp; &nbsp; public FileOperation(File file) {&nbsp; &nbsp; &nbsp; &nbsp; this(file, FileStatus.NOT_RUN);&nbsp; &nbsp; }&nbsp; &nbsp; public File getFile() {&nbsp; &nbsp; &nbsp; &nbsp; return file;&nbsp; &nbsp; }&nbsp; &nbsp; public FileStatus getStatus() {&nbsp; &nbsp; &nbsp; &nbsp; return status;&nbsp; &nbsp; }&nbsp; &nbsp; public void setStatus(FileStatus newStatus) {&nbsp; &nbsp; &nbsp; &nbsp; if (status == newStatus) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; this.status = newStatus;&nbsp; &nbsp; }}现在,当我们想知道文件的状态时,我们知道从哪里获取它。但是JList呢?你问,好问题。我们真正想要的是某种方式JList可以在任何FileOperation对象的状态发生变化时得到通知。现在,您可以迭代ListModel,但这不是一个非常干净的解决方案,更好的解决方案是允许在FileOperation更改时生成事件并ListModel监听它们并采取自己的行动。这是观察者模式ƒ的基本概念有很多方法可以做到这一点,但我很懒,所以我将使用可用的属性更改 APIpublic class FileOperation {&nbsp; &nbsp; private File file;&nbsp; &nbsp; private FileStatus status;&nbsp; &nbsp; private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);&nbsp; &nbsp; public FileOperation(File file, FileStatus status) {&nbsp; &nbsp; &nbsp; &nbsp; this.file = file;&nbsp; &nbsp; &nbsp; &nbsp; this.status = status;&nbsp; &nbsp; }&nbsp; &nbsp; public FileOperation(File file) {&nbsp; &nbsp; &nbsp; &nbsp; this(file, FileStatus.NOT_RUN);&nbsp; &nbsp; }&nbsp; &nbsp; public File getFile() {&nbsp; &nbsp; &nbsp; &nbsp; return file;&nbsp; &nbsp; }&nbsp; &nbsp; public FileStatus getStatus() {&nbsp; &nbsp; &nbsp; &nbsp; return status;&nbsp; &nbsp; }&nbsp; &nbsp; public void setStatus(FileStatus newStatus) {&nbsp; &nbsp; &nbsp; &nbsp; if (status == newStatus) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; FileStatus oldStatus = status;&nbsp; &nbsp; &nbsp; &nbsp; status = newStatus;&nbsp; &nbsp; &nbsp; &nbsp; propertyChangeSupport.firePropertyChange("status", oldStatus, status);&nbsp; &nbsp; }&nbsp; &nbsp; public void addPropertyChangeListener(PropertyChangeListener listener) {&nbsp; &nbsp; &nbsp; &nbsp; propertyChangeSupport.addPropertyChangeListener(listener);&nbsp; &nbsp; }&nbsp; &nbsp; public void removePropertyChangeListener(PropertyChangeListener listener) {&nbsp; &nbsp; &nbsp; &nbsp; propertyChangeSupport.removePropertyChangeListener(listener);&nbsp; &nbsp; }}现在我们需要一个ListModel可以响应它的......public class FileOperationListModel extends AbstractListModel<FileOperation> {&nbsp; &nbsp; private List<FileOperation> items = new ArrayList<FileOperation>(25);&nbsp; &nbsp; private PropertyChangeListener handler = new PropertyChangeHandler();&nbsp; &nbsp; public void add(FileOperation fo) {&nbsp; &nbsp; &nbsp; &nbsp; fo.addPropertyChangeListener(handler);&nbsp; &nbsp; &nbsp; &nbsp; int size = items.size();&nbsp; &nbsp; &nbsp; &nbsp; items.add(fo);&nbsp; &nbsp; &nbsp; &nbsp; fireIntervalAdded(this, size, size);&nbsp; &nbsp; }&nbsp; &nbsp; public void remove(FileOperation fo) {&nbsp; &nbsp; &nbsp; &nbsp; int index = items.indexOf(fo);&nbsp; &nbsp; &nbsp; &nbsp; if (index < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fo.removePropertyChangeListener(handler);&nbsp; &nbsp; &nbsp; &nbsp; items.remove(fo);&nbsp; &nbsp; &nbsp; &nbsp; fireIntervalRemoved(this, index, index);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getSize() {&nbsp; &nbsp; &nbsp; &nbsp; return items.size();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public FileOperation getElementAt(int index) {&nbsp; &nbsp; &nbsp; &nbsp; return items.get(index);&nbsp; &nbsp; }&nbsp; &nbsp; public class PropertyChangeHandler implements PropertyChangeListener {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void propertyChange(PropertyChangeEvent evt) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!(evt.getSource() instanceof FileOperation)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileOperation fo = (FileOperation) evt.getSource();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int index = items.indexOf(fo);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fireContentsChanged(FileOperationListModel.this, index, index);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}现在,拼图的最后一部分是您将需要一个ListCellRenderer可以显示您想要的信息的自定义。为此,您必须首先阅读如何使用列表和编写自定义单元格渲染器
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java