所以我有一个问题..在最底层它说//十进制到二进制它给我一个错误说我不能显示我的保持变量...错误是"这种表达的类型' INT'不是参考类型"请帮忙!
// The "RADIO_BUTTONGUI" class.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.URL;
import java.awt.Color;
public class DecimalToBinary
{
static JLabel leftTitle, rightTitle, leftInstruct, rightInstruct, decimal, binary;
static JButton leftBtn, rightBtn;
static JTextField leftInput, rightInput, leftAns, rightAns;
static int decimalAns = 0;
static int binaryAns = 0;
static int decList[] = new int[]{128, 64, 32, 16, 8, 4, 2, 1};
private static void guiApp ()
{
// rgb(41, 182, 246) blue
// rgb(231, 76, 60) red
JFrame frame = new JFrame ("Binary <=> Decimal Converter");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel (new GridLayout (6, 2));
panel.setBackground (new Color (44, 62, 80));
leftTitle = new JLabel ("Binary 2 Decimal", JLabel.CENTER);
leftTitle.setForeground (new Color (41, 182, 246));
leftTitle.setFont (new Font ("Times New Roman", Font.BOLD, 35));
leftInstruct = new JLabel ("Enter an 8-digit binary number:", JLabel.CENTER);
leftInstruct.setForeground (new Color (41, 182, 246));
leftInstruct.setFont (new Font ("Times New Roman", Font.BOLD, 15));
leftInput = new JTextField ("");
leftInput.setForeground (Color.black);
leftInput.setBackground (new Color (41, 182, 246));
leftInput.setHorizontalAlignment (JTextField.CENTER);
leftInput.setFont (new Font ("Times New Roman", Font.BOLD, 20));
leftBtn = new JButton ("Convert to Decimal");
leftBtn.setForeground (Color.black);
leftBtn.setBackground (new Color (41, 182, 246));
decimal = new JLabel ("Decimal Equivalent", JLabel.CENTER);
decimal.setForeground (new Color (41, 182, 246));
decimal.setFont (new Font ("Times New Roman", Font.BOLD, 20));
leftAns = new JTextField ("");
leftAns.setBackground (new Color (41, 182, 246));
leftAns.setForeground (Color.black);
leftAns.setFont (new Font ("Times New Roman", Font.BOLD, 20));
leftAns.setHorizontalAlignment (JTextField.CENTER);
//////////////////////////////////////////////////////////////////////////////
rightTitle = new JLabel ("Decimal 2 Binary", JLabel.CENTER);
rightTitle.setForeground (new Color (242, 4, 24));
rightTitle.setFont (new Font ("Times New Roman", Font.BOLD, 35));
rightInstruct = new JLabel ("Enter a Decimal Number 0-255:", JLabel.CENTER);
rightInstruct.setForeground (new Color (242, 4, 24));
rightInstruct.setFont (new Font ("Times New Roman", Font.BOLD, 15));
rightInput = new JTextField ("");
rightInput.setBackground (new Color (242, 4, 24));
rightInput.setForeground (Color.white);
rightInput.setFont (new Font ("Times New Roman", Font.BOLD, 20));
rightInput.setHorizontalAlignment (JTextField.CENTER);
rightBtn = new JButton ("Convert to Binary");
rightBtn.setForeground (Color.white);
rightBtn.setBackground (new Color (242, 4, 24));
binary = new JLabel ("Binenter code hereary Equivalent", JLabel.CENTER);
binary.setForeground (new Color (242, 4, 24));
binary.setFont (new Font ("Times New Roman", Font.BOLD, 20));
rightAns = new JTextField ("");
rightAns.setForeground (Color.white);
rightAns.setBackground (new Color (242, 4, 24));
rightAns.setFont (new Font ("Times New Roman", Font.BOLD, 20));
rightAns.setHorizontalAlignment (JTextField.CENTER);
ButtonHandler onClick = new ButtonHandler ();
leftBtn.addActionListener (onClick);
rightBtn.addActionListener (onClick);
panel.add (leftTitle);
panel.add (rightTitle);
panel.add (leftInstruct);
panel.add (rightInstruct);
panel.add (leftInput);
panel.add (rightInput);
panel.add (leftBtn);
panel.add (rightBtn);
panel.add (decimal);
panel.add (binary);
panel.add (leftAns);
panel.add (rightAns);
Container contentPane = frame.getContentPane ();
contentPane.add (panel);
frame.setSize (600, 500);
frame.setVisible (true);
}
private static class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
String command = e.getActionCommand ();
final String value = leftInput.getText ();
String binary = leftInput.getText ();
int binAry = binary.length ();
String decimal = rightInput.getText ();
int decimalLength = decimal.length ();
int hold = 0;
for (int x = 0 ; x < 8 ; x++)
{
if (e.getSource () == leftBtn)
{
if (binAry < 8 || binAry > 8)
{
JOptionPane.showMessageDialog (null, "You Must Only Enter An 8 Digit Number");
}
else if (value.charAt (x) == '1' || value.charAt (x) == '0')
{
JOptionPane.showMessageDialog (null, "You May Only Enter 1 or 0");
}
else if (binAry == 8)
{
for (int i = 8 ; i < 0 ; i++)
{
if (value.substring (i, i + 1).equals ("1"))
{
hold += Math.pow (2, 7 - i);
}
}
}
leftAns.setText ("" + hold);
}
}
//DECIMAL TO BINARY
for(int x = 0; decimalLength > 8; x++)
{
hold = decimalLength% 2;
hold /= 2;
}
decimalAns.setText("" + hold);
}
}
public static void main (String[] args)
{
javax.swing.SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
guiApp ();
}
}
);
} // main method
} // RADIO_BUTTONGUI class
答案 0 :(得分:0)
您正在尝试从没有方法的int类调用方法。如果您尝试将decimalAns
转换为字符串,请尝试使用Integer.toString()
。
String theString = Integer.toString(decimalAns);