这可能不是完全正确的答案,但这将完成这项工作。
假设您有 2 个 Jframe,即 Home.java 和 Second.java
Second.java 的代码如下,
public static String selection = "";//static variable to store seletced value from combobox
Home h = new Home();//instance of Home Jframe
/**
* return selected value (called from Home Jframe)
*/
public static String getSeletced() {
return selection;
}
/**
* get selected value from comboBox event
*/
private void cmbLapActionPerformed(java.awt.event.ActionEvent evt) {
selection = cmbLap.getSelectedItem().toString();
h.isSelected = true;//this is to control data duplication
}
现在对于 Home.java 文件,我们可以使用 formWindowGainedFocus
事件来更新jTextArea
. Home .java 文件包含以下代码,
public static boolean isSelected = false;//flag to check combo box is selected
private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {
System.out.println(isSelected);
if (isSelected) {
String text = new Second().getSeletced();
System.out.println(text);
txaData.append("Your Laptop: " + text + "\n");//appending data
isSelected = false;//to prevent duplication
}
}
此方法可用于使用来自另一个 jFrame 的数据更新 jTextArea。