2017-12-01 20 views
0

JTextAreaに追加するにはどうすればよいですか? 私はそのようなコードを持っています。国名をJTextFieldに印刷すると、RESTを使用していくつかの情報がJTextAreaに表示されます。しかし、私はこの目的のためにJButtonを使用したいと思います。ユーザーがJButtonをクリックすると、検索が開始され、表示されます。JButtonをJTextAreaに追加するには

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

私のプログラムではこのようなウィンドウを表示していますが、ウィンドウの下にボタンを置いています(写真上)。

enter image description here

+2

お待ちくださいJButtonをJTextAreaに追加したいですか?物理的にボタンをテキスト領域の内側に配置しますか?どうして?どのような問題が解決するのでしょうか?あなたの質問とあなたの問題を明確にしてください。 –

+0

その後、テキストフィールドと同じように、同じ 'ActionListener'をボタンに追加します。 – user1803551

+0

OK、 'JTextField'に国名を表示してから' JButton'をクリックするだけで、 'JTextArea'にRESTを使って情報が表示されます。たぶん私は 'JPanel'を作成し、そこにボタンを追加するべきでしょうか? – Viola

答えて

0

私はあなたが正確に期待しているのか分からないが、私はこのコードを書いて、私はそれを実行することはできません。原因私は、あなたのコードを使用していないんだけど、私は、これはあなたがしたいということだと思います。

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);}} 
関連する問題