2017-12-01 1 views
0

JButtonJTextArea에 어떻게 추가 할 수 있습니까? 나는 이런 코드를 가지고 있는데, 국가 이름을 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

좋아, 그냥 '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);}}