java 中同一个包中调用另一个类中的方法

qiuzigame 2015-07-19 08:40:53
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中输入写内容改如何,谢谢
...全文
771 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,350

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧