我有使用递归将我的java类的整数转换为二进制的赋值,它必须在GUI中完成。我可以使用扫描仪和控制台输入和输出来完成它,但我在使用GUI时遇到了一些麻烦。我已经在Netbeans GUI-builder中完成了它,但它不起作用,我用类似的逻辑编写了我自己的GUI,它不会编译。
我是一个初学者,因为它是家庭作业,我不希望任何人直接做这项工作,我只是想在正确的方向上轻推。我得到了这个概念,但我倾向于犯下愚蠢的错误,而不是思考简单的问题。
我搜索了Stack Overflow,并用Google搜索了几个小时,觉得我很亲近。 我有几个问题,当我使用Netbeans时,我应该如何将输出传递给textArea,另外我的方法实际上是递归的,还是我只是使用循环?如果我理解这些材料,我很确定我使用了递归。
请参阅我的附加代码:这是NetBeans WYSIWYG代码:
private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {
jTextFieldNum.setText("");
jTextAreaResults.setText("");
}
private void jButtonExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jButtonCalcActionPerformed(java.awt.event.ActionEvent evt) {
//get input and set into variables
int num1;
num1 = integer.parseInteger(jTextFieldNum.getText());
if (num1>255 || num1< 0);
{
jTextAreaResults.setText("Please Enter a number between 0 and 255.");
}
toBinary(num1);
public static void toBinary(int num1);
{
String r = "";
for ( int i=0; i<8; i++);
if (num1 % 2 ==1 )
{
r = '1' + r;
}
if (num1 %2 == 0)
{
r = '0' + r:
}
num1 = num1/2;
jTextArea.setText("Binary Equivaalent is" + r);
}
}
/**
* @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(Binary.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Binary.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Binary.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Binary.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 Binary().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonCalc;
private javax.swing.JButton jButtonClear;
private javax.swing.JButton jButtonExit;
private javax.swing.JLabel jLabelNum;
private javax.swing.JLabel jLabelResults;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextAreaResults;
private javax.swing.JTextField jTextFieldNum;
// End of variables declaration
}
这是我编写的GUI的代码,我尝试使用相同的逻辑,但看起来我正在使我的内部方法错误?我能做些什么,风格上和“理解”它。
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/*This class creates the GUI that we will be using to practice using recursive method calls
*/
public class BinaryConverter extends JFrame
{
//declare all variables, constants, and components for the GUI
private JPanel panel;
private JLabel messageLabelNum;
private JLabel messageLabelR;
private JTextField boxNum;
private JTextField boxCalc;
private JTextArea results;
private JButton convertButton;
private JButton clearButton;
private JButton exitButton;
private final int WINDOW_WIDTH = 1028;
private final int WINDOW_HEIGHT = 760;
/*
Constructor method for the GUI
*/
public BinaryConverter ()
{
//set title
setTitle("Binary Converter");
//set size of the window
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
//build the panel and add it to the frame and display the window
buildPanel();
add(panel);
setVisible(true);
}
/*
build panel adds label, text fields and all the buttons to the panel
*/
private void buildPanel()
{
//create the label for the text boxes, the boxes, and the buttons
messageLabelNum = new JLabel("Enter a Number");
messageLabelR = new JLabel("Results");
boxNum = new JTextField(10);
boxCalc = new JTextField(10);
results = new JTextArea(20, 80);
results.setEditable(false);
convertButton = new JButton("Convert to binary");
clearButton = new JButton(" Clear Fields");
exitButton = new JButton("Exit");
//Group the buttons, and add them to the panel
add(convertButton);
add(clearButton);
add(exitButton);
//Create a panel and add components to it
panel = new JPanel();
panel.add(boxNum);
panel.add(results);
panel.add(messageLabelNum);
panel.add(messageLabelR);
panel.add(results);
panel.add(convertButton);
panel.add(clearButton);
panel.add(exitButton);
}
/*
Private inner class for event handling when the user clicks buttons
*/
private class ButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
//convertButton, exitButton, and clearButton event handler
ButtonListener handler = new ButtonListener ();
convertButton.addActionListener(handler);
//get input and set into variables
int num1;
int result;
num1 = integer.parseInteger(boxNum.getText());
if (num1>255 || num1< 0);
{
results.setText("Please Enter a number between 0 and 255.");
}
toBinary(num1)
public void toBinary(int num1) {
}
{
String r = "";
for ( int i=0; i<8; i++);
if (num1 % 2 ==1 )
{
r = '1' + r;
}
if (num1 %2 == 0)
{
r = '0' + r;
}
num1 = num1/2;
result.setText("Binary Equivalent is " + r);
}
clearButton.addActionListener(handler);
boxNum.setText("");
boxCalc.setText("");
results.setText("");
//action listener for exit button
exitButton.addActionListener(handler);
System.exit(0);
}
public void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new BinaryConverter().setVisible(true);
}
});
}
}
}
我不太喜欢WYSWYG,因为我没有遵循其中生成的所有膨胀代码,但是我在上周实验室获得了关于在我自己的GUI中编写工作而不使用WSYWYG。
澄清问题, 1.如何将输出打印到jTextArea我认为jTextArea.setText方法会这样做。 2.我想我最紧迫的问题是如何让我的转换按钮在netbeans代码中工作,因为这是我必须转入的。
答案 0 :(得分:0)
我认为您正在寻找>>
和<<
函数(位移运算符)。这些,结合你的数字和数字1,可以用来实现你想要的。使用递归,for循环或其他:)