actionlistener字面上理解就是动作监听器。\x0d\x0a它是一个接口,在实现此接口的类中,你可以给需要关注其动作的组件(如Button)添加监听器(addActionListener(this)),之后在事件处理方法(public void actionPerformed(ActionEvent event){})中,对每个事件进行不同处理。\x0d\x0a给你个例子吧,是我自己写的一个记事本:\x0d\x0aimport java.io.*\x0d\x0aimport java.awt.event.*\x0d\x0aimport javax.swing.*\x0d\x0apublic class MainClass extends JFrame implements ActionListener{\x0d\x0aint width = 500,height = 400\x0d\x0aJPanel panel\x0d\x0aJMenuBar bar\x0d\x0aJMenu fileMenu,editMenu,helpMenu\x0d\x0aJMenuItem 打开O,
新建N,保存S,另存A,
剪切T,复制C,
粘贴P,关于A\x0d\x0aJTextArea textArea = null\x0d\x0aFile tempFile = null\x0d\x0apublic MainClass(){ //构造方法\x0d\x0asetTitle("TextEdit")\x0d\x0asetSize(width,height)\x0d\x0apanel = (JPanel)getContentPane()\x0d\x0abar = new JMenuBar()\x0d\x0afileMenu = new JMenu("文件F")\x0d\x0afileMenu.setMnemonic('F')\x0d\x0aeditMenu = new JMenu("编辑E")\x0d\x0aeditMenu.setMnemonic('E')\x0d\x0ahelpMenu = new JMenu("帮助H")\x0d\x0ahelpMenu.setMnemonic('H')\x0d\x0a打开O = new JMenuItem("打开O")\x0d\x0a打开O.setMnemonic('O')\x0d\x0a新建N = new JMenuItem("新建N")\x0d\x0a新建N.setMnemonic('N')\x0d\x0a保存S = new JMenuItem("保存S")\x0d\x0a保存S.setMnemonic('S')\x0d\x0a另存A = new JMenuItem("另存A")\x0d\x0a另存A.setMnemonic('A')\x0d\x0a剪切T = new JMenuItem("剪切C")\x0d\x0a剪切T.setMnemonic('t')\x0d\x0a复制C = new JMenuItem("复制C")\x0d\x0a复制C.setMnemonic('C')\x0d\x0a粘贴P = new JMenuItem("粘贴P")\x0d\x0a粘贴P.setMnemonic('P')\x0d\x0a关于A = new JMenuItem("关于A")\x0d\x0a关于A.setMnemonic('A')\x0d\x0afileMenu.add(打开O)\x0d\x0afileMenu.add(新建N)\x0d\x0afileMenu.add(保存S)\x0d\x0afileMenu.add(另存A)\x0d\x0abar.add(fileMenu)\x0d\x0aeditMenu.add(剪切T)\x0d\x0aeditMenu.add(复制C)\x0d\x0aeditMenu.add(粘贴P)\x0d\x0abar.add(editMenu)\x0d\x0ahelpMenu.add(关于A)\x0d\x0abar.add(helpMenu)\x0d\x0atextArea = new JTextArea()\x0d\x0apanel.add("North",bar)\x0d\x0apanel.add("Center", textArea)\x0d\x0a打开O.addActionListener(this)\x0d\x0a新建N.addActionListener(this)\x0d\x0a保存S.addActionListener(this)\x0d\x0a另存A.addActionListener(this)\x0d\x0a剪切T.addActionListener(this)\x0d\x0a复制C.addActionListener(this)\x0d\x0a粘贴P.addActionListener(this)\x0d\x0a关于A.addActionListener(this)\x0d\x0asetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)\x0d\x0asetVisible(true)\x0d\x0a}\x0d\x0apublic void actionPerformed(ActionEvent event){ //处理事件\x0d\x0aif(event.getSource() == 打开O){ //处理打开\x0d\x0aJFileChooser jfc = new JFileChooser()\x0d\x0ajfc.showOpenDialog(panel)\x0d\x0atempFile = jfc.getSelectedFile()\x0d\x0aFileReader fis\x0d\x0atry {\x0d\x0afis = new FileReader(tempFile)\x0d\x0atextArea.read(fis,null)\x0d\x0atextArea.setEditable(true)\x0d\x0a}catch(Exception ex){ex.printStackTrace()}\x0d\x0a}\x0d\x0aif(event.getSource() == 新建N){ //处理新建\x0d\x0atextArea.setEditable(true)\x0d\x0atextArea.setText(null)\x0d\x0a}\x0d\x0aif(event.getSource() == 保存S){ //处理保存\x0d\x0aif(tempFile == null){\x0d\x0aJFileChooser jfc = new JFileChooser()\x0d\x0ajfc.showSaveDialog(panel)\x0d\x0atempFile = jfc.getSelectedFile()\x0d\x0atry{\x0d\x0aFileWriter fos = new FileWriter(tempFile)\x0d\x0atextArea.write(fos)\x0d\x0a}catch(Exception ex){ex.printStackTrace()}\x0d\x0a}\x0d\x0aelse{\x0d\x0atry{\x0d\x0aFileWriter fos = new FileWriter(tempFile)\x0d\x0atextArea.write(fos)\x0d\x0a}catch(Exception ex){ex.printStackTrace()}\x0d\x0a}\x0d\x0a}\x0d\x0aif(event.getSource() == 另存A){ //处理另存\x0d\x0aJFileChooser jfc = new JFileChooser()\x0d\x0ajfc.showSaveDialog(panel)\x0d\x0atempFile = jfc.getSelectedFile()\x0d\x0atry{\x0d\x0aFileWriter fos = new FileWriter(tempFile)\x0d\x0atextArea.write(fos)\x0d\x0a}catch(Exception ex){ex.printStackTrace()}\x0d\x0a}\x0d\x0aif(event.getSource() == 剪切T){ //处理剪切\x0d\x0atextArea.cut()\x0d\x0a}\x0d\x0aif(event.getSource() == 复制C){ //处理复制\x0d\x0atextArea.copy()\x0d\x0a}\x0d\x0aif(event.getSource() == 粘贴P){ //处理粘贴\x0d\x0atextArea.paste()\x0d\x0a}\x0d\x0aif(event.getSource() == 关于A){ //处理关于\x0d\x0atextArea.setText("Manifest-Version: 1.0\n" +\x0d\x0a"Created-By: Libra_JL\n" +\x0d\x0a"QQ: 254791521\n")\x0d\x0atextArea.setEditable(false)\x0d\x0apanel.validate()\x0d\x0avalidate()\x0d\x0a}\x0d\x0a}\x0d\x0apublic static void main(String []args){ //主函数\x0d\x0anew MainClass()\x0d\x0a}\x0d\x0a}简单的例子:
import java.applet.*
import java.awt.*
import java.awt.event.*
public class TestPanel extends Applet implements ActionListener,ContainerListener
{
Panel p1,p2,p3
Label prompt1,prompt2,prompt3
Button button1
public void init()
{
setLayout(new BorderLayout())
p1=new Panel()
p1.setBackground(Color.gray)
p2=new Panel()
p2.setBackground(Color.red)
p3=new Panel()
p3.setBackground(Color.cyan)
button1=new Button("input 3panel")
button1.addActionListener(this)
prompt1=new Label("1panel")
prompt2=new Label("2panel")
prompt3=new Label("3panel")
p1.add(prompt1)
p2.add(prompt2)
p3.add(prompt3)
p3.add(button1)
p1.add(p3)
p1.addContainerListener(this)
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
p1.remove(p3)
}
public void componentRemoved(ContainerEvent e)
{
showStatus("移除3panel")
}
public void componentAdded(ContainerEvent e)
{
}
}
可以捕获监听器的内容,并进行处理,参数也可以为空,空代表什么都不执行。
加了个this.addWindowListener()方法以下是代码:
import java.awt. *
import java.awt.event.*
public class AddDemo extends Frame implements ActionListener//实现相应接口
{
Label l1,l2
TextField t1,t2,t3
Button b
public AddDemo()
{
l1=new Label("加数1:",Label.CENTER)
l2=new Label("加数2:",Label.CENTER)
t1=new TextField(6)
t2=new TextField(6)
t3=new TextField(6)
t3.setEditable(false)
b=new Button("求和")
setLayout(new GridLayout(3,2))
add(l1)
add(t1)
add(l2)
add(t2)
add(b)
add(t3)
b.addActionListener(this)
setSize(200,160)
setVisible(true)
setLocation(400,200)
this.addWindowListener(new WindowListener(){
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0)
}
@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
})
}
public static void main (String arg[])
{
new AddDemo()
}
public void actionPerformed(ActionEvent e)
{
t3.setText(""+(Integer.parseInt(t1.getText())+Integer.parseInt(t2.getText())))
}
}
评论列表(0条)