java中JButton的方法doClick()方法怎么结束?

chenqibob 2015-07-31 09:25:36
在java中我想设置每隔1s触发一个JButton的事件
于是找到了http://endual.iteye.com/blog/1143072
这个帖子的中方法 doClick();
查看API :
public void doClick(int pressTime)以编程方式执行“单击”。此方法的效果等同于用户按下并随后释放按钮。按钮在虚拟“按下”状态下停留 pressTime 毫秒的时间。
参数:
pressTime - “按下”按钮的时间,以毫秒为单位
但是触发之后根本停不下来啊,点击主JFrame的关闭按钮也没有用啊!!!!
那应该怎么办呢?
...全文
608 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
飏飏一蝶 2015-07-31
  • 打赏
  • 举报
回复
api又没问题,你咋用的谁知道啊,代码呢,难道你要大家模拟你出问题的代码……停不下来,是你不断的按下了按钮,还是按下了,压根没弹起来,得看你代码。
chenqibob 2015-07-31
  • 打赏
  • 举报
回复
4楼正解,已结贴
chenqibob 2015-07-31
  • 打赏
  • 举报
回复
ls正解,已结贴
迷林 2015-07-31
  • 打赏
  • 举报
回复
楼主好好玩 “我想停下来” 逗死我了
peter_xizi 2015-07-31
  • 打赏
  • 举报
回复
import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class TestJFrame extends JFrame { private JButton doClickButton; private JPanel panel; public static void main(String[] args) { TestJFrame mainFrame = new TestJFrame(); mainFrame.setTitle("doClick"); } public TestJFrame() { super(); this.setVisible(true); this.setBounds(10, 10, 400, 200); this.setResizable(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLayout(null); panel = new JPanel(); panel.setBackground(new Color(135, 206, 250)); panel.setBounds(10, 10, 380, 150); this.add(panel); panel.setLayout(null); doClickButton = new JButton("stop"); doClickButton.setBounds(90, 50, 200, 50); panel.add(doClickButton); doClickButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //System.out.println("我想停下来"); //doClickButton.doClick(10000); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub System.out.println("我想停下来"); try{ Thread.currentThread().sleep(3000L); } catch (InterruptedException e) { System.out.println("doClick interrupted"); }finally{ run(); } } }).start(); } }); } } 代码已测试通过 将事件单独放放一个新的线程中,不要阻塞swing事件分发线程,swing事件模型是单线程的
chenqibob 2015-07-31
  • 打赏
  • 举报
回复
package testDoClick;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainFrame extends JFrame{
	private JButton doClickButton;
	private JPanel panel;
	public static void main(String[] args) {
		MainFrame mainFrame = new MainFrame();
		mainFrame.setTitle("测试doClick()");
	}
	public MainFrame(){
		super();
		this.setVisible(true);
		this.setBounds(10, 10, 400, 200);
		this.setResizable(false);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(null);
		
		panel = new JPanel();
		panel.setBackground(new Color(135, 206, 250));
		panel.setBounds(10, 10, 380, 150);
		this.add(panel);
		panel.setLayout(null);
		
		doClickButton = new JButton("我想停下来");
		doClickButton.setBounds(90, 50, 200, 50);
		panel.add(doClickButton);
		
		doClickButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				System.out.println("我想停下来");
				doClickButton.doClick(1000);
			}
		});
		
		
	}

	

}
chenqibob 2015-07-31
  • 打赏
  • 举报
回复
引用 1 楼 qq118194716 的回复:
api又没问题,你咋用的谁知道啊,代码呢,难道你要大家模拟你出问题的代码……停不下来,是你不断的按下了按钮,还是按下了,压根没弹起来,得看你代码。
不好意思,没有把代码贴上来,确实很难说明问题 我简化了代码,写了一个测试的代码
package testDoClick; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MainFrame extends JFrame{ private JButton doClickButton; private JPanel panel; public static void main(String[] args) { MainFrame mainFrame = new MainFrame(); mainFrame.setTitle("测试doClick()"); } public MainFrame(){ super(); this.setVisible(true); this.setBounds(10, 10, 400, 200); this.setResizable(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLayout(null); panel = new JPanel(); panel.setBackground(new Color(135, 206, 250)); panel.setBounds(10, 10, 380, 150); this.add(panel); panel.setLayout(null); doClickButton = new JButton("我想停下来"); doClickButton.setBounds(90, 50, 200, 50); panel.add(doClickButton); doClickButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.out.println("我想停下来"); doClickButton.doClick(1000); } }); } }

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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