简体   繁体   English

将JButton添加到JTextArea

[英]To add JButton to JTextArea

How can I add JButton on JTextArea ? 如何在JTextArea上添加JButton I have such a code, when I print name of country in JTextField some information using REST displays on JTextArea . 我有这样的代码,当我在JTextField打印国家名称时,使用REST在JTextArea显示的一些信息。 But I want to use for this purpose JButton. 但我想为此使用JButton。 When user click JButton information will be start searching and then display. 当用户单击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);
}

My program show such a window, I want to place my button on the bottom of the window (on the picture). 我的程序显示了一个这样的窗口,我想将我的按钮放在窗口的底部(在图片上)。

在此处输入图片说明

I don't know what are you expecting exactly, but I wrote this code, I'm not use your code, cause I can't run it, but I think this is that you want. 我不知道您的期望是什么,但是我编写了这段代码,我没有使用您的代码,因为我无法运行它,但是我认为这是您想要的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM