📄 anceventdemo.java
字号:
import javax.swing.*;import javax.swing.event.*;import java.awt.*;public class AncEventDemo extends JFrame implements AncestorListener{ private JButton button; private JTextField jtf; public AncEventDemo() {/* A JButton is created and placed on a JFrame. The JButton *//* registers an AncestorListener. Because the AncEventDemo *//* class serves as the AncestorListener, the addAncestorListener() *//* method is passed the "this" reference. */ button = new JButton("help"); button.setBorder(BorderFactory.createRaisedBevelBorder()); button.addAncestorListener(this); jtf = new JTextField(15); jtf.setEditable(false); JPanel panel = new JPanel(); panel.add(button); getContentPane().add(panel, BorderLayout.CENTER); getContentPane().add(jtf, BorderLayout.SOUTH); setName("frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 300, 200); setVisible(true); }/* The AncEventDemo class serves as the AncestorListener so the *//* implementations of the methods declared in the AncestorListener *//* interface are provided inside the AncEventDemo class. If the *//* JFrame is moved, the JButton generates an AncestorEvent which *//* is sent to the ancestorMoved() method. */ public void ancestorMoved(AncestorEvent event) { Container ancestor = event.getAncestor(); String framename=ancestor.getName(); try{ if (framename.equals("frame") ) { jtf.setText("frame was moved to ("+ancestor.getX()+","+ ancestor.getY()+")"); } }catch(Exception e){} } public void ancestorAdded(AncestorEvent event) {} public void ancestorRemoved(AncestorEvent event){} public static void main(String args[]) { AncEventDemo demo = new AncEventDemo(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -