Java 删除按钮功能
我已经冲洗了下面的窗口 我想开发删除功能。单击删除按钮时,相应的文本字段应从窗口中删除。 我将在下面附上设计代码-仅用于Java 删除按钮功能,java,swing,Java,Swing,我已经冲洗了下面的窗口 我想开发删除功能。单击删除按钮时,相应的文本字段应从窗口中删除。 我将在下面附上设计代码-仅用于添加更多文件按钮操作。 单击添加更多文件按钮时,另一个文本字段和删除按钮正在创建并添加到标题面板。S我无法连接这些文本字段和按钮(在同一行中)。我知道这不是开发这种类型windows的正确格式 如何设计(或重新设计)以满足删除功能 添加更多文件操作代码: cAttach.addActionListener(new ActionListener() {
添加更多文件
按钮操作。
单击添加更多文件
按钮时,另一个文本字段和删除按钮正在创建并添加到标题面板
。S我无法连接这些文本字段和按钮(在同一行中)。我知道这不是开发这种类型windows的正确格式
如何设计(或重新设计)以满足删除功能
添加更多文件
操作代码:
cAttach.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt)
{
File file = null;
int returnVal = fc.showOpenDialog(EMailDialog.this);
if ((returnVal == javax.swing.JFileChooser.APPROVE_OPTION))
{
attachments.add(fc.getSelectedFile());
file = fc.getSelectedFile();
fieldPosition += 5;
CTextField cFileNew = new CTextField();
headerPanel.add(cFileNew, new GridBagConstraints(0, fieldPosition,
5, 1, 0.0, 0.0, GridBagConstraints.EAST,
GridBagConstraints.HORIZONTAL, new Insets(7, 80, 5,
185), 0, 0)); // Text field
//thush
CButton cDelete= new CButton("Delete");
headerPanel.add(cDelete, new GridBagConstraints(0, fieldPosition , 5,
1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE,
new Insets(7, 425, 5, 105), 0, 0)); // thush
cFileNew.setText("" + file);
listOfTextFields.add(cFileNew);
headerPanel.updateUI();
mainPanel.updateUI();
}
}
});
你有办法把他们联系起来。只需将它们保存/存储在例如
HashMap
其中每个
按钮
都映射到其相应的文本字段
。当按下按钮时
获取一个单击,只需在
HashMap
中进行查找,拉出相应的文本字段
需要什么就用什么 IMO这是一个很好的用例,用于组合和/或尽可能地匿名化操作本身 让我解释一下:每次附加新文件时,都需要添加一对文本字段删除按钮,对吗?按delete按钮时要执行的操作始终相同:从其父容器中删除按钮和关联的文本字段(我猜它是
headerPanel
)。这就是我要做的:
1) 定义一个单一的和通用的操作,如下所示。此操作应附加到所有删除按钮
final Action deleteAction = new AbstractAction("Delete") {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
JTextField textField = (JTextField)button.getClientProperty("AssociatedTextField");
if (textField != null) {
JComponent parentContainer = (JComponent)button.getParent();
parentContainer.remove(button);
parentContainer.remove(textField);
parentContainer.revalidate();
parentContainer.repaint();
SwingUtilities.windowForComponent(parentContainer).pack();
}
}
};
如您所见,此操作旨在附加到按钮并查找AssociatedTextField
客户端属性。如果此请求检索到的值不为null,则从父容器中删除按钮和关联的文本字段,重新验证组件层次结构并最终打包框架
2) 要使前面的代码正常工作,必须将文本字段添加到按钮客户端属性映射中,如下所示:
cAttach.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
...
CTextField cFileNew = new CTextField();
...
CButton cDelete = new CButton(deleteAction);
cDelete.putClientProperty("AssociatedTextField", cFileNew);
...
}
});
注意:按钮是使用上一步中定义的操作创建的
3) 最后,您不必调用updateUI
(此方法的作用与您认为的不同),只需重新验证组件层次结构并打包框架以反映更改:
cAttach.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
...
headerPanel.revalidate();
headerPanel.repaint();
SwingUtilities.windowForComponent(headerPanel).pack();
}
});
我想在上面建一座塔moon@OpenJDK向我们展示你的moontower代码,我们将尝试提供帮助。为了更快地获得更好的帮助,请发布一个(最小完整的可验证示例)。顺便说一句-我会将文件添加到
JList
中,并提供一个漂亮的渲染器(显示文件图标)。那么,设计一个删除选定的按钮就容易多了。因此,我的按钮和文本字段声明应该在添加更多文件按钮的操作侦听器之外。但是在本例中,我在添加更多文件按钮的ActionListener中声明和定义了这两个部分。之所以这样做,是因为可能需要附加“n”个文件。@Thush不太清楚。Map Map=new Hashmap()
声明不在操作侦听器范围内。在actionPerformed()
中,创建了一个新的按钮+文本字段,并将这两个字段添加到Hashmap:map.put(myButton,myTextField)
@Thush在某个“全局”(不要按字面意思)级别创建Hashmap实例,例如,Hashmap可以是实例成员。创建对象(按钮、文本字段)时,将其添加到该HashMap。