JAVA 事件
在窗体上放置3个文本框,在文本框中输入3原色值,窗体的背景立即改变,我想知道用的是什么事件,不是单击...
在窗体上放置3个文本框,在文本框中输入3原色值,窗体的背景立即改变,我想知道用的是什么事件,不是单击
展开
3个回答
展开全部
import java.awt.*;
import java.awt.event.*;
public class ThreeColor extends Frame implements TextListener{
Label red=new Label("红色:"),green=new Label("绿色:"),blue=new Label("蓝色:");
TextField t1=new TextField(20),t2=new TextField(20),t3=new TextField(20);
public ThreeColor(){
super("三原色");
this.setLayout(new FlowLayout(FlowLayout.CENTER));
add(red);add(t1);add(green);add(t2);add(blue);add(t3);
t1.addTextListener(this);
t2.addTextListener(this);
t3.addTextListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.setSize(270,400);
this.setVisible(true);
}
public void textValueChanged(TextEvent e){
if(e.getSource()==t1||e.getSource()==t2||e.getSource()==t3){
if(!t1.getText().equals("")&&!t1.getText().equals("")&&!t1.getText().equals("")){
try{
int i1,i2,i3;
i1=Integer.parseInt(t1.getText());
i2=Integer.parseInt(t2.getText());
i3=Integer.parseInt(t3.getText());
if(i1<0||i1>255){t1.setText("");return;}
if(i2<0||i2>255){t2.setText("");return;}
if(i3<0||i3>255){t3.setText("");return;}
this.setBackground(new Color(i1,i2,i3));
}catch(NumberFormatException ee){}
}
}
}
public static void main (String[] args) {
new ThreeColor();
}
}
你看是不是和我写的这个功能差不多
用的是TextEvent(文本事件),用来监听TextField和TextArea的
实现TextListener接口,实现textValueChanged(TextEvent)方法
import java.awt.event.*;
public class ThreeColor extends Frame implements TextListener{
Label red=new Label("红色:"),green=new Label("绿色:"),blue=new Label("蓝色:");
TextField t1=new TextField(20),t2=new TextField(20),t3=new TextField(20);
public ThreeColor(){
super("三原色");
this.setLayout(new FlowLayout(FlowLayout.CENTER));
add(red);add(t1);add(green);add(t2);add(blue);add(t3);
t1.addTextListener(this);
t2.addTextListener(this);
t3.addTextListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.setSize(270,400);
this.setVisible(true);
}
public void textValueChanged(TextEvent e){
if(e.getSource()==t1||e.getSource()==t2||e.getSource()==t3){
if(!t1.getText().equals("")&&!t1.getText().equals("")&&!t1.getText().equals("")){
try{
int i1,i2,i3;
i1=Integer.parseInt(t1.getText());
i2=Integer.parseInt(t2.getText());
i3=Integer.parseInt(t3.getText());
if(i1<0||i1>255){t1.setText("");return;}
if(i2<0||i2>255){t2.setText("");return;}
if(i3<0||i3>255){t3.setText("");return;}
this.setBackground(new Color(i1,i2,i3));
}catch(NumberFormatException ee){}
}
}
}
public static void main (String[] args) {
new ThreeColor();
}
}
你看是不是和我写的这个功能差不多
用的是TextEvent(文本事件),用来监听TextField和TextArea的
实现TextListener接口,实现textValueChanged(TextEvent)方法
展开全部
javascript的onblur
当一个对象失去被选种状态时触发onblur事件
Syntax
语法
onblur="所要执行的脚本"
Parameter
参数 Description
注释
SomeJavaScriptCode
所要执行的脚本 Required. Specifies a JavaScript to be executed when the event occurs.
必选项。当事件被触发时所要执行的脚本。
Supported by the following HTML tags:
所支持的HTML标签:
<a>, <acronym>, <address>, <area>, <b>, <bdo>, <big>, <blockquote>, <button>, <caption>, <cite>, <dd>, <del>, <dfn>, <div>, <dl>, <dt>, <em>, <fieldset>, <form>, <frame>, <frameset>, <h1> to <h6>, <hr>, <i>, <iframe>, <img>, <input>, <ins>, <kbd>, <label>, <legend>, <li>, <object>, <ol>, <p>, <pre>, <q>, <samp>, <select>, <small>, <span>, <strong>, <sub>, <sup>, <table>, <tbody>, <td>, <textarea>, <tfoot>, <th>, <thead>, <tr>, <tt>, <ul>, <var>
Supported by the following JavaScript objects:
所支持的JavaScript对象:
button, checkbox, fileUpload, layer, frame, password, radio, reset, submit, text, textarea, window
Example
实例
In this example we will execute some JavaScript code when a user leaves an input field:
在下面的例子中,当用户从文本框离开时将执行一些JavaScript程序:
<html>
<head>
<script type="text/javascript">
function upperCase()
{
var x=document.getElementById("fname").value
document.getElementById("fname").value=x.toUpperCase()
}
</script>
</head>
<body>
输入些字母:
<input type="text" id="code" onblur="upperCase()">
</body>
</html>
The output of the code above will be:
输出结果:
当一个对象失去被选种状态时触发onblur事件
Syntax
语法
onblur="所要执行的脚本"
Parameter
参数 Description
注释
SomeJavaScriptCode
所要执行的脚本 Required. Specifies a JavaScript to be executed when the event occurs.
必选项。当事件被触发时所要执行的脚本。
Supported by the following HTML tags:
所支持的HTML标签:
<a>, <acronym>, <address>, <area>, <b>, <bdo>, <big>, <blockquote>, <button>, <caption>, <cite>, <dd>, <del>, <dfn>, <div>, <dl>, <dt>, <em>, <fieldset>, <form>, <frame>, <frameset>, <h1> to <h6>, <hr>, <i>, <iframe>, <img>, <input>, <ins>, <kbd>, <label>, <legend>, <li>, <object>, <ol>, <p>, <pre>, <q>, <samp>, <select>, <small>, <span>, <strong>, <sub>, <sup>, <table>, <tbody>, <td>, <textarea>, <tfoot>, <th>, <thead>, <tr>, <tt>, <ul>, <var>
Supported by the following JavaScript objects:
所支持的JavaScript对象:
button, checkbox, fileUpload, layer, frame, password, radio, reset, submit, text, textarea, window
Example
实例
In this example we will execute some JavaScript code when a user leaves an input field:
在下面的例子中,当用户从文本框离开时将执行一些JavaScript程序:
<html>
<head>
<script type="text/javascript">
function upperCase()
{
var x=document.getElementById("fname").value
document.getElementById("fname").value=x.toUpperCase()
}
</script>
</head>
<body>
输入些字母:
<input type="text" id="code" onblur="upperCase()">
</body>
</html>
The output of the code above will be:
输出结果:
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
实现TextListener这个接口吧,实现public void textValueChanged(TextEvent e) {}这个方法即可
这个接口会侦听文本框值发生改变,然后调用textValueChanged方法
这个接口会侦听文本框值发生改变,然后调用textValueChanged方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询