JButton jb=new JButton("click");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"ok!");
}
});
然后如果需要调用这个事件,则如下调用即可
jb.doClick();
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"ok!");
}
});
然后如果需要调用这个事件,则如下调用即可
jb.doClick();
解决方案 »
- socket问题啊!
- Iterator接口的疑惑
- 使用JNDI访问DOMINO LDAP服务器数据丢失的问题
- 关于JUNIT的问题(快过年了,顺便散把分,祝大家新年快乐)
- java 中如何调用 exe、bat等文件
- 请问:socket编程中如何发送与接受指定长度的字节流 ?
- 窗体关闭按钮不显示
- 我在jbuilder中先添加ButtonGroup控件,然后添加3个JRadioButton组件,本这3个组件是排斥的(即一次只能选一个),可运行起来不行,3个能
- 在JDesktopPane中加入ScrollBar!
- Java初学者问题
- 高分求解:如何用Java 实现 Excel 表达式的解析?
- swing的问题,谢谢大家
忘了说了,具体说吧。
目前的项目涉及声音编程。所以我用一实现了runnable的applet来进行录音(也是单独的线程)。
applet上全部时awt组件。
我需要在录音线程运行1段时间后停止。在applet的run()中,调用applet上的“stop”按键。
int stopTime=0;
public void run() {
try {
while(true) {
……………………//你的操作
Thread.sleep(100);
if((stopTime++)==1000) {
break;
return;
}
}
}catch(Exception e){System.out.println(e.toString());return;}
}
//如果点击了录音按键,才运行线程
if (brecord) {
//只显示60秒录音
if (ishowtime < 60) {
if (ishowtime >= 54) {
tfsoundlength.setForeground(Color.RED);
}
tfsoundlength.setText(String.valueOf(++ishowtime));
} //如果到达了60秒,自动停止录音
if ( ishowtime == 60 )
{
//希望在此处调用stop的actionPerformed(); }
}
else {
tfsoundlength.setText(String.valueOf(ishowtime));
}
} /**
* 运行线程
*/
public void run() {
Thread me = Thread.currentThread();
while (timer == me) {
try { Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
}
repaint();
}
}停止播放按键部分代码
void btnstop_actionPerformed(ActionEvent e) {
btnrecord.setEnabled(true);
btnplay.setEnabled(true);
btnsend.setEnabled(true);
btnreply.setEnabled(true);
brecord = false;
dorecord.actionPerformed(ButtonType.BTN_STOP, e, sessionid);
}
{
stopButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
processActionEvent(event);
}
});
}
void processActionEvent(ActionEvent event)
{
//here you do something to process the action event
//you get from the above listener
//****************************
}
?? is that processActionEvent() method what you want? You can customize the
method in any way you like. And the parameter of ActionEvent event is what you are looking for, right?
Come to me if you don't understand.
then modify the code given by beyond_xiruo:
基本上就是这样
boolean isActive = true;
int stopTimes = 0;
public void run()
{
try {
while(isActive) {
……………………//你的操作
Thread.sleep(100);
if((stopTimes++)==10)
{
stopButton.doClick();
}
}
}catch(Exception e){System.out.println(e.toString());return;}
}
其实如果不传参数,可以调用actionperformed( null );
只是我想构造ActionEvent!