CodePudding user response:
Let the two button click event calls the same method to meet your requirements?
Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;
The import javax.mail. Swing. JButton;
The import javax.mail. Swing. JFrame;
The import javax.mail. Swing. JPanel;
Public class DoubleButton extends JFrame {
Public static void main (String [] args) {
New DoubleButton (.) setVisible (true);
}
Public DoubleButton () {
PB PB=new PB ();
SetBounds (0, 0, 200, 150);
PA PA=new PA (pb);
Add (pa);
}
}
The class PA extends JPanel {
Private PB PB;
The public PA (PB) in PB {
This. Pb=pb;
JButton btnInPA=new JButton (" in pa ");
BtnInPA. AddActionListener (new ActionListener () {
@ Override
Public void actionPerformed (an ActionEvent e) {
Pb. MInPB ();
}
});
Add (btnInPA);
}
}
The class PB extends JPanel {
Public PB () {
JButton btnInPB=new JButton (" in PB ");
BtnInPB. AddActionListener (new ActionListener () {
@ Override
Public void actionPerformed (an ActionEvent e) {
MInPB ();
}
});
Add (btnInPB);
}
Public void mInPB () {
System. The out. Println (" by the method of PB mInPB () output, ");
}
}