import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JFrm extends JFrame{ public JFrm(){ Container c=this.getContentPane(); JButton btn=new JButton("Button"); //Left JPanel JPanel p=new JPanel(); p.setBackground(Color.pink); //Right JPanel PanelB pb=new PanelB(); pb.setBackground(Color.BLUE); p.add(btn,BorderLayout.CENTER); c.add(p,BorderLayout.WEST); c.add(pb); //Add Listener ButtonListener bl=new ButtonListener(pb); btn.addActionListener(bl); this.setSize(400,300); this.setVisible(true); } public static void main(String[] args) { new JFrm(); } } class PanelB extends JPanel{ public JTextField txt; public PanelB( ){ txt=new JTextField(20); this.add(txt); } } //class ButtonListener implements ActionListener{ PanelB p; public ButtonListener(PanelB p){ this.p=p; }//这个是用到的什么机制? public void actionPerformed(ActionEvent e){ p.txt.setText(e.getActionCommand()); } } |
不明白?