在Java中,通过自定义容器内的按钮刷新自定义容器
早上好 我有一个从java.awt.Container继承的类。他的工作范围是将垂直显示的文件列表包装为“文件名标签+删除按钮”列表 文件正确显示,每次添加新文件时,都会正确刷新 组件类的代码:在Java中,通过自定义容器内的按钮刷新自定义容器,java,awt,refresh,components,containers,Java,Awt,Refresh,Components,Containers,早上好 我有一个从java.awt.Container继承的类。他的工作范围是将垂直显示的文件列表包装为“文件名标签+删除按钮”列表 文件正确显示,每次添加新文件时,都会正确刷新 组件类的代码: public class AttachmentsList extends Container { private List<File> attachments = null; public AttachmentsList(List<File> attachments) {
public class AttachmentsList extends Container {
private List<File> attachments = null;
public AttachmentsList(List<File> attachments)
{
super();
this.attachments = attachments;
buildListAttachments();
}
@Override
public void repaint()
{
buildListAttachments();
super.repaint();
}
protected void buildListAttachments()
{
int yTraslation = 0;
for (File attachment : this.attachments)
{
Label fileName = new Label(attachment.getName());
fileName.setBounds(10, 10 + yTraslation, 70, 20);
//invisible, just contain the absolute path...
final Label path = new Label(attachment.getAbsolutePath());
fileName.setBounds(10, 10 + yTraslation, 70, 20);
Button deleteFile = new Button("x");
deleteFile.setBounds(90, 10 + yTraslation, 20, 20);
deleteFile.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
File fileToRemove = new File(path.getText());
attachments.remove(fileToRemove);
System.out.println(attachments.size());
repaint();//<---- It doesn't refresh the main UI.
}
});
add(fileName);
add(deleteFile);
yTraslation += 20;
}
}
公共类附件列表扩展了容器{
私有列表附件=null;
公共附件列表(列表附件)
{
超级();
这个。附件=附件;
buildListAttachments();
}
@凌驾
公共无效重绘()
{
buildListAttachments();
super.repaint();
}
受保护的void buildListAttachments()
{
int Ytrastration=0;
对于(文件附件:this.attachments)
{
标签文件名=新标签(attachment.getName());
fileName.setBounds(10,10+ytrasation,70,20);
//不可见,只包含绝对路径。。。
最终标签路径=新标签(attachment.getAbsolutePath());
fileName.setBounds(10,10+ytrasation,70,20);
按钮删除文件=新按钮(“x”);
deleteFile.setBounds(90,10+yTraslation,20,20);
deleteFile.addActionListener(新ActionListener()
{
@凌驾
已执行的公共无效操作(操作事件e)
{
File fileToRemove=新文件(path.getText());
附件。删除(文件删除);
System.out.println(attachments.size());
repaint();//终于根据安德鲁的建议找到了解决方案
这是我的班级:
public class AttachmentsList extends JPanel
{
private List<File> attachments = null;
private JTable jt = null;
private DefaultTableModel model = null;
private JScrollPane scroller = null;
public AttachmentsList(List<File> attachments)
{
super();
this.attachments = attachments;
setLayout(new GridLayout());
Vector<Vector> rowData = new Vector<Vector>();
for (File iterable_element : this.attachments)
{
Vector<Object> row = new Vector<Object>();
row.addElement(iterable_element.getName());
row.addElement(new JCheckBox());
rowData.add(row);
}
Vector<String> columnNames = new Vector<String>();
columnNames.add("File");
columnNames.add("Selected");
this.jt = new JTable(rowData, columnNames);
this.jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
this.model = (DefaultTableModel) this.jt.getModel();
final TableColumnModel columnModel = this.jt.getColumnModel();
columnModel.getColumn(0).setPreferredWidth(100);
columnModel.getColumn(1).setPreferredWidth(60);
this.scroller = new JScrollPane(this.jt);
add(this.scroller);
}
@Override
public void repaint()
{
buildTable();
}
protected void buildTable()
{
if (this.attachments != null)
{
Vector<Vector> rowData = new Vector<Vector>();
for (File currentFile : this.attachments)
{
Vector<Object> row = new Vector<Object>();
row.addElement(currentFile.getName());
row.addElement(false);
rowData.add(row);
}
this.model.addRow(rowData);
}
}
public void addFile(File file)
{
Vector<Object> row = new Vector<Object>();
row.addElement(file.getName());
row.addElement(false);
this.model.addRow(row);
}
public List<File> getAttachments()
{
return attachments;
}
public void setAttachments(List<File> attachments)
{
this.attachments = attachments;
}
}
公共类附件列表扩展了JPanel
{
私有列表附件=null;
私有JTable jt=null;
私有DefaultTableModel模型=null;
私有JScrollPane滚动条=null;
公共附件列表(列表附件)
{
超级();
这个。附件=附件;
setLayout(新的GridLayout());
向量行数据=新向量();
对于(文件iterable_元素:this.attachments)
{
向量行=新向量();
row.addElement(iterable_element.getName());
addElement(新的JCheckBox());
rowData.add(行);
}
向量列名称=新向量();
columnNames.add(“文件”);
columnNames。添加(“选定”);
this.jt=新的JTable(行数据、列名称);
this.jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
this.model=(DefaultTableModel)this.jt.getModel();
final TableColumnModel columnModel=this.jt.getColumnModel();
columnModel.getColumn(0).setPreferredWidth(100);
columnModel.getColumn(1).setPreferredWidth(60);
this.scroller=newjscrollpane(this.jt);
添加(这个滚动条);
}
@凌驾
公共无效重绘()
{
buildTable();
}
受保护的void buildTable()
{
如果(this.attachments!=null)
{
向量行数据=新向量();
对于(文件currentFile:this.attachments)
{
向量行=新向量();
row.addElement(currentFile.getName());
行。添加元素(false);
rowData.add(行);
}
this.model.addRow(rowData);
}
}
公共无效添加文件(文件文件)
{
向量行=新向量();
row.addElement(file.getName());
行。添加元素(false);
this.model.addRow(row);
}
公共列表getAttachments()
{
归还附件;
}
公共附件(列出附件)
{
这个。附件=附件;
}
}
这是一个正常工作的原始组件,它显示了存储在列表中的文件为什么要使用AWT?请参阅许多放弃使用AWT组件而支持Swing的好理由。谢谢Andrew!我不知道AWT有那个答案中描述的那么差。我没有任何关于Java GUI的经验,所以我拿了一本旧Java学校的书来构建它在应用程序中,它解释了AWT,但没有解释Swing,这就是我开始使用AWT的原因。“我拿了一本旧的Java学校的书”一本书!多么……古怪。我一直期待(害怕)在Youtube上看到教程的链接。;)但出于中间立场(基于网络,低带宽)看。:)是的……可能更好。我相信那本书好几年了,但现在是时候把它烧掉了!尽管我读了你在下面的链接,但我还是不明白问题所在……我是个新手!:)将它们垂直显示为“文件名标签+删除按钮”列表我会使用JList
或JTable
来显示文件,也许可以使用下面的一个JButton
来删除选定的文件
。请参阅,了解基于Swing的JTable
(或JTree
)中如何优雅地表示文件。
public class AttachmentsList extends JPanel
{
private List<File> attachments = null;
private JTable jt = null;
private DefaultTableModel model = null;
private JScrollPane scroller = null;
public AttachmentsList(List<File> attachments)
{
super();
this.attachments = attachments;
setLayout(new GridLayout());
Vector<Vector> rowData = new Vector<Vector>();
for (File iterable_element : this.attachments)
{
Vector<Object> row = new Vector<Object>();
row.addElement(iterable_element.getName());
row.addElement(new JCheckBox());
rowData.add(row);
}
Vector<String> columnNames = new Vector<String>();
columnNames.add("File");
columnNames.add("Selected");
this.jt = new JTable(rowData, columnNames);
this.jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
this.model = (DefaultTableModel) this.jt.getModel();
final TableColumnModel columnModel = this.jt.getColumnModel();
columnModel.getColumn(0).setPreferredWidth(100);
columnModel.getColumn(1).setPreferredWidth(60);
this.scroller = new JScrollPane(this.jt);
add(this.scroller);
}
@Override
public void repaint()
{
buildTable();
}
protected void buildTable()
{
if (this.attachments != null)
{
Vector<Vector> rowData = new Vector<Vector>();
for (File currentFile : this.attachments)
{
Vector<Object> row = new Vector<Object>();
row.addElement(currentFile.getName());
row.addElement(false);
rowData.add(row);
}
this.model.addRow(rowData);
}
}
public void addFile(File file)
{
Vector<Object> row = new Vector<Object>();
row.addElement(file.getName());
row.addElement(false);
this.model.addRow(row);
}
public List<File> getAttachments()
{
return attachments;
}
public void setAttachments(List<File> attachments)
{
this.attachments = attachments;
}
}