将 JButton 添加到 JTextArea

To add JButton to JTextArea

如何在 JTextArea 上添加 JButton? 我有这样的代码,当我在 JTextField 中打印国家名称时,一些使用 REST 的信息显示在 JTextArea 上。但我想为此目的使用 JButton。当用户点击JButton时,信息将开始搜索并显示。

public class Client extends JPanel implements ActionListener{

protected JTextField textField;
protected JTextArea textArea;
protected static JButton search;

public Client() {
    super(new GridBagLayout());

    search = new JButton("Search");
    search.setBounds(100,190,60,30);

    textField = new JTextField(20);
    textField.addActionListener(this);

    textArea = new JTextArea(10, 20);
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea);

    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;

    c.fill = GridBagConstraints.HORIZONTAL;
    add(textField, c);

    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;
    add(scrollPane, c);
}

我的程序显示这样一个window,我想把我的按钮放在window的底部(如图)。

我不知道你到底在期待什么,但我写了这段代码,我没有使用你的代码,因为我不能 运行 它,但我认为这就是你想要的.

JFrame gui = new JFrame("Button on bottom.");

JPanel panel = new JPanel(new BorderLayout());

JTextField textfield = new JTextField();
textfield.setText("Australia");

JTextArea textarea = new JTextArea();
textarea.setText("Australia, -27, 133. AUD");

JButton button = new JButton("Button on bottom.");
button.setFont(new java.awt.Font("Dialog", 0, 15));
button.setBorderPainted(false);
button.setFocusable(false);
button.setForeground(new java.awt.Color(255, 255, 255));
button.setBackground(new java.awt.Color(0, 140, 255));

panel.add(textfield, BorderLayout.PAGE_START);
panel.add(textarea);
panel.add(button, BorderLayout.PAGE_END);

gui.setDefaultCloseOperation(gui.EXIT_ON_CLOSE);
gui.setSize(300, 300);
gui.setLocationRelativeTo(null);
gui.add(panel);
gui.setVisible(true);}}