Swing: taking variables from two private jTextFiel

2019-08-18 06:14发布

I'm in NetBeans 8.2 and I've constructed a nice jFrame GUI for a Binary-to-Decimal converter, I think I have the code in my two text fields correct but I can't figure out how to access the variables for my Button to perform operations on.

I have 3 method operations called parseBinary, isBinary, and illegal that I want to use to perform operations on my two text fields. In NetBeans when you code a button the method is locked to private so that's my issue.

I will post all my of code and highlight where my issue is (near the bottom):

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Assignment2_gui extends javax.swing.JFrame
{

public static int parseBinary(String binary) throws NumberFormatException
{
    if (!isBinary(binary))
    {
        throw new NumberFormatException("Invalid Format for a Binary String - Illegal character: " + illegal(binary));
    }
    int power = 0;
    int decimal = 0;
    for (int i = binary.length() - 1; i >= 0; i--)
    {
        if (binary.charAt(i) == '1')
        {
            decimal += Math.pow(2, power);
        }
        power++;
    }
    return decimal;
}

public static boolean isBinary(String binary)
{
    for (char ch : binary.toCharArray())
    {
        if (ch != '1' && ch != '0')
        {
            return false;
        }
    }
    return true;
}

public static char illegal(String iChar)
{
    char test = 0;
    char arr[] = iChar.toCharArray();
    for(char cha : arr)
    {
        if (cha != '1' && cha != '0')
        {
            test = cha;
        }
    }
    return test;
}

/**
 * Creates new form Assignment2_gui
 */
public Assignment2_gui()
{
    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")



///////// the issue is with this jButton method and the two jTextField methods /////////

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

}                              

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    JTextField test1 = new JTextField("");
    test1.setText(test1.getText());
}                                           

private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    JTextField test2 = new JTextField("");
    test2.setText(test2.getText());
}                                           

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */

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

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration                   
}

0条回答
登录 后发表回答