我有以下代码,其中自定义面板添加到JTaskPane。该窗格有一个标签,当我点击它时会被删除。但是,我的自定义面板未被删除。如何点击面板中的十字架将其删除?
以下是快照。所以,在这种情况下,我可以删除“关闭”JLabel,但是当我点击“交叉”标志时我无法删除紫色标签
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);
}
});
}
}
答案 0 :(得分:2)
您遇到了上下文问题。
首先,您创建一个SRLSearchPanel
private SRLSearchPanel panel = new SRLSearchPanel();
您的FilterBox
但是你创建了另一个实例......
SRLSearchPanel spanel = new SRLSearchPanel();
frame.add(spanel);
这是实际出现在屏幕上的那个,因此与panel
进行交互无效,因为它与您实际放在屏幕上的内容无关
相反,您可以尝试简单地请求从其父级中移除FilterBox
...
closeBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getParent().remove(FilterBox.this);
}
});