A. java按钮的点击事件是什么意思
java中,按钮点击时会触发你的点击函数onclick,你在点击的函数中写你的代码就可以了,比如你说的跳转另一个面板。
B. java onclick事件
每一个div加class属性
比如<div class="harvest">
$('.harvest').click(function(){
$(this).???//开始你的逻辑
});
C. java keypressed长按的时候 会有一小段的延迟
我猜你的意思是想按下某个按钮的时候程序就立即响应为长按的状态,你可以先设定一个boolean来作为标志(按键后生效),然后起一个新线程来循环触发你的业务逻辑。键盘长按事件也就是以一定的频率触发持续触发的keypressed事件,这样就能避免键盘从按下到识别为长按的空白区
D. JAVA如何监测按住按钮的事件
代码如下:
importjava.awt.EventQueue;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.time.Duration;
importjava.time.Instant;
importjavax.swing.JButton;
importjavax.swing.JFrame;
{
=1L;
publicInstantstart;
publicInstantend;
publicMyFrame()
{
setSize(300,200);
setTitle("Test");
setLocationRelativeTo(null);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButtonbtn=newJButton("BUTTON");
btn.setBounds(100,60,100,40);
btn.addMouseListener(newMouseAdapter()
{
@Override
publicvoidmousePressed(MouseEvente)
{
start=Instant.now();
}
@Override
publicvoidmouseReleased(MouseEvente)
{
end=Instant.now();
System.out.println("总用时:"+Duration.between(start,end).toMillis()+"毫秒");
}
});
add(btn);
}
publicstaticvoidmain(String[]args)
{
EventQueue.invokeLater(()->
{
try
{
MyFrameframe=newMyFrame();
frame.setVisible(true);
}
catch(Exceptione)
{
e.printStackTrace();
}
});
}
}
按下按钮,再松开鼠标之后,总用时将被打印在控制台上,单位是毫秒,你可以设置其他单位
E. java键盘事件,如何监听连续按两次按键
importjava.awt.BorderLayout;
importjava.awt.Dimension;
importjava.awt.event.*;
importjavax.swing.*;
,ActionListener{
JTextAreadisplayArea;
JTextFieldtypingArea;
publicstaticvoidmain(String[]args){
javax.swing.SwingUtilities.invokeLater(newRunnable(){
publicvoidrun(){
createAndShowGUI();
}
});
}
/**
*CreatetheGUIandshowit.Forthreadsafety,thismethodshouldbe
*invokedfromtheevent-dispatchingthread.
*/
(){
//Createandsetupthewindow.
KeyEventDemoframe=newKeyEventDemo("KeyEventDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Setupthecontentpane.
frame.addComponentsToPane();
//Displaythewindow.
frame.pack();
frame.setVisible(true);
}
(){
JButtonbutton=newJButton("Clear");
button.addActionListener(this);
typingArea=newJTextField(20);
typingArea.addKeyListener(this);
displayArea=newJTextArea();
displayArea.setEditable(false);
JScrollPanescrollPane=newJScrollPane(displayArea);
scrollPane.setPreferredSize(newDimension(375,125));
getContentPane().add(typingArea,BorderLayout.PAGE_START);
getContentPane().add(scrollPane,BorderLayout.CENTER);
getContentPane().add(button,BorderLayout.PAGE_END);
}
publicKeyEventDemo(Stringname){
super(name);
}
longtime=System.currentTimeMillis();
longtimeInterval=300;
/**.*/
publicvoidkeyTyped(KeyEvente){
//displayInfo(e,"KEYTYPED:");
}
/**.*/
publicvoidkeyPressed(KeyEvente){
System.out.println(System.currentTimeMillis()-time);
if(System.currentTimeMillis()-time<timeInterval)
displayArea.setText("Doubleclicked");
else
displayArea.setText("Singleclicked");
time=System.currentTimeMillis();
//displayInfo(e,"KEYPRESSED:");
}
/**.*/
publicvoidkeyReleased(KeyEvente){
//displayInfo(e,"KEYRELEASED:");
}
/**Handlethebuttonclick.*/
publicvoidactionPerformed(ActionEvente){
//Clearthetextcomponents.
displayArea.setText("");
typingArea.setText("");
//Returnthefocustothetypingarea.
typingArea.requestFocusInWindow();
}
}
F. java按钮被点击事件
添加按钮监听。回见内容写成方法,然后调用即可。大概像下面这样
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
publicclassTest{
publicstaticvoidmain(String[]args){
=newJButton("A");
JButtonB=newJButton("B");
JButtonC=newJButton("C");
A.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
methodA();
}
});
B.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
methodB();
}
});
C.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
methodA();
methodB();
}
});
}
publicstaticvoidmethodA(){
//执行A方法
}
publicstaticvoidmethodB(){
//执行B方法
}
}
G. java 有按住鼠标左键拖拽的事件吗
public class Test implements MouseMotionListener{
public void mouseDragged(MouseEvent e){ //鼠标拖拽事件
//
}
public void mouseMoved(MouseEvent e){ //鼠标移动事件
//
}
}
H. java怎么给按钮添加事件
解决方法:
(1)public void actionPerformed(ActionEvent e) {
}
这么一个方法,然后再在构造方法里面添加注册内事件容,button.addActionListener(this);
(2)在res/layout中的activity_main.xml添加一个按钮,命名为button1。
(3)在MainActivity.Java中添加按钮事件绑定:
I. java模拟长按
我记得没有呢, 按键按下的时候记录下时间,弹起的时候用当前时间跟记录时间比较,一定范围内认为是长按,执行长按事件方法
J. 怎么样让JAVA 设置一秒钟触发一个事件
你的代码我不看了..长......
具体参考java.util.Timer类和java.util.TimerTask
TimerTask是一个虚类要实现里面的run方法,run方法就是你时间到了以后要执行的方法.
送你一个蒸鸡蛋的例子
import java.util.Timer;
import java.util.TimerTask;
public class EggTimer {
private final Timer timer = new Timer();
private final int minutes;
public EggTimer(int minutes) {
this.minutes = minutes;
}
public void start() {
timer.schele(new TimerTask() {
public void run() {
playSound();
timer.cancel();
}
private void playSound() {
System.out.println("Your egg is ready!");
// Start a new thread to play a sound...
}
}, minutes * 60 * 1000);
}
public static void main(String[] args) {
EggTimer eggTimer = new EggTimer(2);
eggTimer.start();
}
}
可以帖出去看看的...