-2

Code 1:

public static JFrame frame = null;    
public myClass(JFrame frame1)
{
    initComponents();
    frame = frame1;
    String result = JOptionPane.showInternalInputDialog(
        frame.getContentPane(), "Sample");
}

Code 2:

public static JFrame frame = null;
public myClass(JFrame frame1)
{
    initComponents();
    frame = frame1;
    sampleMethod();
}

public static void sampleMethod() 
{
    String result = JOptionPane.showInternalInputDialog(
        frame.getContentPane(), "Sample");
}

I wanted the result in Code 1 but the code must look like Code 2. Why do they have different results?

4

1 回答 1

6

如果两段代码都是相关的,那么结果就不可能不同。因此,这意味着您提供的代码不够完整,或者您没有明确“不同结果”的含义。

我的猜测是您正在同时创建多个实例myClass?我建议试试这个:(没有所有这些static

public JFrame frame = null; // static removed
public myClass(JFrame frame1)
{
    initComponents();
    frame = frame1;
    sampleMethod();
}

public void sampleMethod() // static removed
{
    String result = JOptionPane.showInternalInputDialog(
        frame.getContentPane(), "Sample");
}
于 2013-01-01T02:46:10.123 回答