我在NetBeans 8.2中,我为二进制到十进制转换器构建了一个很好的jFrame GUI,我想我的两个文本字段中的代码是正确的,但我无法弄清楚如何访问我的Button执行操作的变量。
我有3个方法操作,称为parseBinary,isBinary和非法,我想用它来对我的两个文本字段执行操作。 在NetBeans中,当您对按钮进行编码时,该方法将被锁定为私有,因此这是我的问题。
我将发布我的所有代码并突出显示我的问题所在(靠近底部):
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
}