0

自从学了 C++ 之后,我时不时就会想到 C++ 方面的一些其他编程语言(即 Java)的一些特性。这可能是因为我根本无法 100% 信任它们(我认为 100% 信任任何东西都是愚蠢的),而关于 JFrame 的内容只会证实我的担忧。我记得在某处读到 JFrame 请求的内存在不再使用时必须归还给系统,这可以通过JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);我想知道是否有一个 JFrame 类使其在关闭时处理相当于在 C++ 中调用类delete this;close()方法。

我已经尝试过了,并让该类实现了 WindowListener。然后我把一些System.out.print()方法放进去windowClosed()windowClosing()当我关闭 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

}

}

是的,我最终向你展示了我所有的代码

4

1 回答 1

1

您似乎忘记了添加窗口侦听器:

addWindowListener(this);
于 2013-06-19T22:23:12.487 回答