当前位置: 动力学知识库 > 问答 > 编程问答 >

java - main gui and newjdialog mixed up on netbeans

问题描述:

I have created new java application on Netbeans by pressing create new project and it creates java package. This fully fine ! When I add new JFrame it creates and automatically MAIN METHOD , after I am done with Matisse I mean putting some gui components , I want to add JDialog to that , I go on package right click and create JDialog Form which also created MAIN METHOD as well so itself. So both MAINS mixes up. this is confusing me all the time. My Aim is to create JMenuItem called new , when I click on it I want my JDialog to be appear and something like project creation dialog. Help please ! How to combine to different components in these situations ?

Regards

网友答案:

"this so confusing always , do have some examples "

I'm not so sure what is so confusing. Your program should only have one launching class with a main method. Netbeans will create a main method for you in JDialog forms, so just delete the main method. The only main method you need is your main JFrame form.

You have your JDialog form

public class MyDialog extends javax.swing.JDialog {
    public MyDialog(final Frame parent, boolean modal) {
        super(parent, model);
        initComponents();
    }

    private void initiComponent() {
        ...
    }

    // delete the auto-generated main method
}

You have your JFrame form with JMenuItem. Add a listener to the JmenuItem to open the MyDialog

public class MyFrame extends javax.swing.JFrame {
    private javax.swing.JMenuItem jMenuItem1;

    public MyFrame() {
        initComponents();
    }

    /* Auto-generated code */
    private void initComponents() {
        jMenuItem1 = new JMenuItem();
        jMenuItem1.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
    }

    /* Auto-generated method */
    private jmenuItemActionPerformed(java.awt.event.ActionEvent evt) {
        /* Your hand written code */
        MyDialog dialog = new MyDialog(MyFrame.this, true);
    }

    public static void main(String[] args) {

    }
}

" How to combine to different components in these situations ? "

What does that even mean?


Side Note

  • I'd recommend going through Swing Tutotials and learning to hand code before jumping in to GUI Builder tools.
分享给朋友:
您可能感兴趣的文章:
随机阅读: