gpt4 book ai didi

java - 如何动态创建和删除 JTextArea

转载 作者:行者123 更新时间:2023-11-29 03:32:33 25 4
gpt4 key购买 nike

我刚开始使用 Swings

我的要求是我必须创建两个按钮“Create”和“Remove”。“Create”按钮必须创建一个新的 JTextArea,而“Remove”按钮必须删除选定的 JTextarea。

我已经成功创建了“创建”按钮,但是

我的问题是“如何删除选定的 JTextArea 意味着如果我单击特定的 JTextArea 并按下“删除”按钮,则必须删除单击的 JTextarea

提前致谢

最佳答案

您需要使用某种附加到每个文本区域的焦点监听器。基本上,您只对焦点获得事件感兴趣。

发生这种情况时,您需要存储对获得焦点的文本区域的引用。

例如,当您单击删除按钮时,您将使用此先前存储的引用并将其从其父对象中删除

if (selectedTextArea != null) {
selectedTextArea.getParent().remove(selectedTextArea);
}

看看How to write a focus listener了解更多详情

更新

正如评论中指出的那样,您需要调用revalidate 以便更新容器/布局,然后请求重绘

更新了例子

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();

}
}
}
}
}

关于java - 如何动态创建和删除 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17377937/

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