gpt4 book ai didi

java - 自毁 JFrames

转载 作者:行者123 更新时间:2023-11-28 07:33:36 27 4
gpt4 key购买 nike

自从学了C++之后,我时不时会用C++来联想一些其他编程语言(即Java)的一些特性。这可能是因为我根本没有 100% 信任它们(我认为 100% 信任任何东西都是愚蠢的),而关于 JFrame 的东西只证实了我的担心。我记得在某处读到 JFrames 请求的内存在不再使用时必须归还给系统,这可以通过 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

}

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

最佳答案

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

addWindowListener(this);

关于java - 自毁 JFrames,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17182709/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com