2012-12-30 26 views
1

我正在使用actionListener和WindowListener事件的簡單應用程序。目標是根據添加到面板的單擊按鈕使default_close_operation工作。這可以很容易地使用內部類來完成,但是我想爲每個偵聽器事件使用不同的類。下面的代碼:監聽事件的不同類

//test.java 
import java.awt.event.WindowEvent; 
import javax.swing.*; 
public class test extends JFrame 
{ 
    public static void main(String args[]) 
    { 
     new test(); 
    } 

    private JButton button, exit; 
    action a1 = new action(); 
    close c1 = new close(); 
    public test() 
    { 
     this.setSize(200,200); 
     this.setTitle("test "); 
     this.setVisible(true); 
     this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
     button = new JButton("Button"); 
     exit = new JButton("Exit"); 
     exit.addActionListener(c1); 
     button.addActionListener(a1); 
     JPanel p1 = new JPanel(); 
     p1.add(button); 
     p1.add(exit); 
     this.add(p1); 
    } 

}

//action.java

import javax.swing.*; 
import java.awt.event.*; 
public class action extends WindowAdapter implements ActionListener 
{ 
    JButton b1; 

    public void actionPerformed(ActionEvent e) 
    { 
     b1 = (JButton)e.getSource(); 
     if(b1.getText().equalsIgnoreCase("button")) 
     { 
      b1.setText("Clicked"); 
     } 
     else if(b1.getText().equalsIgnoreCase("exit")) 
     { 
      System.exit(0); 
     } 
    } 
} 

//close.java

import javax.swing.*; 
import java.awt.event.*; 
public class close extends WindowAdapter 
{ 
    public void windowClosing(WindowEvent e) 
    { 
     exit.doClick(); //WRONG as no variable exists with exit name. 
    } 
} 

一切都很好,除了在close.java類。我如何將WindowClosing方法指向動作類,以便程序以正確的方式終止?

+1

你想要什麼行爲來實現?在關閉應用程序窗口之前,是否需要先按下按鈕,或者是否要用按鈕關閉窗口,...? –

+0

在Java中,類名通常以大寫字母開頭。 – sorencito

+0

我不知道你用這段代碼試圖完成什麼,但你的聽衆看起來有點搞砸了。也許你可以幫助我們理解你想要做的事情。 – Pr0gr4mm3r

回答

0

可以擴展的ActionListener只是你爲Action類的方式,退出在Action.java

//from your code 
else if(b1.getText().equalsIgnoreCase("exit")) 
    { 
     System.exit(0); 
    } 
0

處理事情你需要的JFrame在自己的動作偵聽器,這樣

執行操作的方式
public class MyListener extends WindowAdapter implements ActionListener 
{ 

    JFrame frame ; 
    public MyListener(JFrame component) { 
     this.frame = component; 
    } 


    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     String actionCommand = e.getActionCommand(); 
     if(actionCommand.equals("Exit")) { 
      //When the last displayable window within the Java virtual 
      // machine (VM) is disposed of, the VM may terminate 
      frame.dispose(); 
     } 
    } 

    @Override 
    public void windowClosing(WindowEvent e) { 
      // perfrom operation after window closed 
     Component component = e.getComponent(); 
     String name = component.getName(); 
     System.out.println("Component " + name + " is closing"); 

    } 


    @Override 
    public void windowClosed(WindowEvent e) { 
     System.out.println("window is closed"); 
    } 

} 

添加監聽器框架和按鈕

JFrame frame = new JFrame("Test Frame"); 
    buttonExit = new JButton("Exit"); 
    listener = new MyListener(this); 
    frame.addWindowListener(listener); 
    buttonExit.addActionListener(listener);