1

私は今、SSCEEのガイドラインに従って問題を再現しました。

次の 3 つのクラス (クラス コード) をコピー、貼り付け、コンパイルする準備ができているはずです。

Menu(): (これは私のメインクラスです)

    public class Menu extends javax.swing.JFrame {

    /**
     * Creates new form Menu
     */
    public Menu() {
    initComponents();
    }

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    cmdMainWindow = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    cmdMainWindow.setText("Main Window");
    cmdMainWindow.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cmdMainWindowActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(cmdMainWindow, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(cmdMainWindow)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

private void cmdMainWindowActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    MainWindow main = new MainWindow();
    main.setVisible(true);

}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /*
     * Set the Nimbus look and feel
     */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /*
     * If Nimbus (introduced in Java SE 6) is not available, stay with the
     * default look and feel. For details see
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /*
     * Create and display the form
     */
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new Menu().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JButton cmdMainWindow;
// End of variables declaration
    }

MainWindow():

    public class MainWindow extends javax.swing.JFrame {

private MainWindow main;
public String strOptionOne;

/**
 * Creates new form MainWindow
 */
public MainWindow() {
    initComponents();
}



/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    lblOptionOne = new javax.swing.JLabel();
    cmdOptions = new javax.swing.JButton();
    txtOptionOne = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    lblOptionOne.setText("Option One");

    cmdOptions.setText("Options");
    cmdOptions.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cmdOptionsActionPerformed(evt);
        }
    });

    txtOptionOne.setEditable(false);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(lblOptionOne)
                .addComponent(cmdOptions, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(txtOptionOne))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(lblOptionOne)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(txtOptionOne, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(11, 11, 11)
            .addComponent(cmdOptions)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

private void cmdOptionsActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
   ConfigWindow config = new ConfigWindow(main);
   config.setVisible(true);

}
     public String setOptionOne() {
  ConfigWindow config = new ConfigWindow(main);

strOptionOne = config.getOptionOne();
return strOptionOne;
}
/**
 * @param args the command line arguments
 */

// Variables declaration - do not modify
private javax.swing.JButton cmdOptions;
private javax.swing.JLabel lblOptionOne;
public javax.swing.JTextField txtOptionOne;
// End of variables declaration
    }

ConfigWindow():

    public class ConfigWindow extends javax.swing.JFrame {
private MainWindow main;
public String btnTxtOptionOne;

/**
 * Creates new form ConfigWindow
 */
public ConfigWindow(MainWindow main) {
    initComponents();
    this.main = main;
}


public String getOptionOne() {

if ("1".equals(grpOptionOne.getSelection())) {
    btnTxtOptionOne = "1";
    return this.btnTxtOptionOne;
}

if ("2".equals(grpOptionOne.getSelection())) {
    btnTxtOptionOne = "2";
    return this.btnTxtOptionOne;
}
return btnTxtOptionOne;
    }



/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    grpOptionOne = new javax.swing.ButtonGroup();
    lblOptionOne = new javax.swing.JLabel();
    btn1 = new javax.swing.JRadioButton();
    btn2 = new javax.swing.JRadioButton();
    cmdApplySettings = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    lblOptionOne.setText("Option One");

    grpOptionOne.add(btn1);
    btn1.setText("1");

    grpOptionOne.add(btn2);
    btn2.setText("2");

    cmdApplySettings.setText("ApplySettings");
    cmdApplySettings.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cmdApplySettingsActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(lblOptionOne)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(btn1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(btn2))
                .addComponent(cmdApplySettings))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(lblOptionOne)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btn1)
                .addComponent(btn2))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(cmdApplySettings)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

private void cmdApplySettingsActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
   main.txtOptionOne.setText(main.strOptionOne);
    dispose();
}

/**
 * @param args the command line arguments
 */

// Variables declaration - do not modify
private javax.swing.JRadioButton btn1;
private javax.swing.JRadioButton btn2;
private javax.swing.JButton cmdApplySettings;
private javax.swing.ButtonGroup grpOptionOne;
private javax.swing.JLabel lblOptionOne;
// End of variables declaration
    }

すべてのコードを修正したので、問題が何であるかをもう一度説明します。

MainWindow から ConfigWindow に移動して Options を選択すると、オプションを選択した後、ConfigWindow のメソッドが選択したボタンの値を取得し、次に MainWindow のメソッドがこの値を取得して MainWindow 内の変数に設定します。次に、ConfigWindow で [適用] をクリックすると、MainWindow からのメソッドが実行され、選択されたオプションで MainWindow 変数が設定されますが、そうではありません!

私はプロジェクトを最も基本的な形にトリミングしましたが、今では適用をクリックするとネットビーンズでエラーがスローされますが、以前は何もしませんでしたが、ネットビーンズではエラーはありませんでした。

ここでなんとか SSCEE を達成できたことを願っています ... がんばります!!!

4

2 に答える 2

1

これは、あなたが投稿したものを進めて、単一のソースファイルに圧縮したソースです。修正すべき問題はまだありますが、まだ動作しない理由を指摘するのに役立つはずのデバッグ ステートメント (値の出力) を追加しました。投稿の下部にある詳細なメモを参照してください。

public class Menu117 extends javax.swing.JFrame {

    /**
     * Creates new form Menu
     */
    public Menu117() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        cmdMainWindow = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        cmdMainWindow.setText("Main Window");
        cmdMainWindow.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdMainWindowActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(cmdMainWindow, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE).addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(cmdMainWindow).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>

    private void cmdMainWindowActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        MainWindow main = new MainWindow();
        main.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Menu117.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Menu117.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Menu117.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Menu117.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Menu117().setVisible(true);
            }
        });
    }
// Variables declaration - do not modify
    private javax.swing.JButton cmdMainWindow;
// End of variables declaration
}

class MainWindow extends javax.swing.JFrame {

    // This IS a MainWindow.  No need to keep a reference to one as well!
//private MainWindow main;
    public String strOptionOne;

    /**
     * Creates new form MainWindow
     */
    public MainWindow() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        lblOptionOne = new javax.swing.JLabel();
        cmdOptions = new javax.swing.JButton();
        txtOptionOne = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblOptionOne.setText("Option One");

        cmdOptions.setText("Options");
        cmdOptions.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdOptionsActionPerformed(evt);
            }
        });

        txtOptionOne.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(lblOptionOne).addComponent(cmdOptions, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(txtOptionOne)).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(lblOptionOne).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(txtOptionOne, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(11, 11, 11).addComponent(cmdOptions).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>

    private void cmdOptionsActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        ConfigWindow config = new ConfigWindow(this);
        config.setVisible(true);

    }
    /*
     * public String setOptionOne() { ConfigWindow config = new
     * ConfigWindow(this);
     *
     * strOptionOne = config.getOptionOne(); return strOptionOne;
    }
     */
    /**
     * @param args the command line arguments
     */
// Variables declaration - do not modify
    private javax.swing.JButton cmdOptions;
    private javax.swing.JLabel lblOptionOne;
    public javax.swing.JTextField txtOptionOne;
// End of variables declaration
}

class ConfigWindow extends javax.swing.JFrame {

    private MainWindow main;
    public String btnTxtOptionOne;

    /**
     * Creates new form ConfigWindow
     */
    public ConfigWindow(MainWindow main) {
        initComponents();
        this.main = main;
    }

    public String getOptionOne() {
        System.out.println(grpOptionOne.getSelection().getActionCommand());
        if ("1".equals(grpOptionOne.getSelection().getActionCommand())) {
            btnTxtOptionOne = "1";
        } else if ("2".equals(grpOptionOne.getSelection())) {
            btnTxtOptionOne = "2";
        } else {
            btnTxtOptionOne = "-1";
        }
        return this.btnTxtOptionOne;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        grpOptionOne = new javax.swing.ButtonGroup();
        lblOptionOne = new javax.swing.JLabel();
        btn1 = new javax.swing.JRadioButton();
        btn2 = new javax.swing.JRadioButton();
        cmdApplySettings = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblOptionOne.setText("Option One");

        grpOptionOne.add(btn1);
        btn1.setText("1");

        grpOptionOne.add(btn2);
        btn2.setText("2");

        cmdApplySettings.setText("ApplySettings");
        cmdApplySettings.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdApplySettingsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(lblOptionOne).addGroup(layout.createSequentialGroup().addComponent(btn1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(btn2)).addComponent(cmdApplySettings)).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(lblOptionOne).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(btn1).addComponent(btn2)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(cmdApplySettings).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>

    private void cmdApplySettingsActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        System.out.println(evt);
        //main.txtOptionOne.setText(main.strOptionOne);
        main.txtOptionOne.setText(getOptionOne());
        dispose();
    }
    /**
     * @param args the command line arguments
     */
// Variables declaration - do not modify
    private javax.swing.JRadioButton btn1;
    private javax.swing.JRadioButton btn2;
    private javax.swing.JButton cmdApplySettings;
    private javax.swing.ButtonGroup grpOptionOne;
    private javax.swing.JLabel lblOptionOne;
// End of variables declaration
}
  1. SSCCE には 1 つのソース ファイルが必要ですが、253 行あり、実際には「短く」ありません。自動生成された Netbeans コメント (非常に余分で苛立たしい) を削除し、不要なコード ブロックを削除して PLAF を設定すると (これはこの問題とは関係ありません)、コードはおそらく 150 行未満になり、おそらく短いものになります。 「SSCCE」と呼ぶには十分です。
  2. コード ブロックには一貫性のある論理的なインデントを使用します。コードのインデントは、人々がプログラムの流れを理解できるようにすることを目的としています。Netbeans では、これはキーの組み合わせを押すのと同じくらい簡単ですAlt+ Shift+F
    上記のコードは、現在のバージョンの Netbeans がコードをフォーマットする方法ですが、新しいバージョンでは、メソッド シグネチャが char で始まるようにするために、最初のインデントが省略されると思われます行に 0 (表示する幅を広げます)。
  3. Netbeans についてはしばらく脇に置いておくことを強くお勧めします。基本的な Java に慣れるまでは、邪魔になるだけです。
  4. このソースは 3 フレームを使用します。アプリケーションは通常、メイン アプリケーション ウィンドウ用のフレームを 1 つだけ持つ (べきです)。JDialog他の 2 つはまたはに入れることができますJOptionPane複数の JFrames の使用、良い/悪い習慣を参照してください。詳細については。
于 2013-02-23T09:47:25.397 に答える
1

最終的にこれを整理しましたが、私はこの問題を完全に考えすぎていました。

作業方法:

public String getOptionOne() {

    if (btn1.isSelected()) {
        btnTxtOptionOne = "1";
    } else if (btn2.isSelected()) {
        btnTxtOptionOne = "2";
    } else {
        btnTxtOptionOne = "-1";
    }
    return this.btnTxtOptionOne;
}

作業適用ボタン:

private void cmdApplySettingsActionPerformed(java.awt.event.ActionEvent evt) {

    System.out.println(evt);
    main.txtOptionOne.setText(getOptionOne());
    dispose();
}

完全な SSCCCE ソース:

public class Menu117 extends javax.swing.JFrame {

public Menu117() {
    initComponents();
}

@SuppressWarnings("unchecked")
private void initComponents() {

    cmdMainWindow = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    cmdMainWindow.setText("Main Window");
    cmdMainWindow.addActionListener(new java.awt.event.ActionListener() {

        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cmdMainWindowActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(cmdMainWindow, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE).addContainerGap()));
    layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(cmdMainWindow).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

    pack();
}

private void cmdMainWindowActionPerformed(java.awt.event.ActionEvent evt) {

    MainWindow main = new MainWindow();
    main.setVisible(true);
}

public static void main(String args[]) {

    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Menu117.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Menu117.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Menu117.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Menu117.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }

    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new Menu117().setVisible(true);
        }
    });
}
private javax.swing.JButton cmdMainWindow;
}

class MainWindow extends javax.swing.JFrame {

public String strOptionOne;

public MainWindow() {
    initComponents();
}

@SuppressWarnings("unchecked")
private void initComponents() {

    lblOptionOne = new javax.swing.JLabel();
    cmdOptions = new javax.swing.JButton();
    txtOptionOne = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    lblOptionOne.setText("Option One");

    cmdOptions.setText("Options");
    cmdOptions.addActionListener(new java.awt.event.ActionListener() {

        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cmdOptionsActionPerformed(evt);
        }
    });

    txtOptionOne.setEditable(false);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(lblOptionOne).addComponent(cmdOptions, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(txtOptionOne)).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
    layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(lblOptionOne).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(txtOptionOne, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(11, 11, 11).addComponent(cmdOptions).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

    pack();
}

private void cmdOptionsActionPerformed(java.awt.event.ActionEvent evt) {

    ConfigWindow config = new ConfigWindow(this);
    config.setVisible(true);

}
private javax.swing.JButton cmdOptions;
private javax.swing.JLabel lblOptionOne;
public javax.swing.JTextField txtOptionOne;
}

class ConfigWindow extends javax.swing.JFrame {

private MainWindow main;
public String btnTxtOptionOne;

public ConfigWindow(MainWindow main) {
    initComponents();
    this.main = main;
}

public String getOptionOne() {

    if (btn1.isSelected()) {
        btnTxtOptionOne = "1";
    } else if (btn2.isSelected()) {
        btnTxtOptionOne = "2";
    } else {
        btnTxtOptionOne = "-1";
    }
    return this.btnTxtOptionOne;
}

@SuppressWarnings("unchecked")
private void initComponents() {

    grpOptionOne = new javax.swing.ButtonGroup();
    lblOptionOne = new javax.swing.JLabel();
    btn1 = new javax.swing.JRadioButton();
    btn2 = new javax.swing.JRadioButton();
    cmdApplySettings = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    lblOptionOne.setText("Option One");

    grpOptionOne.add(btn1);
    btn1.setText("1");


    grpOptionOne.add(btn2);
    btn2.setText("2");

    cmdApplySettings.setText("ApplySettings");
    cmdApplySettings.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cmdApplySettingsActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(lblOptionOne).addGroup(layout.createSequentialGroup().addComponent(btn1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(btn2)).addComponent(cmdApplySettings)).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
    layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(lblOptionOne).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(btn1).addComponent(btn2)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(cmdApplySettings).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

    pack();
}

private void cmdApplySettingsActionPerformed(java.awt.event.ActionEvent evt) {

    System.out.println(evt);

    main.txtOptionOne.setText(getOptionOne());
    dispose();
}
private javax.swing.JRadioButton btn1;
private javax.swing.JRadioButton btn2;
private javax.swing.JButton cmdApplySettings;
private javax.swing.ButtonGroup grpOptionOne;
private javax.swing.JLabel lblOptionOne;
    }

アンドリューの助けに感謝するのと同じように、彼のアドバイスと支援により、私は正しい道を歩むことができました!

于 2013-02-23T20:14:16.443 に答える