当前位置: 首页 > >

ActionListener


1.按钮的事件通过监听按钮接口,是不是,监听就是解释你的动作呢?按下,释放,按下不放代表什么“意义”。
2.事件来源:用Java语言来解释,就是可以把用户操作(鼠标,键盘,关闭窗口)变成事件的对象。
3.事件:用对象来表示,在java.awt.event会有很多事件的类Mouseevent,keyevent,windowsevent.
4.监听:当事件来源发出动作变为事件对象,程序大多时候都是在处理事情对象,而不是当事件的来源。我们很在乎这个感应装置,我们必须有一个监听就是模仿一个人这个事件的动作行为。
5.监听接口:
从事件源??-》事件??》接口(监听)??-》接口方法(处理监听)
6.监听和事件源如何通信呢?
我们在事件源的类中使用addActionListener注册,成为一个Actionlistener接口,它的功能是:当发生事件后,可以监听到这个事件,同时就会执行Actionlistener接口它唯一的方法actionperformed来完成监听后需要执行的操作.
这就相当于把监听给予一个类,让它自己具有监听的功能。就类似Dog来实现Pet接口,那Dog具有了Pet的方法,Dog is a Pet .


public class ActionEvent
extends AWTEvent


指示发生了组件定义的动作的语义事件。当特定于组件的动作(比如被按下)发生时,由组件(比如 Button)生成此高级别事件。事件被传递给每一个ActionListener 对象,这些对象是使用组件的addActionListener 方法注册的,用以接收这类事件。


public interface ActionListener
extends EventListener


用于接收操作事件的侦听器接口。
方法只有一个:
actionPerformed
void actionPerformed(ActionEvent e)
发生操作时调用。
需要明白一点,接口是一种你必须实现的类,当然如果你在一个类中实现了接口,那你可以随意用类来调用接口的方法。


例子:


import javax.swing.*;
import java.awt.event.*;
public class SimpleGuil implements ActionListener{
JButton button;
public static void main(String[] args){
SimpleGuil gui=new SimpleGuil();
gui.go();}
public void go(){
JFrame frame=new JFrame();
button=new JButton(“click me”);
button.addActionListener(this);
frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event){
button.setText(“i have been clicked”);
}
}


8.还有很多event,listener,他们都有不同的功能。
鼠标按下,会产生一些static final值,代表不同的鼠标行为
在鼠标按下的时候,通过构造函数生成一个对象,我们称为事件
MouseEvent
public MouseEvent(Component source,
int id,
long when,
int modifiers,
int x,
int y,
int clickCount,
boolean popupTrigger,
int button)
构造一个具有指定源组件、类型、修饰符、坐标和单击计数的 MouseEvent 对象。


Mouselisener是一个事件发生后,会调用的接口
public interface MouseListener
extends EventListener
用于接收组件上“感兴趣”的鼠标事件(按下、释放、单击、进入或离开)的侦听器接口。一个事件,总是会需要调用监听接口。


方法有很多:mousepressed.
9.在JFrame中新建的一个JButton,然后添加一个接口,JFrame是一个什么对象呢?
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
javax.swing.JFrame
10.在JAVA的文档中,我们可以查询到相关的用法;地址如下
http://docs.oracle.com/javase/tutorial/index.html
11.一个综合例子
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyButton implements ActionListener{
JTextArea text;
public static void main(String[] args){
MyButton mb=new MyButton();
mb.go();
}
public void go(){
JFrame frame=new JFrame();


JPanel panel=new JPanel();
panel.setBackground(Color.darkGray);
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));


JButton button1=new JButton(“click me”);
//JButton button2=new JButton(“click me”);
JButton button3=new JButton(“click meclick me”);
JButton button4=new JButton(“click me”);
JButton button5=new JButton(“click me”);
button3.addActionListener(this);


JTextField field=new JTextField(“ZHOUXIAOMU”);


text=new JTextArea(10,20);
JScrollPane scroller=new JScrollPane(text);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
text.setText(“not all who are lost are wondering”);


frame.getContentPane().add(BorderLayout.EAST, panel);
frame.getContentPane().add(BorderLayout.NORTH,field);
panel.add(button3);
panel.add(button4);
panel.add(button5);
panel.add(scroller);


frame.setSize(300,300);
frame.setVisible(true);

}
public void actionPerformed(ActionEvent ev){
text.append(“
button clicked”);


}
}


12.文件的输入输出,缓冲区的使用,swing的使用
3个java:QuixCardBuilder.java QuixCardPlayer.java QuixCard.java
package first;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class QuixCardBuilder {
private JTextArea question;
private JTextArea answer;
private ArrayList cardList;
private JFrame frame;
public static void main(String[] args){
QuixCardBuilder builder=new QuixCardBuilder();
builder.go();
}


public void go(){
//gui
frame=new JFrame(“Quix Card Builder”);
JPanel mainPanel=new JPanel();
Font bigFont=new Font(“sanserif”,Font.BOLD,24);
question=new JTextArea(6,20);
question.setLineWrap(true);
question.setWrapStyleWord(true);
question.setFont(bigFont);


JScrollPane qScroller=new JScrollPane(question);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);


answer=new JTextArea(6,20);
answer.setLineWrap(true);
answer.setWrapStyleWord(true);
answer.setFont(bigFont);


JScrollPane aScroller=new JScrollPane(answer);
aScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);


JButton nextButton=new JButton(“Next Card”);


cardList=new ArrayList();


JLabel qLabel=new JLabel(“Question:”);
JLabel aLabel=new JLabel(“Answer:”);


mainPanel.add(qLabel);
mainPanel.add(aLabel);
mainPanel.add(qScroller);
mainPanel.add(aScroller);
mainPanel.add(nextButton);
nextButton.addActionListener(new NextCardListener());
JMenuBar menuBar=new JMenuBar();
JMenu fileMenu=new JMenu(“File”);
JMenuItem newMenuItem=new JMenuItem(“New”);
JMenuItem saveMenuItem=new JMenuItem(“Save”);
newMenuItem.addActionListener(new NewMenuListener());


saveMenuItem.addActionListener(new SaveMenuListener());
fileMenu.add(newMenuItem);
fileMenu.add(saveMenuItem);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.getContentPane().add(BorderLayout.CENTER,mainPanel);
frame.setSize(500,600);
frame.setVisible(true);
}
public class NextCardListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
QuixCard card=new QuixCard(question.getText(),answer.getText());
cardList.add(card);
clearCard();
}
}


public class NewMenuListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
cardList.clear();
clearCard();
}
}
public class SaveMenuListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
QuixCard card=new QuixCard(question.getText(),answer.getText());
cardList.add(card);


JFileChooser fileSave=new JFileChooser();
fileSave.showSaveDialog(frame);
saveFile(fileSave.getSelectedFile());
}
}
public void clearCard(){
question.setText(“”);
answer.setText(“”);
question.requestFocus();
}


public void saveFile(File file){
try{
BufferedWriter writer=new BufferedWriter(new FileWriter(file));


for(QuixCard card:cardList){
writer.write(card.getQuestion()+”/”);
writer.write(card.getAnswer()+”
”);
}
writer.close();
}catch(IOException ex){
System.out.println(“couldn’t write the cardList out”);
ex.printStackTrace();
}
}


}



package first;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class QuixCardPlayer {
private JTextArea display;
private ArrayList cardList;
private QuixCard currentCard;
private JFrame frame;
private JButton nextButton;
private boolean isShowAnswer;
private int currentCardIndex;


public static void main(String[] args){
QuixCardPlayer reader=new QuixCardPlayer();
reader.go();
}
public void go(){
frame=new JFrame(“Quix Card Player”);
JPanel mainPanel=new JPanel();
Font bigFont=new Font(“sanserif”,Font.BOLD,24);


display=new JTextArea(10,20);
display.setFont(bigFont);


display.setLineWrap(true);
display.setEditable(true);


JScrollPane qScroller=new JScrollPane(display);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
nextButton=new JButton(“show question”);
mainPanel.add(qScroller);
mainPanel.add(nextButton);
nextButton.addActionListener(new NextCardListener());


JMenuBar menuBar=new JMenuBar();
JMenu fileMenu=new JMenu("File");
JMenuItem loadMenuItem=new JMenuItem("Load card set");
loadMenuItem.addActionListener(new OpenMenuListener());
fileMenu.add(loadMenuItem);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.getContentPane().add(BorderLayout.CENTER,mainPanel);
frame.setSize(640,500);
frame.setVisible(true);

}//go close


public class NextCardListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
if(isShowAnswer){
display.setText(currentCard.getAnswer());
nextButton.setText(“Next Card”);
isShowAnswer=false;
}else{


if(currentCardIndex showNextCard();
}else{
display.setText("that was last card");
nextButton.setEnabled(false);
}

}
}
}//Nextclass close
public class OpenMenuListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
JFileChooser fileOpen=new JFileChooser();
fileOpen.showOpenDialog(frame);
loadFile(fileOpen.getSelectedFile());
}
}


private void loadFile(File file){
cardList=new ArrayList();
try{
BufferedReader reader=new BufferedReader(new FileReader(file));
String line=null;
while((line=reader.readLine())!=null)
{
makeCard(line);
}
reader.close();
}catch(Exception ex){
System.out.println(“coul’t read the card file”);
ex.printStackTrace();
}
}//close


private void makeCard(String lineToParse){
String[] result=lineToParse.split(“/”);
QuixCard card=new QuixCard(result[0],result[1]);
cardList.add(card);
System.out.println(“made a card”);
}
private void showNextCard()
{
currentCard=cardList.get(currentCardIndex);
currentCardIndex++;
display.setText(currentCard.getQuestion());
nextButton.setText(“show answer”);
isShowAnswer=true;
}


}//close



package first;
public class QuixCard {
public String question;
public String answer;
public QuixCard(String a,String b){
question=a;
answer=b;
}
public String getQuestion(){
return question;
}
public String getAnswer(){
return answer;
}
}



友情链接: year2525网 工作范文网 QS-ISP 138资料网 528200 工作范文网 baothai 表格模版