与往常一样,您应该发布SSCCE。这是一段显示所需功能的工作代码
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AppendTextArea {
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
@Override
public void run() {
JFrame frame = createUI();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
} );
}
private static JFrame createUI(){
JFrame result = new JFrame( "TestFrame" );
JTextArea firstArea = new JTextArea( 30, 30 );
JTextArea secondArea = new JTextArea( 30,30 );
JButton appendButton = new JButton( "Append" );
appendButton.addActionListener( new ComeOnListener( firstArea ) );
appendButton.addActionListener( new ComeOnListener( secondArea ) );
result.add( firstArea, BorderLayout.NORTH );
result.add( secondArea, BorderLayout.CENTER );
result.add( appendButton, BorderLayout.SOUTH );
return result;
}
private static class ComeOnListener implements ActionListener {
private final JTextArea auxTextArea;
public ComeOnListener(JTextArea jta) {
auxTextArea = jta;
}
@Override
public void actionPerformed(ActionEvent e) {
auxTextArea.append("Come on!");
}
}
}
如您所见,您发布的代码只是复制到此代码段中并按预期工作。问题很可能位于代码中的其他位置。