自毁JFrames

时间:2013-06-19 04:05:02

标签: java c++ user-interface

自从我学习了C ++以来,我不禁想到C ++方面的一些其他编程语言(即Java)中的某些特性。这可能是因为我根本不相信他们100%(我认为相信任何100%是愚蠢的),而关于JFrame的东西只能证实我的担忧。我记得在某个地方读过JFrames请求的内存必须在不再使用时返回给系统,这可以通过JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);来完成我想知道是否有一个类是JFrame在C ++中,使用delete this;类的close()方法调用System.out.print()来进行处理。

我试过这个,并让类实现WindowListener。然后我在windowClosed()windowClosing()中添加了一些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 } 方法,并且在关闭JFrame时没有发生任何问题!

我做了类似的事情:

{{1}}

}

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

1 个答案:

答案 0 :(得分:1)

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

addWindowListener(this);