Java Swing从子对话框访问父数据 [英] Java Swing accesing parent data from child dialog
本文介绍了Java Swing从子对话框访问父数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使JFrame成为父级,而Jdialog成为子级.但是我无法访问变量的修改后的值
子类中的父项.代码:
I made a JFrame parent and Jdialog child. But I am not able to access the modified value of the variable
of parent from the child class. code:
import [swing packages needed];
public class grading extends JFrame implements ActionListener
{
public int me=4; //this one
JTextField jt;
grading()
{
Container c=getContentPane();
// set the layout
jt=new JTextField(30);
jt.addActionListener(this);
c.add(jt);
setSize(800,600);
show();
}
public void actionPerformed(ActionEvent a) // pressed enter key in the textfield
{
me=2; //changed me value
JOptionPane.showMessageDialog(null,(me)+" is me from parent"); //displayed : 2
child jd=new child(this); //calls dialog
jd.setVisible(true);
}
public static void main(String ar[])
{
grading Gr=new grading();
//close window handling
}
}
class child extends JDialog
{
JTextField jta[];
child(JFrame frame)
{
super(frame,true);
Container c=getContentPane();
c.setLayout(new FlowLayout());
grading gr=new grading();
JOptionPane.showMessageDialog(null,(gr.me)+" is me from child"); //shows 4
setSize(400,300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
推荐答案
您正在子类中创建第二个评分,而不是访问父类.
尝试更换线路:
You''re creating a second grading in the child class, rather than accessing the parent class.
Try replacing the lines:
grading gr=new grading();
JOptionPane.showMessageDialog(null,(gr.me)+" is me from child"); //shows 4
与
with
JOptionPane.showMessageDialog(null,(frame.me)+" is me from child");
这篇关于Java Swing从子对话框访问父数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文