java 中同一个包中调用另一个类中的方法
package txttest;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import txttest.sendTxt;
import java.awt.Button;
public class TxtTest {
public JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TxtTest window = new TxtTest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TxtTest() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea textArea = new JTextArea();
textArea.setBounds(0, 0, 434, 195);
frame.getContentPane().add(textArea);
textArea.append("TESTONE");
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(10, 218, 93, 23);
frame.getContentPane().add(btnNewButton);
Button button = new Button("button1");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton Button2 = new JButton("button2");
Button2.setBounds(120, 218, 93, 23);
frame.getContentPane().add(Button2);
frame.repaint();
}
});
button.setBounds(259, 218, 76, 23);
frame.getContentPane().add(button);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.append("TESTTWO");
sendTxt cbt = new sendTxt();
cbt.createbutton();
}
});
}
}
---------------------------------------------------------------------------
package txttest;
import java.awt.Button;
import txttest.TxtTest;
public class sendTxt {
public static void main(String args[]) {
}
public void createbutton(){
Button button = new Button("button");
button.setBounds(120, 218, 93, 23);
TxtTest bt = new TxtTest();
bt.frame.getContentPane().add(button);
bt.frame.repaint();
}
public void sendtext(){
//想在textarea中输入写内容
}
}
---------------------------------------------------------
在sendtxt中的createbutton()不能显示按钮
想在txttest中的textarea中输入写内容改如何,谢谢