gpt4 book ai didi

java - 如何从 JXTaskPane 中删除组件?

转载 作者:行者123 更新时间:2023-12-01 23:13:41 27 4
gpt4 key购买 nike

我有以下代码段,其中将自定义面板添加到 JTaskPane。该 Pane 有一个标签,当我单击它时该标签会被删除。但是,我的自定义面板没有被删除。如何单击面板中的叉号将其删除?

以下是快照。因此,在这种情况下,我可以删除“关闭”JLabel,但当我单击“十字”符号时无法删除紫色标签

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import org.jdesktop.swingx.JXTaskPane;

public class Test {
private SRLSearchPanel panel = new SRLSearchPanel();

class SRLSearchPanel extends JPanel {

private JXTaskPane filterPanel = new JXTaskPane();
private JTextField searchField = new JTextField(20);

public SRLSearchPanel() {
setLayout(new BorderLayout());
final JLabel lbl = new JLabel("Close");
filterPanel.add(lbl);
lbl.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
filterPanel.remove(lbl);
filterPanel.revalidate();
}
});

filterPanel.add(searchField);
add(filterPanel, BorderLayout.WEST);

searchField.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
FilterBox filterBox = new FilterBox(searchField.getText());
filterPanel.add(filterBox);
}
});
}

public void removeFilter(FilterBox filterBox) {
filterPanel.remove(filterBox);
}
}

class FilterBox extends JPanel {

private static final int PADDING = 2;
private static final String PAD_STR = " ";

private JLabel filterLbl = new JLabel();
private JButton closeBtn = new JButton("X");

public FilterBox(String label) {
filterLbl.setText(PAD_STR + label + PAD_STR);
closeBtn.setBorderPainted(false);
closeBtn.setFocusPainted(false);
closeBtn.setContentAreaFilled(false);
closeBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
System.err.println("Adding action listener "
+ FilterBox.this.hashCode());
closeBtn.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
panel.removeFilter(FilterBox.this);
}
});

this.setLayout(new BorderLayout());
this.add(filterLbl, BorderLayout.WEST);
this.add(closeBtn, BorderLayout.EAST);

this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
System.err.println("Removing filter");
panel.filterPanel.remove(FilterBox.this);
}
});
}

@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(new Color(0f, 0f, 1f, 0.3f));
g.fillRoundRect(0, 0, this.getWidth(), this.getHeight() - PADDING,
20, 20);
g.setColor(Color.BLACK);
}
}

public static void main(String[] args) {
Test i = new Test();
i.testRun();
}

public void testRun() {
final JFrame frame = new JFrame();
SRLSearchPanel spanel = new SRLSearchPanel();
frame.add(spanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800, 800));
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
});
}
}

最佳答案

您遇到了上下文问题。

首先创建 SRLSearchPanel 的实例变量

private SRLSearchPanel panel = new SRLSearchPanel();

这由您的FilterBox使用

但是随后您创建了另一个实例...

SRLSearchPanel spanel = new SRLSearchPanel();
frame.add(spanel);

这是实际在屏幕上的内容,因此与面板交互没有任何效果,因为它与您实际放置在屏幕上的内容没有关系

相反,您可以尝试简单地请求将 FilterBox 从其父级中删除...

closeBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getParent().remove(FilterBox.this);
}
});

关于java - 如何从 JXTaskPane 中删除组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21540308/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com