简体   繁体   中英

java swing with no generated code

I am using Netbeans 8.0.2 i would like to know if there is any way to create a java Jframe without the generated code or if there id any way to edit that code

So to get rid of this (this is the automatically generated code):

   // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );
}// </editor-fold> 

i would like to know if there is any way to create a java Jframe without the generated code

yes:

JFrame myFrame = new JFrame("My Frame"):

Now we're done with the easy part.

The tougher part for you will likely be learning how to create and place components into your GUI when you're not using the NetBeans drag-and-drop GUI builder, and to do this successfully, you'll need to learn all about layout managers. Please go to the tutorial: Laying out Components within a Container .

Other key bits:

  • Avoid GroupLayout if possible since this was not meant to be used to hand-create code, but rather for IDE code creation. That's not to say that it can't be used, but it's neither simple nor intuitive and should certainly not be the first layout manager used.
  • Remember that it's easy to nest JPanels, and each one can use its own layout manager. This makes it easy to create complex GUI's using simple layout managers.
  • Best to gear your GUI creation towards creation of JPanels, and not extend JFrames. JPanels can be placed into JFrames, JDialogs, swapped with CardLayout, into JOptionPanes, into other JFrames. Best to create a JFrame when needed and where needed and plop your JPanel into it.

Well, you could add your components via your own method versus the initComponents() .

public class Test extends JFrame
{
    public Test()
    {
        initComponents();
        doMyCustomComponents();
    }

    public void doMyCustomComponents()
    {
        JFrame frame = new JFrame("Frame");
        //do whatever you need to.
        ...
        ...
        ...

    }
}

Note. This is a lot more complex because you are essentially writing the "designer" code on your own. You need to layout the UI and manage everything that initComponents would have done for you otherwise.

That automatically generated code is created by Netbeans and the information about read-only sections of code are kept in the java file.

You cannot see those comments as they're stripped from the viewable-source for presentation by Netbeans automagically.

You can modify the autogeneration by clicking Tools > Templates > Swing GUI Forms, and then right clicking the one you want to modify.

Note that this applies to all future autogenerations, not currently generated forms.

To remove or modify those read-only sections for one file, you need to open that file outside of Netbeans and remove:

//GEN-BEGIN:initComponents

and

//GEN-END:initComponents

from the sections you'd like to remove the read-only property from. Then you can modify or delete them to your leisure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM