Netbeans Java SE GUI-Builder: private initComponents() problem

Wenn ich Baue eine GUI für die Java-SE-Anwendung mit Netbeans GUI-builder, es bringt all die codes in die initComponents() Methode, die ist privat. Ich konnte es nicht ändern, um der öffentlichkeit. So, alle Komponenten sind zugänglich nur für die Klasse, in der die UI.

Ich für den Zugriff auf die Komponenten von einer anderen Klasse, so dass ich schreiben kann, benutzerdefinierten event-Handler und alles. Am wichtigsten ist, die ich trennen möchte meine GUI-code und nicht die Oberfläche von jedem anderen.

Kann ich kopieren und einfügen der GUI-code und später veröffentlicht Sie mit der hand zu erreichen, was ich will. Aber das ist ein Schmerz. Ich habe zu Handwerk ein Teil, Wann immer ich brauche, um re-design der Benutzeroberfläche.

Was ich versucht habe:

Benutzte ich die Variablen-Bezeichner, um die text-box der öffentlichkeit. Nun, wie kann ich das Textfeld aus der Main-Klasse? Ich glaube, ich brauche die Komponente generiert eine öffentliche Methode als gut.

Ich bin neu in Java. Jeder hilft?

Hier ist der Beispiel-Klassen:

Die UI (uiFrame.java)

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * uiFrame.java
 *
 * Created on Jun 3, 2010, 9:33:15 PM
 */
package barcode;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import net.sourceforge.barbecue.output.OutputException;

/**
 *
 * @author masnun
 */
public class uiFrame extends javax.swing.JFrame {

    /** Creates new form uiFrame */
    public uiFrame() {
        try {
            try {
                //Set cross-platform Java L&F (also called "Metal")
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(uiFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(uiFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(uiFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedLookAndFeelException ex) {
                Logger.getLogger(uiFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        } finally {
        }


        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() {

        label1 = new javax.swing.JLabel();
        textBox = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        label1.setFont(label1.getFont().deriveFont(label1.getFont().getStyle() | java.awt.Font.BOLD, 13));
        label1.setText("Type a text:");
        label1.setName("label1"); //NOI18N

        saveButton.setText("Save");
        saveButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                saveButtonMousePressed(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()
                .addGap(56, 56, 56)
                .addComponent(textBox, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(72, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(154, Short.MAX_VALUE)
                .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(144, 144, 144))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(140, Short.MAX_VALUE)
                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(127, 127, 127))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(saveButton)
                .addContainerGap(193, Short.MAX_VALUE))
        );

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

    @SuppressWarnings("static-access")
    private void saveButtonMousePressed(java.awt.event.MouseEvent evt) {                                        
        JFileChooser file = new JFileChooser();
        file.showSaveDialog(null);
        String data = file.getSelectedFile().getAbsolutePath();
        String text = textBox.getText();
        BarcodeGenerator barcodeFactory = new BarcodeGenerator();
        try {
            barcodeFactory.generateBarcode(text, data);
        } catch (OutputException ex) {
            Logger.getLogger(uiFrame.class.getName()).log(Level.SEVERE, null, ex);
        }


    }                                       
    /**
     * @param args the command line arguments
     */
    //Variables declaration - do not modify
    private javax.swing.JLabel label1;
    private javax.swing.JButton saveButton;
    public javax.swing.JTextField textBox;
    //End of variables declaration
}

Die Main-Klasse (Main.java)

package barcode;


import javax.swing.JFrame;


public class Main {

    public static void main(String[] args) {

        JFrame ui = new uiFrame();
        ui.pack();
        ui.show();




    }


}
InformationsquelleAutor masnun | 2010-06-04
Schreibe einen Kommentar