2013-06-19 79 views
0

自從我學習了C++之後,我不禁想到了其他一些編程語言(即Java)中的某些特性在C++方面的表現。這可能是因爲我完全不信任他們(我認爲100%信任是愚蠢的),而關於JFrame的東西只能證實我的擔憂。我記得在某處讀取JFrames請求的內存必須在系統不再使用時返回給系統,這可以通過JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);來完成我想知道是否有一個JFrame類讓它處理在C++中,用一個close()類的方法調用delete this;自毀式JFrames

我試過這個,並且讓類實現了WindowListener。然後,我在windowClosed(),windowClosing()中加入了一些System.out.print()方法,並且當我關閉JFrame時沒有發生任何事情!

我不喜歡的東西:

import java.awt.GridLayout; 
import java.awt.HeadlessException; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 
import java.awt.event.WindowListener; 
import java.awt.event.WindowEvent; 

import javax.swing.ButtonGroup; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 

public class ItemListenerDemo extends JFrame implements WindowListener,ItemListener 
{ 
private JRadioButton buttonA, buttonB; 
private ButtonGroup radioButtonGroup; 
private JLabel buttonSelectionLabel; 
private JPanel buttonPanel; 

private GridLayout gridLayout; 

public ItemListenerDemo(String title) throws HeadlessException 
{ 
    super(title); 
    //setting up the buttons 
    buttonA = new JRadioButton("Option A", true); 
    buttonB = new JRadioButton("Option B", false); 
    //adding itemListeners 
    buttonA.addItemListener(this); 
    buttonB.addItemListener(this); 
    //adding the buttons to the radioButtonGroup 
    radioButtonGroup = new ButtonGroup(); 
    radioButtonGroup.add(buttonA); 
    radioButtonGroup.add(buttonB); 
    //adding buttons to JPanel 
    buttonPanel = new JPanel(); 
    buttonPanel.add(buttonA); 
    buttonPanel.add(buttonB); 

    //making the buttonSelectionLabel 
    buttonSelectionLabel = new JLabel("some text"); 
    //setting up the gridLayout 
    gridLayout = new GridLayout(2,1); 
    //setting the layout of the demo to the gridLayout 
    setLayout(gridLayout); 
    //adding components to demo 
    add(buttonSelectionLabel); 
    add(buttonPanel); 
    //setting up the demo 
    setSize(200,200); 
    setVisible(true); 
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
} 

/** 
* @param args 
*/ 
public static void main(String[] args) 
{ 
    ItemListenerDemo demo = new ItemListenerDemo("Item Listener Example"); 

} 

@Override 
public void itemStateChanged(ItemEvent e) { 
    // TODO Auto-generated method stub 
    buttonSelectionLabel.setText("You selected " + 
      ((JRadioButton)e.getItem()).getText()); 
} 

@Override 
public void windowActivated(WindowEvent e) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void windowClosed(WindowEvent e) { 
    // TODO Auto-generated method stub 
    System.out.println(this.toString() + " was deleted"); 
} 

@Override 
public void windowClosing(WindowEvent e) { 
    // TODO Auto-generated method stub 
    System.out.println(this.toString() + " was deleted"); 
} 

@Override 
public void windowDeactivated(WindowEvent e) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void windowDeiconified(WindowEvent e) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void windowIconified(WindowEvent e) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void windowOpened(WindowEvent e) { 
    // TODO Auto-generated method stub 

} 

}

是的,我結束了展示你我所有的CODE

+2

關於最後一部分 - 你是否實際上將該類註冊爲JFrame的WindowListener? –

+2

你的序言是不必要的。我們沒有時間閱讀所有不相關的東西。保持簡單明瞭的問題。更好地發佈你的'SSCCE'來證明你的問題。我們不能(總是)猜測你做錯了什麼。 – camickr

+0

你是否設置了默認的關閉操作,而不是'HIDE_ON_CLOSE'? – MadProgrammer

回答

1

你似乎忘了添加窗口監聽:

addWindowListener(this);