actionlistener是什么 addeventlistener和onclick的區(qū)別
夕逆IT
- 前端設(shè)計(jì)
- 2023-08-13 11:14:36
- 119

大家好,關(guān)于actionlistener是什么很多朋友都還不太明白,今天小編就來為大家分享關(guān)于addeventlistener和onclick的區(qū)別的知識(shí),希望對各位有...
大家好,關(guān)于actionlistener是什么很多朋友都還不太明白,今天小編就來為大家分享關(guān)于addeventlistener和onclick的區(qū)別的知識(shí),希望對各位有所幫助!
簡述什么是多態(tài)
多態(tài)指為不同數(shù)據(jù)類型的實(shí)體提供統(tǒng)一的接口。多態(tài)類型可以將自身所支持的操作套用到其它類型的值上。
計(jì)算機(jī)程序運(yùn)行時(shí),相同的消息可能會(huì)送給多個(gè)不同的類別之對象,而系統(tǒng)可依據(jù)對象所屬類別,引發(fā)對應(yīng)類別的方法,而有不同的行為。簡單來說,所謂多態(tài)意指相同的消息給予不同的對象會(huì)引發(fā)不同的動(dòng)作。
idea判斷一個(gè)數(shù)是否為素?cái)?shù)
判斷一個(gè)數(shù)是否為素?cái)?shù)的方法如下:1.若該數(shù)小于2,則不是素?cái)?shù)。2.若該數(shù)等于2,則是素?cái)?shù)。3.若該數(shù)能被2整除,則不是素?cái)?shù)。4.從3開始,依次判斷該數(shù)能否被從3到sqrt(n)之間的奇數(shù)整除。4.1若能被某個(gè)數(shù)整除,則不是素?cái)?shù)。4.2若不能被任何數(shù)整除,則是素?cái)?shù)。
如何eclipse編寫一個(gè)簡單實(shí)用的登陸界面
//服務(wù)器端代碼
importjava.awt.FlowLayout;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.net.ServerSocket;
importjava.net.Socket;
importjava.util.ArrayList;
importjava.util.Collection;
importjava.util.Iterator;
importjavax.swing.JFrame;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
publicclassQLServerextendsJFrame{
publicJTextAreajtextarea=null;
publicvoidlanuchFrame(Stringstr){
this.setName(str);
init();
}
privatevoidinit(){
setLayout(newFlowLayout());
jtextarea=newJTextArea(20,17);
jtextarea.setLineWrap(true);
jtextarea.setEditable(false);
this.getContentPane().add(newJScrollPane(jtextarea));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,400);
setLocationRelativeTo(null);
setResizable(false);
}
ServerSocketserver=null;
CollectioncClients=newArrayList<ClientConn>();//加個(gè)泛型
publicvoidstartServer()throwsIOException{
while(true){
Sockets=server.accept();
cClients.add(newClientConn(s));
jtextarea.append("newclientlogin"+s.getInetAddress()+":"+s.getPort()+"\n");
}
}
publicQLServer(intport,Stringstr)throwsIOException{
server=newServerSocket(port);
lanuchFrame(str);
}
classClientConnimplementsRunnable
{
Sockets=null;
publicClientConn(Sockets)
{
this.s=s;
(newThread(this)).start();
}
publicvoidsend(Stringstr)throwsIOException
{
DataOutputStreamdos=newDataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
publicvoiddispose()//客戶端下線
{
try{
if(s!=null)s.close();
cClients.remove(this);
jtextarea.append("Aclientout!\n");
jtextarea.append("clientcount:"+cClients.size()+"\n\n");
}
catch(Exceptione)
{
e.printStackTrace();
}
}
publicvoidrun()
{
try{
DataInputStreamdis=newDataInputStream(s.getInputStream());
Stringstr=dis.readUTF();
while(str!=null&&str.length()!=0)
{
System.out.println(str);
for(Iteratorit=cClients.iterator();it.hasNext();)
{
ClientConncc=(ClientConn)it.next();
if(this!=cc)
{
cc.send(str+""+s.getInetAddress().getHostName());
}
}
str=dis.readUTF();//少了這句話會(huì)無限輸出
//send(str);
}
this.dispose();
}
catch(Exceptione)
{
this.dispose();
}
}
}
publicstaticvoidmain(String[]args){
try{
QLServerqlserver=newQLServer(8888,"QLServer");
qlserver.startServer();
}catch(IOExceptione){
e.printStackTrace();
}
}
}importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.BufferedReader;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.net.InetAddress;
importjava.net.Socket;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
//客戶端代碼
publicclassQLClientextendsJFrameimplementsActionListener{
publicJTextAreajtextarea1=null;
publicJTextAreajtextarea2=null;
publicJButtonbutton=null;
Sockets=null;
publicvoidlaunchFrame(Stringstr){
this.setName(str);
init();
}
publicQLClient(Stringstr)throwsIOException{
launchFrame(str);
s=newSocket("127.0.0.1",8888);//哪臺(tái)電腦做服務(wù)器,IP地址改成那臺(tái)機(jī)子的IP
(newThread(newServeConn())).start();
}
privatevoidinit(){
setLayout(newFlowLayout());
jtextarea1=newJTextArea(17,16);
jtextarea2=newJTextArea(4,16);
jtextarea1.setLineWrap(true);
jtextarea1.setEditable(false);
jtextarea2.setLineWrap(true);
button=newJButton("發(fā)送");
button.addActionListener(this);//綁定button事件
this.getContentPane().add(newJScrollPane(jtextarea1));
this.getContentPane().add(newJScrollPane(jtextarea2));
add(button);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,470);
setLocationRelativeTo(null);
setResizable(false);
}
publicvoidsend(Stringstr)throwsIOException{
DataOutputStreamdos=newDataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==button){
StringsendStr=jtextarea2.getText();
if(sendStr.trim().length()==0){
return;
}
try{
this.send(sendStr);
jtextarea2.setText("");
InetAddressa;
a=InetAddress.getLocalHost();
Stringhostname=a.getHostName();
jtextarea1.append(sendStr+"("+hostname+")"+"\n");
}catch(IOExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
}
}
classServeConnimplementsRunnable{
publicvoidrun(){
if(s==null)return;
try{
DataInputStreamdis=newDataInputStream(s.getInputStream());
Stringstr=dis.readUTF();
while(str!=null&&str.length()!=0)
{
//System.out.println(str);
QLClient.this.jtextarea1.append(str+"\n");//內(nèi)部類用外類中的變量或方法加外類名
str=dis.readUTF();
}
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}
//main主函數(shù)入口
publicstaticvoidmain(String[]args)throwsIOException{
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
QLClientqlclient=newQLClient("QLClient");
Stringstr=br.readLine();
while(str!=null&&str.length()!=0){
qlclient.send(str);
str=br.readLine();//防止死循環(huán)
}
qlclient.s.close();
}
}
java中監(jiān)聽事件怎么設(shè)置
一.使用內(nèi)部類,一個(gè)個(gè)設(shè)置Button然后創(chuàng)建一個(gè)內(nèi)部類,用ActionPerformed來實(shí)現(xiàn)按鈕事件內(nèi)容;
二.使用lambda表達(dá)式,但是這個(gè)方法比較繁瑣,需要?jiǎng)?chuàng)建一個(gè)內(nèi)部類,其實(shí)有更簡單的方法,比如使用lambda表達(dá)式,而不需要?jiǎng)?chuàng)建內(nèi)部類,我們可以把創(chuàng)建按鈕和響應(yīng)的部分放到一個(gè)方法里面;
三.使用匿名對象,當(dāng)然使用lambda表達(dá)式肯定不是非用不可的,以前的方法是使用匿名對象,也是可以的;
四.自身框架做事件源容器,其實(shí)可以直接用JFrame本身做事件源容器,使ButtonFrame支持ActionListener接口,需要把按鈕定義在屬性中,然后在構(gòu)造函數(shù)中添加的監(jiān)聽器中直接使用this像這樣:greenButton.addActionListener(this);然后在自己類中實(shí)現(xiàn)ActionPerformed方法,使用Objectsource=e.getSource();獲取到事件的更新源,然后用判斷語句來判斷是哪個(gè)按鈕的事件依次執(zhí)行事件操作;
五.使用EventHandler指定事件監(jiān)聽器,使用這樣的方法EventHandler.create(ActionListener.class,frame,"loadData")創(chuàng)造一個(gè)ActionListener,這種方法用lambda表達(dá)式就等同于event->frame.localData();
關(guān)于actionlistener是什么和addeventlistener和onclick的區(qū)別的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
本文鏈接:http:///qianduan/2455.html