简体   繁体   English

如何从私有方法中获取价值

[英]How to take a value from a private method

I have created using NetBeans a jSlider ChangeEvent, with the following code: 我使用NetBeans使用以下代码创建了一个jSlider ChangeEvent:

public class Slider extends javax.swing.JFrame {
    public Slider() {
         initComponents;
         field.getText();
         String fieldVal = field.getText();
         jtextField1.setText(fieldVal):
    }

    public JTextField getField() {
         return field;
    }

    public void setField(JTextField field) {
          this.field = field;
    }

    private void sliderStateChanged(javax.swing.event.ChangeEvent evt) { 
          int value = slider.getValue();
          String val = value + "";
          field.setText(val + "%");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Slider().setVisible(true);
        }
    });
    }
}

So, I have a JTextField (field) who is receiving values from jSpiner. 因此,我有一个JTextField (字段),它从jSpiner接收值。 Everything works fine, but I want to take the "val" and make some computations with it. 一切正常,但我想使用“ val”并进行一些计算。

But I can't do that, because it's in a private method, and I've tried to make it public, with Refactor -> Change Method Parameters, but it gives me the following warning: Read-only block of text cannot be refactored., and it doesn't work. 但是我不能这样做,因为它在私有方法中,并且我尝试使用Refactor -> Change Method Parameters将其公开,但是它给了我以下警告:只读文本块无法重构。,它不起作用。

I also have tried to make getters and setters, but still not working. 我也尝试过使吸气剂和二传手,但仍然无法正常工作。 All I want is to take that value. 我想要的就是珍惜这一价值。 I could take it directly from JTextField (field) but I want to have "%" also in it so I can't do computations... Can someone please help me with an idea? 我可以直接从JTextField(字段)中获取它,但是我也想在其中包含“%”,这样我就无法进行计算了。有人可以帮我一个忙吗? I know that I'm wrong with something, but don't know where is the mistake. 我知道我在某件事上错了,但是不知道哪里出了错。 Or is a possibility to put "%" in the text field in other way? 还是可以通过其他方式在文本字段中输入“%”? I need some help, thanks! 我需要一些帮助,谢谢!

Best regards, Iulia 最好的问候,伊利亚

Java Reflection to the rescue! Java 反射来营救! Use getDeclaredField on the class, and then setAccessible(true); 在类上使用getDeclaredField,然后使用setAccessible(true);。 lastly reflectively get the value through field.get(instance)! 最后通过field.get(instance)反射性地获取值! Tuh-duh. 嘟嘟

Alternatively, and much more complexly, implement a ClassLoader and modify the "modifiers" for said field/method to make it public... 另外,更复杂的是,实现ClassLoader并修改所述字段/方法的“修饰符”以使其公开。

Overall, reflection has major implications and impact. 总体而言,反思具有重大影响和影响。 Please read into it. 请仔细阅读。

据我了解,您不能让String val成为作用域之外的全局变量,而不是局部变量,然后再使用它吗?

A temporary solution can be : 临时解决方案可以是:

If you want to make changes to the restricted area, copy paste the code in a new .java file and edit. 如果要更改限制区域,请复制代码,将其粘贴到新的.java文件中并进行编辑。

If it is only about the value, create a public variable and assign the JSlider value. 如果仅与值有关,请创建一个公共变量并分配JSlider值。

From your edited question I can see that all you need is a text box showing value of a slider with % appended. 从您编辑的问题中,我可以看到,您所需要的只是一个文本框,其中显示了附加了的滑块的值。

Here is a code example showing this. 这是显示此代码示例。 https://gist.github.com/anonymous/96e898ec41cd2ed0f821 https://gist.github.com/anonymous/96e898ec41cd2ed0f821

Create a file name NewJFrame.java copy and pest the code and run to see the result. 创建一个文件名NewJFrame.java复制并粘贴代码,然后运行以查看结果。

As per your code I think theres something wrong with text field may be it's not initialized. 根据您的代码,我认为文本字段可能存在问题,可能是未初始化。 If you share your whole code my be I can find the bug. 如果您共享整个代码,我可以找到该错误。

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

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