메인 클래스 :다른 클래스에서 textarea에 액세스하는 방법?
public class tuna {
public static void main(String[] args) {
JFrame frame1 = new JFrame();
frame1.setVisible(true);
frame1.add(new apple());
frame1.setSize(200 , 240);
}
}
2 등석
public class apple extends JPanel{
JTextArea ta = new JTextArea();
Border blackline = BorderFactory.createLineBorder(Color.black);
apple(){
setBorder(blackline);
System.out.println("apple");
ta.setText("hello");
ta.setEditable(false);
add(ta);
add(new doctor());
repaint();
revalidate();
}
}
3 종
public class doctor extends JPanel implements ActionListener{
public JButton butt = new JButton("change");
Border blackline = BorderFactory.createLineBorder(Color.black);
public doctor(){
setBorder(blackline);
add(butt);
}
@Override
public void actionPerformed(ActionEvent e){
if(e.getSource() == butt)
{
System.out.println("she");
}
}
}
왜 매번 버튼은 늘 콘솔에서 "그녀를"인쇄를 누르면됩니다. 버튼을 누를 때마다 텍스트 영역 내부의 텍스트를 변경하려면 프로그램이 필요합니다. 예를 들어 버튼을 눌렀을 때 텍스트 영역에 "world"가 추가되어야합니다.
예, 이것이 더 좋은 방법입니다. 좋은 대답, +1 – cybersoft
이것은 문제를 해결! 하지만 전체 텍스트를 변경하는 대신 텍스트를 추가하고 싶다면 어떨까요? – PNC
@PNC'JTextArea # append' ... – MadProgrammer