相关疑难解决方法(0)

从内部类对象获取外部类对象

我有以下代码.我想掌握使用它创建内部类对象的外部类对象inner.我该怎么做?

public class OuterClass {

    public class InnerClass {
        private String name = "Peakit";
    }

    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        InnerClass inner = outer.new InnerClass();
       // How to get the same outer object which created the inner object back?
        OuterClass anotherOuter = ?? ;

        if(anotherOuter == outer) {
             System.out.println("Was able to reach out to the outer object via inner !!");
        } else {
             System.out.println("No luck :-( ");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:嗯,你们中的一些人建议通过添加一个方法来修改内部类: …

java inner-classes

228
推荐指数
3
解决办法
14万
查看次数

从内部类调用外部类函数

我在Java中实现了一个嵌套类,我需要从内部类调用外部类方法.

class Outer {
    void show() {
        System.out.println("outter show");
    }

    class Inner{
        void show() {
            System.out.println("inner show");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎么称呼这个Outer方法show

java

120
推荐指数
1
解决办法
8万
查看次数

Android:为什么必须使用getBaseContext()而不是这个

this经常参考当前的背景.但是,在某些情况下,为什么我们必须使用getBaseContext()而不是this.(这意味着使用时this会发现错误).

这是我的例子:

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);            
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3){
       Toast.makeText(getBaseContext(),"SELECTED", Toast.LENGTH_SHORT).show(); //this line
    }
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,当我更改getBaseContext()this将收到错误.

请问谁可以帮我解释一下.

android this android-context

31
推荐指数
4
解决办法
7万
查看次数

如何从Java中的类中的函数访问主类对象?

请阅读以下代码中的2条评论.

public class Field extends LinearLayout {
    public void init() {
        setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

                // I want to access the main object 'Field' here(not the class, the object)
            }
        });

    // to be clear the object referred as 'this' from HERE should be accessed from where the above comment is.
    }
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?是否有关键字从对象内的函数访问主类对象?

java oop class object

5
推荐指数
1
解决办法
85
查看次数

'this'关键字的范围问题

我正在尝试创建自己的窗口类,扩展JFrame.但是,我遇到了动作监听器的问题fullScreenBtn.在编写ActionListener.actionPerformed函数时,我无法使用this它所引用的关键字new ActionListener.我如何参考实例MyWindow

public class MyWindow extends JFrame {
    private static GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    private static GraphicsDevice gDev = gEnv.getDefaultScreenDevice();
    private static JPanel toolbar = new JPanel();
    private static JButton fullScreenBtn = new JButton("Show Full Screen");
    private static boolean isFullScreen = false;

    public MyWindow() {
        toolbar.setLayout(new FlowLayout());
        this.getContentPane().add(toolbar, BorderLayout.PAGE_START);

        fullScreenBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Toggle full screen window
                this.setUndecorated(!isFullScreen);
                this.setResizable(isFullScreen);
                gDev.setFullScreenWindow(this);

                isFullScreen = …
Run Code Online (Sandbox Code Playgroud)

java swing jframe actionlistener

4
推荐指数
1
解决办法
328
查看次数

获取对匿名内部类的类对象的引用

如何在Java中获得对匿名内部类的类对象的引用?

对于非无限级的类,它完成了ClassName.class.

java anonymous-class

4
推荐指数
1
解决办法
258
查看次数

无法在JPanel引用上调用"this.firePropertyChange()"

为什么我不能firePropertyChange()this引用时调用?

    [javac] /home/thufir/NetBeansProjects/Legacy/src/legacy/gui/table/SelectTableFollowup.java:41: error: cannot find symbol
    [javac]                     this.firePropertyChange("client", null, i);
    [javac]                         ^
    [javac]   symbol: method firePropertyChange(String,<null>,int)
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] 1 error
    [javac] 1 warning

BUILD FAILED
/home/thufir/NetBeansProjects/Legacy/nbproject/build-impl.xml:923: The following error occurred while executing this line:
/home/thufir/NetBeansProjects/Legacy/nbproject/build-impl.xml:263: Compile failed; see the compiler error output for details.

Total time: 4 seconds
thufir@dur:~/NetBeansProjects/Legacy$ 
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

package legacy.gui.table;

import java.awt.BorderLayout;
import java.util.logging.Logger;
import javax.swing.JPanel;
import …
Run Code Online (Sandbox Code Playgroud)

java polymorphism swing netbeans matisse

1
推荐指数
1
解决办法
522
查看次数