1. java中關於addActionListener的問題
這里的this 指的是你的方法或成員或操作火災的這個類,this在這里的作用是說明,你必須在該類裡面來實現ActionListener裡面的actionPerformed方法,其實btnUp.addActionListener(Object t);這里的參數的意思是,這個t是哪個類的對象,那麼那個類就負責來實現介面的方法,不知道你理解了沒有。
import java.awt.*;
import java.awt.event.*;
public class Bnbn //implements ActionListener
{
Frame frmFrame;
Button btnUp, btnDown;
public Bnbn()
{
frmFrame = new Frame("按鈕應用");
frmFrame.setLayout(new GridLayout(2,1));
ss s=new ss();//產生內部類對象作監視器
//產生按鈕一
btnUp = new Button("上");
btnUp.setActionCommand("btnUp");
btnUp.addActionListener(s);
//產生按鈕二
btnDown = new Button("下");
btnDown.setActionCommand("btnDown");
btnDown.addActionListener(s);
frmFrame.add(btnUp);
frmFrame.add(btnDown);
frmFrame.pack();
frmFrame.setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
class ss implements ActionListener //創建內部類作監視器
{
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if ( cmd.equals("btnDown") )
{
if (btnUp.getBackground()==Color.BLUE)
{
btnUp.setBackground(Color.red);
}
else
{
btnUp.setBackground(Color.blue);
}
}
else
{
if (btnDown.getBackground()==Color.BLUE)
{
btnDown.setBackground(Color.red);
}
else
{
btnDown.setBackground(Color.blue);
}
}
}
}
public static void main(String[] args)
{
new Bnbn();
}
}
2. java中actionPerformed是什麼意思啊 怎麼用啊
這個是ActionListener這個介面裡面的方法
用於接收操作事件的偵聽器介面。對處理操作事件感興趣的類可以實現此介面,而使用該類創建的對象可使用組件的 addActionListener 方法向該組件注冊。在發生操作事件時,調用該對象的 actionPerformed 方法。
3. Java中的actionlistener是什麼意思,有什麼作用,詳細點為好!
actionlistener字面上理解就是動作監聽器。
它是一個介面,在實現此介面的類中,你可以給需要關注其動作的組件(如Button)添加監聽器(addActionListener(this);),之後在事件處理方法(public void actionPerformed(ActionEvent event){})中,對每個事件進行不同處理。
給你個例子吧,是我自己寫的一個記事本:
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
public class MainClass extends JFrame implements ActionListener{
int width = 500,height = 400;
JPanel panel;
JMenuBar bar;
JMenu fileMenu,editMenu,helpMenu;
JMenuItem 打開O,新建N,保存S,另存A,剪切T,復制C,粘貼P,關於A;
JTextArea textArea = null;
File tempFile = null;
public MainClass(){ //構造方法
setTitle("TextEdit");
setSize(width,height);
panel = (JPanel)getContentPane();
bar = new JMenuBar();
fileMenu = new JMenu("文件F");
fileMenu.setMnemonic('F');
editMenu = new JMenu("編輯E");
editMenu.setMnemonic('E');
helpMenu = new JMenu("幫助H");
helpMenu.setMnemonic('H');
打開O = new JMenuItem("打開O");
打開O.setMnemonic('O');
新建N = new JMenuItem("新建N");
新建N.setMnemonic('N');
保存S = new JMenuItem("保存S");
保存S.setMnemonic('S');
另存A = new JMenuItem("另存A");
另存A.setMnemonic('A');
剪切T = new JMenuItem("剪切C");
剪切T.setMnemonic('t');
復制C = new JMenuItem("復制C");
復制C.setMnemonic('C');
粘貼P = new JMenuItem("粘貼P");
粘貼P.setMnemonic('P');
關於A = new JMenuItem("關於A");
關於A.setMnemonic('A');
fileMenu.add(打開O);
fileMenu.add(新建N);
fileMenu.add(保存S);
fileMenu.add(另存A);
bar.add(fileMenu);
editMenu.add(剪切T);
editMenu.add(復制C);
editMenu.add(粘貼P);
bar.add(editMenu);
helpMenu.add(關於A);
bar.add(helpMenu);
textArea = new JTextArea();
panel.add("North",bar);
panel.add("Center", textArea);
打開O.addActionListener(this);
新建N.addActionListener(this);
保存S.addActionListener(this);
另存A.addActionListener(this);
剪切T.addActionListener(this);
復制C.addActionListener(this);
粘貼P.addActionListener(this);
關於A.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent event){ //處理事件
if(event.getSource() == 打開O){ //處理打開
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(panel);
tempFile = jfc.getSelectedFile();
FileReader fis;
try {
fis = new FileReader(tempFile);
textArea.read(fis,null);
textArea.setEditable(true);
}catch(Exception ex){ex.printStackTrace();}
}
if(event.getSource() == 新建N){ //處理新建
textArea.setEditable(true);
textArea.setText(null);
}
if(event.getSource() == 保存S){ //處理保存
if(tempFile == null){
JFileChooser jfc = new JFileChooser();
jfc.showSaveDialog(panel);
tempFile = jfc.getSelectedFile();
try{
FileWriter fos = new FileWriter(tempFile);
textArea.write(fos);
}catch(Exception ex){ex.printStackTrace();}
}
else{
try{
FileWriter fos = new FileWriter(tempFile);
textArea.write(fos);
}catch(Exception ex){ex.printStackTrace();}
}
}
if(event.getSource() == 另存A){ //處理另存
JFileChooser jfc = new JFileChooser();
jfc.showSaveDialog(panel);
tempFile = jfc.getSelectedFile();
try{
FileWriter fos = new FileWriter(tempFile);
textArea.write(fos);
}catch(Exception ex){ex.printStackTrace();}
}
if(event.getSource() == 剪切T){ //處理剪切
textArea.cut();
}
if(event.getSource() == 復制C){ //處理復制
textArea.();
}
if(event.getSource() == 粘貼P){ //處理粘貼
textArea.paste();
}
if(event.getSource() == 關於A){ //處理關於
textArea.setText("Manifest-Version: 1.0\n" +
"Created-By: Libra_JL\n" +
"QQ: 254791521\n");
textArea.setEditable(false);
panel.validate();
validate();
}
}
public static void main(String []args){ //主函數
new MainClass();
}
}