简体   繁体   English

如何修改私有访问对象?

[英]How to modify a private access object?

I made a frame and added a desktop pane (dp_1) to it. 我做了一个框架并添加了一个桌面窗格(dp_1)。 Then I created a separate internal jframe form (Inj) and assigned this code to a button in frame form (main). 然后我创建了一个单独的内部jframe表单(Inj)并将此代码分配给帧形式(main)的按钮。

Inj inj = new Inj();
dp_1.add(inj);
inj.setVisible(true);

It works fine. 它工作正常。 Then I created another internal jframe form(Inj_1). 然后我创建了另一个内部jframe表单(Inj_1)。 I want to set it visible by a button in Inj. 我想通过Inj中的按钮将其设置为可见。 I did trying the same code, but there comes an error. 我尝试了相同的代码,但出现了错误。

Error is ; 错误是;

error: dp_1 has private access in NewJFrame

* () include the variable name. * ()包括变量名称。

You should provide a public method in the class that owns that variable and let it modify its own state. 您应该在拥有该变量的类中提供一个公共方法,并让它修改自己的状态。

You can also use reflection if you must, but I don't recommend it. 如果必须,你也可以使用反射,但我不推荐它。

You must be trying to use dp_1 outside the class in which it is defined. 您必须尝试在定义它的类之外使用dp_1

SO the solution is to either increase its visibility by modifying access level to either default, protected or public 因此,解决方案是通过将访问级别修改为default,protected或public来提高其可见性

Or create a public method that returns dp_1 object from it and then access it. 或者创建一个公共方法, dp_1返回dp_1对象,然后访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM