在eclipse中,兩個布局檔案,我要點擊一個button,要觸發另一個類里面的button點擊事件,應該怎么搞?
uj5u.com熱心網友回復:
讓這兩個按鈕的點擊事件呼叫同一個方法符合你的要求嗎?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.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);
}
}
class PA extends JPanel {
private PB pb;
public PA(PB pb) {
this.pb = pb;
JButton btnInPA = new JButton("in pa");
btnInPA.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pb.mInPB();
}
});
add(btnInPA);
}
}
class PB extends JPanel {
public PB() {
JButton btnInPB = new JButton("in PB");
btnInPB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mInPB();
}
});
add(btnInPB);
}
public void mInPB() {
System.out.println("由PB中的方法mInPB()輸出。");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/141086.html
標籤:Eclipse
上一篇:在程式中回圈多次執行hive查詢,經常會隨機出現HiveSqlException和Socket closed 例外,求大佬幫忙
下一篇:各位大佬,這個該怎么寫
