点击button 清空area Jframe中有个panel1 panel1 里有个textArea 和 一个 panel2 panel2里有个button 我想点击这个button的时候让textArea里的内容清空 请问怎么做?谢谢! 呵呵 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 为button添加个监听点击button 对textArea.setText(""); 将panel1对象传给panel2给textArea添加get方法在panel2中getTextArea().setText(""); button.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ textArea.setText(); } }); 谢谢楼上 关键是textArea到了actionPerformed里面就没作用了 编译器说找不到这个textArea啊 //panel1中加了个textAreaJTextArea tArea=new JTextArea(15,20);tArea.setBorder(new LineBorder(Color.blue));pn1.add(tArea);//panel1的下部加了个panel2JPanel pn2=new JPanel(true);pn1.add("South",pn2);//panel2中添加个button 点这个button清空上面的textAreaJButton btn2=new JButton("Cancel");btn2.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ tArea.setText(""); } } );pn2.add(btn2);编译器说找不到tArea 一個辦法 是你把JTextArea tArea 定義到外面另一個辦法就是樓上說的 用個get()方法 Why didn't u define textArea and button in the same panel? 把tArea定义成全局的,你的应该不是全局的public JTextArea tArea然后在实现的代码用:tArea=new new JTextArea(15,20); btn2.addActionListener(new ActionListener(){ private JTextArea tArea = null; public void actionPerformed(ActionEvent event){ tArea.setText(""); } public ActionListener setTarget(JTextArea tArea) { this.tArea = tArea; return this; }}.setTarget(外面那个JTextArea引用)); 潇洒的写法 jt sf wo zai jf... ....Please look at this....http://topic.csdn.net/u/20080829/12/c0f22640-1063-4569-a98c-ee228ae60266.html Win8快捷键与Eclipse冲突 String s2=new String(char 2,0,4);后面的0 4是什么含义 java获取mysql进程信息 二代身份证读卡器的二次开发(jni) 如何在JUnit4里面定义test method的执行顺序™ 用javascript写一个动态编辑表格 进度条和后台程序处理问题 请问谁有java读取串口数据的源码和方法 关于synchronized的问题 请教:一个关于rose的初级问题??? JTable 如何在Header中添加Jcheckbox等控件?注意是header! 发个基础问题,大家来看看~~~
点击button
对textArea.setText("");
给textArea添加get方法
在panel2中getTextArea().setText("");
new ActionListener(){
public void actionPerformed(ActionEvent event){
textArea.setText();
}
}
);
JTextArea tArea=new JTextArea(15,20);
tArea.setBorder(new LineBorder(Color.blue));
pn1.add(tArea);//panel1的下部加了个panel2
JPanel pn2=new JPanel(true);
pn1.add("South",pn2);//panel2中添加个button 点这个button清空上面的textArea
JButton btn2=new JButton("Cancel");
btn2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
tArea.setText("");
}
}
);
pn2.add(btn2);编译器说找不到tArea
另一個辦法就是樓上說的 用個get()方法
Why didn't u define textArea and button in the same panel?
public JTextArea tArea
然后在实现的代码用:tArea=new new JTextArea(15,20);
new ActionListener(){
private JTextArea tArea = null;
public void actionPerformed(ActionEvent event){
tArea.setText("");
}
public ActionListener setTarget(JTextArea tArea) {
this.tArea = tArea;
return this;
}
}.setTarget(外面那个JTextArea引用)
); 潇洒的写法
wo zai jf...