Jua*_*ong -2 java swing joptionpane
代码1:
public static JFrame frame = null;
public myClass(JFrame frame1)
{
initComponents();
frame = frame1;
String result = JOptionPane.showInternalInputDialog(
frame.getContentPane(), "Sample");
}
Run Code Online (Sandbox Code Playgroud)
代码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");
}
Run Code Online (Sandbox Code Playgroud)
我希望代码1中的结果,但代码必须看起来像代码2.为什么它们有不同的结果?
如果两段代码都相关,则结果不可能不同.因此,这意味着您提供的代码不够完整,或者您没有明确表达"不同结果"的含义.
而我的猜测是你同时创建了多个实例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");
}
Run Code Online (Sandbox Code Playgroud)