我的问题是:如何获取CustomPanel的对象,以便能够访问其字段(因为在我的实际程序中,我在那里还有其他字段),还能够从ArrayList中删除它?
我不知道如何在类窗口中实现ActionListener,才能以某种方式将对象包含在包含被按下按钮的Arraylist中。
此外,我想知道我是否能够以某种方式在类CustomPanel中实现一个ActionListener,它可以影响作为类窗口实例的对象的行为。
我有以下几种代码:
public class Window extends JFrame{
ArrayList<CustomPanel> aLCustomPanel = new ArrayList();
JPanel jp = new JPanel();
public Window() {
for(int i=0;i<5;i++){
aLCustomPanel.add(new CustomPanel());
//here I could put the code from the 1 edit - see below
jp.add(aLCustomPanel.get(i));
}
this.add(jp);
}
public static void main(String args[]){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Window().setVisible(true);
}
});
}
}
class CustomPanel extends JPanel {
private JButton button;
public CustomPanel(){
button = new JButton("button");
this.add(button);
}
public JButton getButton(){
return this.button;
}
}
我的代码更长而奇怪,所以我试图提取(对于这个问题)导入的东西。
感谢您提前提供帮助!
编辑:
例如:我想从ArrayList中删除该对象,并按下该按钮。
//imagine this comment in above code
aLCustomPanel.get(aLCustomPanel.size()-1).getButton().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
button_IwantToDeleteYou(e); //here I want to remove the panel, containing the button that got pressed from the above ArrayList, which is located in Class Window
}
});
edit2:添加了一个缺少的括号并修复了一些错误,代码现在应该可以了。
您的代码包含一些“空白”,即我填写的缺少代码,如下所示:
JFrame
]方法setDefaultCloseOperation()
和pack()
和setLocationByPlatform()
添加了调用。我建议您参考javadoc来了解这些方法,以了解它们的作用。jp
类中的Window
类成员变量设置了一个布局管理器。是,您需要在ActionListener
类中向JButton
注册CustomPanel
,并且侦听器应位于您的Window
类中-扩展JFrame
的类。
这是我对您的代码的重写。请注意,我将类Window
的名称更改为CusPanel
,以便区分您的类和java.awt.Window
类。并不是说它有所作为,我只是不想使用JDK中的类名。
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class CusPanel extends JFrame implements ActionListener {
private static final int COUNT = 5;
private ArrayList<CustomPanel> aLCustomPanel = new ArrayList<>();
private JPanel jp = new JPanel(new GridLayout(0, COUNT));
public CusPanel() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
for (int i = 0; i < COUNT; i++) {
aLCustomPanel.add(new CustomPanel(this));
// here I could put the code from the 1 edit - see below
jp.add(aLCustomPanel.get(i));
}
this.add(jp);
pack();
setLocationByPlatform(true);
}
public void actionPerformed(ActionEvent actionEvent) {
Object source = actionEvent.getSource();
if (source instanceof JButton) {
JButton button = (JButton) source;
Container parent = button.getParent();
jp.remove(parent);
jp.invalidate();
jp.repaint();
pack();
// aLCustomPanel.remove(parent); <- optional
}
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new CusPanel().setVisible(true);
}
});
}
}
class CustomPanel extends JPanel {
private JButton button;
public CustomPanel(ActionListener parent) {
button = new JButton("button");
button.addActionListener(parent);
this.add(button);
}
public JButton getButton() {
return this.button;
}
}
注意,在删除CustomPanel
后,需要重新布置GUI组件,并且JFrame
的大小也应相应调整。因此,在actionPerformed()
方法中,我依次调用invalidate()
,repaint()
和pack()
。我还认为,如果从GUI中删除CustomPanel
,也应该从ArrayList
中删除它,但是,尽管我显然不了解全部内容,但我仍然不明白为什么要这样做首先要做的是背后的故事。
当然,由于每个按钮(和每个CustomPanel
)看起来都完全相同,因此您无法真正知道删除了哪个按钮。同样,我认为您看到的是大局,而我没有。