简体   繁体   English

使用反射更改JFrame中私有成员的值

[英]Change value in private member in JFrame with reflection

Hello, 你好,

I have the following code, what I want is to modify the value of the instance called number to any value 我有以下代码,我想要的是将名为number的实例的值修改为任何值

I have both classes in the same package 我在同一个包中有两个班

ReflectionOnClass.class ReflectionOnClass.class

import javax.swing.*;
import java.awt.*;

public class ReflectionOnClass extends JFrame {

private int number = 2;
private JLabel jLabel = new JLabel("X: " + number);

public ReflectionOnClass() {
    setLayout(new FlowLayout());
    setPreferredSize(new Dimension(200,200));
    add(jLabel);
    setDefaultCloseOperation(3);
    setLocationRelativeTo(null);
    pack();
    setVisible(true);
    }
}

ExecReflection.class ExecReflection.class

import java.lang.reflect.Field;
import java.util.stream.Stream;

public class ExecReflection {

private static final String C = "ReflectionOnClass";

public ExecReflection() {

    try {
        final Field field = Class.forName(C).getDeclaredField("number");
        field.setAccessible(true);
        final ReflectionOnClass rF = new ReflectionOnClass();
        field.set(rF , 100);
        mostrar("Value number: " + field.get(rF));
    }catch (Exception ex){
        ex.printStackTrace();
    }
 }

 public <T> void mostrar(final T t){System.out.println(t);}
 public static void main(String ...gaga) {
    final Runnable r = ExecReflection::new;
    r.run();
 }
}

the output is the correct as in the image but in the JFrame there is no 输出是正确的,如图像中所示,但在JFrame中没有

结果

there is no possibility to change the value of said variable before the JFrame starts? 有没有可能在JFrame启动之前更改所述变量的值?

Update this was what I wanted, to modify the value before the JFrame started, but with reflection 更新这是我想要的,在JFrame启动之前修改值,但要进行反射

final Field field = Class.forName(C).getDeclaredField("jLabel");
field.setAccessible(true);
if(field.getType() == JLabel.class) {
  final JLabel j = (JLabel)field.get(ReflectionOnClass.class.newInstance());
  j.setText("X: " + 5000);
}

在此处输入图片说明

Its possible. 这是可能的。 You can add this functions to ReflectionOnClass: 您可以将此函数添加到ReflectionOnClass:

public void setNumber(int num){
    this.number = num;
    label.setText("X: " + this.number);
}

public int getNumber(){
    return this.number;
}

and just call them from ExecReflection: 然后从ExecReflection调用它们:

final ReflectionOnClass rF = new ReflectionOnClass();
rF.setNumber(4);
System.out.println("Value number: " + rF.getNumber());

Nothing is changing because you are not actually changing value that is displayed 没有任何变化,因为您实际上并未更改显示的值

private int number = 2;
private JLabel jLabel = new JLabel("X: " + number);

You need to access jLabel field, as this is your displayed value, and change text to something else, if number is used in other places, then you probably need to change both of them, so number to this example 100 value, and jLabel text to X: 100 您需要访问jLabel字段,因为这是您显示的值,然后将文本更改为其他内容,如果在其他地方使用number ,则可能需要将它们都更改,因此将number更改为此示例的100值,然后将jLabel文本至X: 100

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

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