1

im super confused about how to add the controller stuff to java Swing gui 1) the code is auto generated and untouchable when i drag and drop the components into the gui a massive thing called initcomponents() is auto generated -this initcomponents() is called in the constructor and part of this includes a pack(), now correct me if im wrong but this means the deal is sealed? things are now packed and cant be modified? -but how do i automatically change the text of labels? so i have a method that generates a string, is there anyway to do variableName.setText(stringMethod)? where do i call it? i cant call it in constructor because of untouchable initcomponents() -where am i suppose to put all the meat of program? in constructor? or in method and then call method in constructor? i cant call any methods in constructor because of untouchable auto generated thing.

i am so confused - spent 100+ hours on this hangman thing

heres code:

public class NewJFrame extends javax.swing.JFrame {
protected static final Insets   bottomInsets    = new Insets(10, 10, 10, 10);
protected static final Insets   normalInsets    = new Insets(10, 10, 0, 10);
protected static final Insets   spacingInsets   = new Insets(20, 10, 0, 10);
private int                     wordGridy;
public static int     maximumWrongGuesses =7;
HangmanModel model;
private List<String>          word;
private List<JLabel>          wordText;
private JPanel                  outsidePanel;
private JPanel                  panel;
/**
 * Creates new form NewJFrame
 */
public NewJFrame() {
    //this.frame = frame


    initComponents();
    tryingToControl();



}
public void tryingToControl(){
    lettersUsedText.setText(model.getNumberOfGuesses());
}


@Override
public void paint(Graphics g)
{
    super.paint(g);

    Gallows gallows = new Gallows();
    Figure figure = new Figure();

    Color color1 = Color.BLUE;
    Color color2 = Color.BLUE;
    g.setColor(color1);
    gallows.paintComponent(g);


    switch (7) {
    case 7:
        color2 = Color.red;
        figure.drawNoose(g, color2);
    case 6:
        figure.drawRightLeg(g, color2);
    case 5:
        figure.drawLeftLeg(g, color2);
    case 4:
        figure.drawRightArm(g, color2);
    case 3:
        figure.drawLeftArm(g, color2);
    case 2:
        figure.drawBody(g, color2);
    case 1:
        figure.drawHead(g, color2);
    }

}
/**
 * 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() {

    secretWordTitle = new javax.swing.JLabel();
    secretWordText = new javax.swing.JLabel();
    lettersUsedTitle = new javax.swing.JLabel();
    lettersUsedText = new javax.swing.JLabel();
    guessTitle = new javax.swing.JLabel();
    guessTextField = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    secretWordTitle.setText("Secret Word:");

    secretWordText.setText("?????????????");

    lettersUsedTitle.setText("Letters Used:");

    lettersUsedText.setText(model.getUnguessedLetters());

    guessTitle.setText("Enter a guess:");

    guessTextField.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            guessTextFieldActionPerformed(evt);
        }
    });

    jButton1.setText("Guess");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jButton2.setText("Reset Game");

    jButton3.setText("Custom Word");

    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(47, 47, 47)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(guessTitle)
                        .addComponent(secretWordTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lettersUsedTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(39, 39, 39)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(secretWordText, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lettersUsedText, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(guessTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jButton1))))
                .addComponent(jButton3)
                .addComponent(jButton2))
            .addContainerGap(79, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(401, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(secretWordTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(secretWordText, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(lettersUsedTitle)
                .addComponent(lettersUsedText))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(guessTitle)
                .addComponent(guessTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton1))
            .addGap(18, 18, 18)
            .addComponent(jButton2)
            .addGap(18, 18, 18)
            .addComponent(jButton3)
            .addGap(30, 30, 30))
    );

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

private void guessTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                               
    // TODO add your handling code here:
}                                              

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
     secretWordText.setText("guess");   


}                                        


/**
 * @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JTextField guessTextField;
private javax.swing.JLabel guessTitle;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel lettersUsedText;
private javax.swing.JLabel lettersUsedTitle;
private javax.swing.JLabel secretWordText;
private javax.swing.JLabel secretWordTitle;
// End of variables declaration                   

}

Tintinabulator Zea
  • 2,617
  • 5
  • 18
  • 32
  • if you want to have full flexibility you probably need to code your components. It's a trade off you need to consider. From my personal experience, it's better to code your GUI rather than using your IDE drag'n'drop features, but again it is my personal experience. – dazito Apr 12 '15 at 00:12
  • what ide are you using and can you provide some code please? Its hard to give hints how to accesx sth if you cant see the environment. – thst Apr 12 '15 at 00:29
  • Possible [duplicate](http://stackoverflow.com/q/12023334/230513). – trashgod Apr 12 '15 at 00:50

0 Answers0