Java 使用JOptionPane和JFrame的逻辑相同,但结果不同
代码1: 代码2:Java 使用JOptionPane和JFrame的逻辑相同,但结果不同,java,swing,joptionpane,Java,Swing,Joptionpane,代码1: 代码2: public static JFrame frame = null; public myClass(JFrame frame1) { initComponents(); frame = frame1; String result = JOptionPane.showInternalInputDialog( frame.getContentPane(), "Sample"); } 我想要代码1中的结果,但代码必须看起来像代码2。
public static JFrame frame = null;
public myClass(JFrame frame1)
{
initComponents();
frame = frame1;
String result = JOptionPane.showInternalInputDialog(
frame.getContentPane(), "Sample");
}
我想要代码1中的结果,但代码必须看起来像代码2。为什么它们有不同的结果?如果两段代码都相关,那么结果就不可能不同。 因此,这意味着您提供的代码不够完整或您没有明确说明“不同结果”的含义 我猜您正在同时创建多个
myClass
实例?我建议试试这个:(没有所有的static
)
“不同的结果”是什么意思?
public static JFrame frame = null;
public myClass(JFrame frame1)
{
initComponents();
frame = frame1;
sampleMethod();
}
public static void sampleMethod()
{
String result = JOptionPane.showInternalInputDialog(
frame.getContentPane(), "Sample");
}
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");
}