java移除组件_java删除组件

你的问题是你的MouseLisetener。您正在监听MainDisplayPanel,因此当您单击JPanel时,mousePressed方法中由e返回的MouseEvent#getComponent方法将返回MainDisplayPanel实例,因为这是正在监听的内容,而不是可调整的实例在鼠标下。

解决方案包括:

创建一个MouseListener的对象,并使用您的当前设置添加此相同的对象,以每个能够调整大小的MouseListener的可调整大小,或

,但握住你的可调整的在一个ArrayList和然后遍历mousePressed方法中的数组列表,以查看是否使用componentAt(...)方法单击了任何可调整大小。

请注意,我不得不创建自己的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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值