java 点击删除组件_java删除一个组件

  • Post author:
  • Post category:java

你的问题是你的MouseLisetener。您正在侦听MainDisplayPanel,因此当您单击JPanel时,您在mousePressed方法中返回的MouseEvent#getComponent方法将返回MainDisplayPanel实例,因为这是正在被侦听的内容,而不是Resizable实例。在老鼠的帮助下。

方案包括:

创建一个MouseListener对象并将此同一对象添加到每个Resizable作为Resizable的MouseListener,或

使用您当前的设置,但将您的Resizable保存在ArrayList中,然后在mousePressed方法中遍历数组列表,以查看是否已使用componentAt(…)方法单击任何Resizable。

请注意,我必须创建自己的SSCCE才能解决此问题。在将来再次,请帮助我们,为我们做这件事,因为它符合您和我们的最佳利益,并表明您尊重我们的时间和我们的帮助。

编辑1

我的SSCCE:

import java.awt.*;

import java.awt.event.*;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import javax.swing.*;

@SuppressWarnings(“serial”)

public class MainDisplayPanel extends JPanel {

private static final int RESIZABLE_COUNT = 40;

private JButton deleteButton;

private Resizable current;

private Resizable resizer;

private List resizableList = new ArrayList();

public MainDisplayPanel(LayoutManager layout) {

super(layout);

deleteButton = new JButton(“Delete”);

deleteButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

deleteButtonActionPerformed(e);

}

});

this.addMouseListener(new MyMouseAdapter());

this.add(deleteButton);

for (int i = 0; i < RESIZABLE_COUNT; i++) {

addResizer();

}

}

private void deleteButtonActionPerformed(ActionEvent e) {

if (current != null) {

this.remove(current);

resizableList.remove(current);

current = null;

this.revalidate();

this.repaint();

}

}

public void addResizer() {

resizer = new Resizable();

this.add(resizer);

resizableList.add(resizer);

this.revalidate();

this.repaint();

}

private class MyMouseAdapter extends MouseAdapter {

@Override

public void mousePressed(MouseEvent e) {

current = null;

Component c = getComponentAt(e.getPoint());

for (Resizable resizable : resizableList) {

if (resizable == c) {

current = resizable;

resizable.setFill(true);

} else {

resizable.setFill(false);

}

}

}

}

public static void main(String[] args) {

JFrame jframe = new JFrame();

// !! jframe.add(new MainDisplayPanel(null));

jframe.add(new MainDisplayPanel(new FlowLayout()));

jframe.setSize(new Dimension(600, 400));

jframe.setVisible(true);

jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

@SuppressWarnings(“serial”)

class Resizable extends JPanel {

private static final int RESIZE_WIDTH = 50;

private static final int RESIZE_HEIGHT = 40;

private static final int THICKNESS = 5;

private static final Color FILL_COLOR = Color.pink;

public Resizable() {

Random rand = new Random();

// different color border so we can see that it was the clicked one that was deleted.

Color color = new Color(

rand.nextInt(255),

rand.nextInt(255),

rand.nextInt(255));

setBorder(BorderFactory.createLineBorder(color, THICKNESS));

}

@Override // so we can see it

public Dimension getPreferredSize() {

return new Dimension(RESIZE_WIDTH, RESIZE_HEIGHT);

}

public void setFill(boolean fill) {

Color fillColor = fill ? FILL_COLOR : null;

setBackground(fillColor);

repaint();

}

}


版权声明:本文为weixin_35688459原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。