从另一个类访问txtField值

时间:2018-04-18 11:57:46

标签: java jtextfield

我想从我的Jframe文本字段中获取另一个类的值。 为什么它在我的例子中不起作用?

我在主 JFrame 类中创建了一个getter,它返回了txtIP1,这是我框架内的Textfield。

在辅助类中的 bepaalklasse 方法中,我试图获取文本字段txtIP1的内容并将其解析为整数,以便我可以将其用于该方法。

中学班级

private int iKlasse_;
private String sKlasse_;
private JFrame JFrame;

public void setiKlasse_(int iKlasse_) {
    this.iKlasse_ = iKlasse_;
}

public void BepaalKlasse() {
    iKlasse_ = Integer.parseInt(JFrame.getTxtIP1().getText());
    System.out.println("iKlasse_" + iKlasse_);
    try {
        if (JFrame.getTxtIP1().getText().isEmpty()) {
            throw new IOException();
        }
        if ((iKlasse_ >= 0) && ((iKlasse_) <= 127)) {
            sKlasse_ = "A Klasse";

        } else if ((iKlasse_ > 127) && ((iKlasse_ <= 191))) {
            sKlasse_ = "B Klasse";

        } else if ((iKlasse_ > 191) && ((iKlasse_ <= 223))) {
            sKlasse_ = "C Klasse";

        } else if ((iKlasse_ > 223)) {
            sKlasse_ = "N/B";
        }
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, "Gelieve een cijfer in te voeren in het eerste vak", "Leeg IP veld!", JOptionPane.ERROR_MESSAGE);

    }
}

public String getsKlasse_() {
    return sKlasse_;
}

} 主要类

private clsInput oInput = new clsInput();
static private clsKlasse oKlasse = new clsKlasse();
private String sKlasse;
protected int iGeselecteerd;

public JFrame() {
    initComponents();
    iniCombobox();
}

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

    jScrollPane1 = new javax.swing.JScrollPane();
    txtaDisplay = new javax.swing.JTextArea();
    btnKies = new javax.swing.JButton();
    btnPrinten = new javax.swing.JButton();
    txtIP1 = new javax.swing.JTextField();
    txtIP2 = new javax.swing.JTextField();
    txtIP3 = new javax.swing.JTextField();
    txtIP4 = new javax.swing.JTextField();
    cmbSubnetten = new javax.swing.JComboBox<>();
    jLabel1 = new javax.swing.JLabel();
    lblKlasse = new javax.swing.JLabel();
    btnBerekenen = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    txtaDisplay.setColumns(20);
    txtaDisplay.setRows(5);
    jScrollPane1.setViewportView(txtaDisplay);

    btnKies.setText("Kies");
    btnKies.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnKiesActionPerformed(evt);
        }
    });

    btnPrinten.setText("Export");

    jLabel1.setText("Klasse:");

    btnBerekenen.setText("bereken");
    btnBerekenen.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnBerekenenActionPerformed(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()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(txtIP1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtIP2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtIP3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtIP4, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addGap(0, 0, Short.MAX_VALUE))
                                .addComponent(cmbSubnetten, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(btnKies, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblKlasse, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(btnBerekenen))
                .addComponent(btnPrinten))
            .addContainerGap(35, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(txtIP1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(txtIP2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(txtIP3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(txtIP4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(btnBerekenen, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbSubnetten, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnKies, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(lblKlasse, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(btnPrinten)
            .addContainerGap(17, Short.MAX_VALUE))
    );

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

private void btnKiesActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
    DoorzendenCmb();
    oInput.Subnetten();
}                                       

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

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

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new JFrame().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton btnBerekenen;
private javax.swing.JButton btnKies;
private javax.swing.JButton btnPrinten;
private javax.swing.JComboBox<String> cmbSubnetten;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lblKlasse;
public javax.swing.JTextField txtIP1;
private javax.swing.JTextField txtIP2;
private javax.swing.JTextField txtIP3;
private javax.swing.JTextField txtIP4;
private javax.swing.JTextArea txtaDisplay;
// End of variables declaration                   

public JTextField getTxtIP1() {
    return txtIP1;
}

public JTextField getTxtIP2() {
    return txtIP2;
}

public JTextField getTxtIP3() {
    return txtIP3;
}

public JTextField getTxtIP4() {
    return txtIP4;
}

private void iniCombobox() { //ini van Combobox (1-10)
    for (int i = 0; i < 10; i++) {
        cmbSubnetten.addItem(String.valueOf(i));
    }
}

private void DoorzendenCmb() {
    iGeselecteerd = Integer.parseInt(cmbSubnetten.getSelectedItem().toString());
    oInput.setiGeselecteerd(iGeselecteerd);
}

private void InstellenKlasse(){
sKlasse = oKlasse.getsKlasse_();
lblKlasse.setText(sKlasse);
}

}

0 个答案:

没有答案