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
    }

主窗口():

    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
    }

配置窗口():

    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 中单击 Apply 时,应该运行 MainWindow 中的方法,使用选定的选项设置 MainWindow 变量,但它没有!

我已经将我的项目修剪为最基本的形式,现在它在单击应用时在 netbeans 中引发错误,而在它根本没有做任何事情之前,netbeans 中没有错误。

我希望我已经成功地在这里完成了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 需要一个源文件,但在 253 行时,它并不是真正的“短”。如果您删除了那些(非常多余和令人讨厌的)自动生成的 Netbeans 注释并删除了不必要的代码块来设置 PLAF(与此问题无关),那么它可能会少于 150 行代码,这可能很短足以称为“SSCCE”。
  2. 对代码块使用一致且合乎逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。在Netbeans中,这就像按下组合键Alt++一样简单 上面看到的代码是我当前版本的 Netbeans 格式化代码的方式,尽管我怀疑较新的版本会省略第一个缩进,以便让方法签名从 char 开始0 在一行上(提供更多宽度来显示它们)。ShiftF
  3. 强烈建议您暂时将 Netbeans 放在一边。在您熟悉基本的 Java 之前,它只会妨碍您。
  4. 此来源使用 3 帧。应用程序将(应该)通常只有一个主应用程序窗口的框架。其他两个可能放入 aJDialog或 aJOptionPane中。请参阅使用多个 JFrame,好/坏做法?更多细节。
于 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 回答