在JAVA(选项窗口)中无法从子代返回父级的变量 [英] Cannot get variable from Child back to Parent in JAVA (Options Window)
问题描述
开始 - 3:00 PM
更新1 - 5:36
)class:
private void cmdApplyActionPerformed(java.awt.event.ActionEvent evt){
// TODO add your在这里处理代码:
hud.setTime(btnTxtTime);
hud.setTemp(btnTxtTemp);
hud.setSurface(btnTxtSurface);
hud.setWeather(btnTxtWeather);
hud.setRadiation(btnTxtRadiation);
dispose();
}
)类。
public class选项extends javax.swing.JFrame {
public String btnTxtTime;
public String btnTxtTemp;
public String btnTxtSurface;
public String btnTxtWeather;
public String btnTxtRadiation;
public static boolean ApplyClicked;
/ **
*创建新表单简介
* /
私人HUD hud;
public Options(HUD hud){
initComponents();
this.hud = hud;
}
这是Option class:
public String getTime(){
if(Day.equals(grpTimeOfDay。 getSelection())){
btnTxtTime =Day;
return this.btnTxtTime;
}
if(Night.equals(grpTimeOfDay.getSelection())){
btnTxtTime =Night;
return this.btnTxtTime;
}
return null;
}
这是如何从HUD ):
private void cmdOptionsActionPerformed(java.awt.event.ActionEvent evt){
// TODO添加您的处理代码这里:
选项o =新选项(hud);
this.getLocation(p);
o.setLocation((int)p.getX()+ 100,(int)p.getY()+ 100);
o.setVisible(true);
}
这是我的HUD()类的开始:
public abstract class HUD extends javax.swing.JFrame implements Runnable {
private Options o;
private HUD hud;
public HUD(Options o){
initComponents();
this.o = o;
这是HUD()的方法,它从Options()获取JButtons的值:
public void setTime(String strTime){
strTime = o.getTime
txtTime.setText(strTime);但是每当我单击应用,选项()中设置的选项不是然后(),然后选择()设置在TextFields中显示它们在HUD()就像他们应该是:/ 解决方案从我的理解,HUD是你的
但是当你应用的时候,你正在设置一个 new
em> HUD,而不是您之前使用过的。
要解决这个问题,您需要在配置窗口中使用主窗口的句柄,以便可以设置属性
ConfigFrame config = new ConfigFrame();
config.setHUD(this);
config.setVisible(true);
在您的配置中
private HUD hud;
public void setHUD(HUD hud){
this.hud = hud;
}
那么只要省略 HUD hud = new hud ();
STARTED - 3:00PM
UPDATE 1 - 5:36PM
Apply Button in the Option() class:
private void cmdApplyActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
hud.setTime(btnTxtTime);
hud.setTemp(btnTxtTemp);
hud.setSurface(btnTxtSurface);
hud.setWeather(btnTxtWeather);
hud.setRadiation(btnTxtRadiation);
dispose();
}
This is a section of the Option() Class.
public class Options extends javax.swing.JFrame {
public String btnTxtTime;
public String btnTxtTemp;
public String btnTxtSurface;
public String btnTxtWeather;
public String btnTxtRadiation;
public static boolean ApplyClicked;
/**
* Creates new form Profile
*/
private HUD hud;
public Options(HUD hud) {
initComponents();
this.hud = hud;
}
This is a method in Option() class:
public String getTime() {
if ("Day".equals(grpTimeOfDay.getSelection())) {
btnTxtTime = "Day";
return this.btnTxtTime;
}
if ("Night".equals(grpTimeOfDay.getSelection())) {
btnTxtTime = "Night";
return this.btnTxtTime;
}
return null;
}
This is how Options() is openned from within HUD():
private void cmdOptionsActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Options o = new Options(hud);
this.getLocation(p);
o.setLocation((int) p.getX() + 100, (int) p.getY() + 100);
o.setVisible(true);
}
This is the start of my HUD() Class:
public abstract class HUD extends javax.swing.JFrame implements Runnable {
private Options o;
private HUD hud;
public HUD(Options o) {
initComponents();
this.o = o;
and this is the method from HUD() which gets the value of the JButtons from Options():
public void setTime(String strTime) {
strTime = o.getTime();
txtTime.setText(strTime);
}
However whenever I click Apply, the options set in Options() are not then set in the TextFields that display them in HUD() like they should be :/
解决方案 From what I understand, HUD is your 'main window' and the users gets to this option frame from that window.
But when you apply, you're setting the properties on a new HUD, not the one you had before.
To fix this, you need a handle to your main window in your config window, so that you can set the properties on it.
in your hud:
ConfigFrame config = new ConfigFrame();
config.setHUD(this);
config.setVisible(true);
In your config
private HUD hud;
public void setHUD(HUD hud){
this.hud = hud;
}
then just leave out the HUD hud = new hud();
这篇关于在JAVA(选项窗口)中无法从子代返回父级的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!