java 运行class 传参_JAVA 不同Class传值问题
展開全部
可以通過構(gòu)62616964757a686964616fe4b893e5b19e31333365663431造方法傳遞給 B, 如下:import?java.awt.*;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JTextField;
class?B?extends?JFrame?{
public?B(String?val)?{
this.setTitle("B");
this.setSize(300,?200);
this.setLocationRelativeTo(null);
this.setLayout(new?FlowLayout());
//?將?a?傳過來的值?給?JTextField
JTextField?txtVal?=?new?JTextField(val);
txtVal.setPreferredSize(new?Dimension(100,?23));
this.add(txtVal);
}
}
public?class?App?extends?JFrame?{
String?a?=?"123";
public?App()?{
this.setSize(300,?200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new?FlowLayout());
JButton?btnTest?=?new?JButton("打開B");
btnTest.addActionListener(e?->?{
//?傳?a?到?B?的構(gòu)造函數(shù)
B?b?=?new?B(a);
b.setVisible(true);
});
this.add(btnTest);
}
public?static?void?main(String[]?args)?{
new?App().setVisible(true);
}
}
運(yùn)行結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的java 运行class 传参_JAVA 不同Class传值问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 19年诺基亚830还可以做什么用(Cor
- 下一篇: 男子花400万买光金条 店员果断报警:都