我是使用Swings的新手
我的要求是我必须创建两个按钮“Create”和“Remove”。“Create”按钮必须创建一个新的JTextArea并且“Remove”按钮必须删除所选的JTextarea。
我已成功创建了“创建”按钮,但
我的问题“如何删除所选的JTextArea意味着如果我点击了特定的JTextArea并且按下了”删除“按钮,则单击的JTextarea必须被删除”
提前致谢
答案 0 :(得分:4)
您需要使用附加到每个文本区域的某种焦点侦听器。基本上,你只对焦点获得的事件感兴趣。
发生这种情况时,您需要存储对焦点文本区域的引用。
当您单击“删除”按钮时,您将使用此先前存储的引用,并将其从父项中删除,例如
if (selectedTextArea != null) {
selectedTextArea.getParent().remove(selectedTextArea);
}
有关详细信息,请查看How to write a focus listener
<强>更新强>
正如评论中指出的那样,您需要致电revalidate
以便更新容器/布局,然后请求repaint
更新了示例
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestRemover {
public static void main(String[] args) {
new TestRemover();
}
public TestRemover() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextArea lastFocused;
private FocusHandler focusHandler;
private JPanel fields;
private JPanel buttons;
public TestPane() {
setLayout(new BorderLayout());
focusHandler = new FocusHandler();
fields = new JPanel(new GridBagLayout());
buttons = new JPanel();
JButton add = new JButton("Add");
JButton remove = new JButton("Remove");
buttons.add(add);
buttons.add(remove);
add(buttons, BorderLayout.SOUTH);
add(new JScrollPane(fields));
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JTextArea ta = new JTextArea(10, 10);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
fields.add(new JScrollPane(ta), gbc);
ta.addFocusListener(focusHandler);
fields.revalidate();
fields.repaint();
}
});
remove.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (lastFocused != null) {
// ViewPort.ScrollPane
Container scollPane = lastFocused.getParent().getParent();
Container parent = scollPane.getParent();
parent.remove(scollPane);
lastFocused = null;
parent.revalidate();
parent.repaint();
}
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public class FocusHandler extends FocusAdapter {
@Override
public void focusGained(FocusEvent e) {
if (e.getComponent() instanceof JTextArea) {
lastFocused = (JTextArea) e.getComponent();
}
}
}
}
}