import javax.swing.*;
import java.awt.*;
import java.awt.event.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class mee {
public static void main(String[] args){
JFrame childfr=new myJFrame();
childfr.setSize(300,400);
childfr.show();
}static class myJFrame extends JFrame implements ActionListener {
public myJFrame ()
{
super();
Container con=getContentPane();
JPanel pan=new JPanel();
JButton bt=new JButton("关闭窗口");
bt.addActionListener(this);
pan.add(bt);
con.add(pan); } public void actionPerformed(ActionEvent e){ System.exit(-1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
}
import java.awt.*;
import java.awt.event.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class mee {
public static void main(String[] args){
JFrame childfr=new myJFrame();
childfr.setSize(300,400);
childfr.show();
}static class myJFrame extends JFrame implements ActionListener {
public myJFrame ()
{
super();
Container con=getContentPane();
JPanel pan=new JPanel();
JButton bt=new JButton("关闭窗口");
bt.addActionListener(this);
pan.add(bt);
con.add(pan); } public void actionPerformed(ActionEvent e){ System.exit(-1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
}
解决方案 »
- 用swing 编写一个可以实现简单的加减乘除的计算器
- 迭代时,当textarea中id="sub${question.id}"时候,如何通过js得到每个id
- servlet调用javabean的包
- jfreechart标注单位的问题
- serialVersionUID什么类要用到?
- system.in.read()为什么输入一个字符,输出时输出两个字符?
- java的一般性问题!!
- 有了Swing,还有必要使用AWT吗?
- NoClassDefFoundError!!!第一次碰上
- 急!急!oracle 中 long 类型在 tomcat 中的问题
- 关注!applet画图板上生成的图片怎么保存的服务器上去阿!
- 急需帮助:一个JAVA问题,关系着我的前途,请各位多多指教
在一个已有的窗口里new一个新的frame
再如何监听新的frame上的Button的动作呢?
比如点了新frame上的按钮然后就关闭新的哪个frame而不是整个程序
button在哪个窗口上,没有关系的。
都可以用button.addActionLisener(new ActionListner(){
public void actionPerformed(ActionEvent e){
//code
}
};
class childframe extends JPanel{
private JButton bt=new JButton("close window");
public childframe(){
add(bt);
}
}
class mainframe extends JFame{
public static void main(String[] args)
{
javax.swing.JFrame frame = new javax.swing.JFrame();
childframe aUI=new childframe();
frame.setContentPane(aUI);
frame.show();
}
}问题在于bt在那边定义的,怎么加listener呢?