A. java中回车键监听按钮事件
document.onkeydown=function(){
if (event.keyCode == 13){ //盯慧回凯凯答车
}
if (event.keyCode == 32){ //孙哗空格
}
}
B. java 鼠标左键 加 ctrl 选中是什么监听事件
事件源。
java的事件监听机制包含三个组件事件源事件监听器事件对象,当事件源上发生操作,时它将会调用事件监听器的一个方法,并且会传递一个事件对象过来,事件监听器由开发人员编写,开发人员在事件监听器中,可以拿到事件源,从而对事件源上的操作进行处理。
C. 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();
}
}
D. Java按钮监听
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.math.BigDecimal;
importjava.util.Scanner;
importjavax.swing.JFrame;
importjava.awt.Button;
importjava.awt.Label;
importjava.awt.TextField;
importjava.awt.Frame;
importjava.awt.Panel;
importjava.awt.Color;
importjava.awt.*;
publicclassPanelTest{
publicstaticvoidmain(Stringargs[]){
/*Scannersc=newScanner(System.in);doublepi=3.14,s;doubler;r=sc.nextDouble();s=pi*r*r;System.out.println("s等于"+s);*/
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
CricleFrameframe=newCricleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
classCricleFrameextendsJFrame{
Panelp=newPanel();
TextFieldt=newTextField();
Buttonb=newButton("确定");
Labela=newLabel("请在此输入半径");
TextFieldresult=newTextField();
publicCricleFrame(){
add(a);
add(t);
add(b);
add(result);
add(p);
setVisible(true);
p.setBackground(Color.black);
a.setBackground(Color.yellow);
t.setBackground(Color.white);
result.setBackground(Color.white);
b.setBackground(Color.cyan);
setSize(300,300);
setTitle("圆的面积");
a.setBounds(105,45,90,25);
t.setBounds(100,80,100,25);
result.setBounds(100,180,100,25);
b.setBounds(111,120,80,40);
b.addActionListener(newActionListener(){//按钮点击事件监听
publicvoidactionPerformed(ActionEventevent){
Doubler=0.0;
try{
r=Double.parseDouble(t.getText());
}catch(Exceptione){
System.out.println(e.getMessage());
}
BigDecimaltmp=newBigDecimal(r*r*Math.PI);
Doublearea=tmp.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();//保留2位小数
result.setText(""+area);
}
});
}
}
在你的基础上改了一下,界面什么的没有改
E. java中怎样为多个按钮设置监听
for(inti=0;i<9;i++){
JButtonbtn=null;
if(i%2==0){
btn=newJButton("+");
f.add(btn);
}
else{
btn=newJButton("-");
f.add(btn);
}
btn.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
if(e.getActionCommand.equals("+")){
btn.setText("-");
}
else{
btn.setText("+");
}
}
});
}
F. java怎么实现对另一个类中的按钮监听
你只有一个窗体,
你想要的就是在一个“窗体类”中,监听一个“监听事件类”的意思吗
只要把监听事件类写在窗体类中,不就行了吗?
class GameState_Begin extends Frame
{
//构造器
public GameState_Begin()
{
Button btn1=new Button("开始游戏");
super.add(btn1);
btn1.addActionListener(new ButtonListen());
}
//●●●●●内部类:按钮事件监听
class ButtonListen implements ActionListener
{
public void actionPerformed(ActionEvent arg0) {
//改变panel布局的代码
}
}
}