将方法放在java swing gui生成的程序中并添加" controller"

时间:2015-04-12 00:05:55

标签: java swing user-interface

我非常困惑如何将控制器的东西添加到java Swing gui 1)代码是自动生成的,不可触及的 当我将组件拖放到gui中时,会自动生成一个名为initcomponents()的大量内容 - 这个initcomponents()在构造函数中调用,其中一部分包括pack(),现在纠正我,如果我错了,但这意味着交易是密封的?现在包装好了,不能修改? - 但如何自动更改标签文本?所以我有一个生成字符串的方法,无论如何都要做variableName.setText(stringMethod)?我在哪里打电话给它?由于不可触及的initcomponents(),我无法在构造函数中调用它 - 我想在哪里放一些程序的肉?在构造函数?或者在方法中然后在构造函数中调用方法?由于不可触及的自动生成的东西,我无法调用构造函数中的任何方法。

我很困惑 - 在这个刽子手的事情上花了100多个小时

继承人代码:

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                   

}

0 个答案:

没有答案