Reputation: 75
I'm trying to make a GUI for this. Basically you press a button and you convert the hexadecimal to a binary and decimal. I feel like I'm getting close with my codes. I created a drive class and a panel class so far. I'm trying to create methods in my panel class that will convert hexadecimal to binary and a hexadecimal to a decimal. I keep getting errors though, and I'm not sure why. I've been following my other codes, but I've been struggling getting these methods at the bottom of the panel class. I'll show you my classes so far.
Driver class:
import javax.swing.JFrame;
public class DriverConverter {
//--------------------------------------------------------
// GUI for Converter
//--------------------------------------------------------
public static void main(String[] args) {
JFrame frame = new JFrame ("Hexadecimal to Binary and Decimal.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ConverterPanel());
frame.pack();
frame.setVisible(true);
}
}
Here's my panel class:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.InputMethodListener;
import java.util.Scanner;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ConverterPanel extends JPanel {
private JLabel[] binary;
private JLabel decimal;
private JLabel inputLabel, outputLabel, resultLabel, totalLabel, totalLabel2;
private JButton BinaryConvert, DecimalConvert;
private JTextField hexString;
private Font convertFont;
private double total;
//-------------------------------------------------------------
// Main GUI
//-------------------------------------------------------------
public ConverterPanel() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(400,300));
hexString = new JTextField();
//hexadecimal.addActionListener(new ConvertListener());
add(hexString);
JLabel converterName = new JLabel ("Hexadecimal Converter");
JPanel panelName = new JPanel();
panelName.add(converterName);
add(panelName, BorderLayout.NORTH);
JPanel totalPanel = new JPanel();
totalPanel.add(new JLabel ("Binary"));
totalLabel = new JLabel ("------");
totalPanel.add(totalLabel);
add(totalPanel, BorderLayout.SOUTH);
JPanel totalPanel2 = new JPanel();
totalPanel2.add(new JLabel ("Decimal"));
totalLabel2 = new JLabel ("------");
totalPanel2.add(totalLabel);
add(totalPanel2, BorderLayout.EAST);
}
// -------------------------------------------------------------
// Equation for Binary Conversion
//--------------------------------------------------------------
public void binaryConversion (double binary){
try {
Integer b = Integer.valueOf(hexString,16);
Integer.toBinaryString(b);
} catch (NumberFormatException ee) {
ee.printStackTrace();
}
}
// Equation for decimal conversion.
public void decimalConversion (double decimal){
String decimal = decimal.getText();
try {
Integer c = Integer.valueOf(hexString,16);
Integer.parseInt(hexString, 16);
} catch (NumberFormatException ee) {
ee.printStackTrace();
}
//Integer c = Integer.valueOf("444", 16);
//Integer.parseInt("444",16);
}
}
I've been experimenting with these methods at the bottom. I've been trying to create them without any errors. I also tried using the getText() method for the JTextField. I feel like I could be close with this, but there's something "off" with my logic of creating these methods for converting hexadecimal to binary, and binary to decimal. I actually know the code in the stardard editor. It makes sense to me in an editor because all you do is system.out.println the result. It's getting it translated for a GUI that's causing some issues.
Any help would be greatly appreciated. I realize this post it long. I'm just not sure where my error is. It could be at the beginning of the panel class, or just at the end, so I thought posting the whole code would help with identifying the problem. I'd appreciate any guidance you can give me. And am I at least on the right track? I've been trying hard to get this. Thanks.
EDIT:
Ok, I just ran it in JGrasp, and these are the errors I'm getting. It runs in Eclipse tho.
Exception in thread "main" java.lang.NoClassDefFoundError: ConverterPanel
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2531)
at java.lang.Class.getMethod0(Class.java:2774)
at java.lang.Class.getMethod(Class.java:1663)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: ConverterPanel
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 6 more
Upvotes: 0
Views: 3859
Reputation: 4403
For the compiler errors;
Your method binaryConversion
has an argument double decimal
which will hide the JLabel
called decimal. This means you're actually calling getText()
on a double
. Also you have defined the String to be called decimal, which further confuses the compiler.
Try this instead:
public void decimalConversion(double d) {
String s = decimal.getText();
try {
Integer c = Integer.valueOf(s);
} catch (NumberFormatException ee) {
ee.printStackTrace();
}
}
And in decimalConversion
Integer.valueOf(hexString,16);
should be Integer.valueOf(hexString.getText(),16);
Here's one way of how you can do it. This way you can enter text and press enter and it will be translated to decimal and binary and shown on the labels:
public class ConverterPanel extends JPanel {
private JLabel binaryLabel = new JLabel();
private JLabel decimalLabel = new JLabel();
private JTextField hexString = new JTextField();
// omitted some variables
public ConverterPanel() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(400, 300));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JLabel converterName = new JLabel("Hexadecimal Converter");
hexString.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
convertInput();
}
});
JPanel panelName = new JPanel(new GridLayout(1,2));
panelName.add(converterName);
panelName.add(hexString);
add(panelName, BorderLayout.NORTH);
JPanel totalPanel = new JPanel(new GridLayout(1,3));
totalPanel.add(new JLabel("Binary"));
totalLabel = new JLabel("------");
totalPanel.add(totalLabel);
totalPanel.add(binaryLabel);
JPanel totalPanel2 = new JPanel(new GridLayout(1,3));
totalPanel2.add(new JLabel("Decimal"));
totalLabel2 = new JLabel("------");
totalPanel2.add(totalLabel2);
totalPanel2.add(decimalLabel);
JPanel south = new JPanel(new GridLayout(2,1));
south.add(totalPanel);
south.add(totalPanel2);
add(south, BorderLayout.SOUTH);
}
private void convertInput() {
try {
Integer n = Integer.valueOf(hexString.getText(), 16);
decimalLabel.setText(String.valueOf(n));
binaryLabel.setText(Integer.toBinaryString(n));
} catch (NumberFormatException ee) {
ee.printStackTrace();
}
}
}
Here's an example of converting the text from hex to int and binary:
public class Test {
public static void main(String[] args) {
String string = "A3";
int parseInt = Integer.parseInt(string, 16);
System.out.println(parseInt);
System.out.println(Integer.toBinaryString(parseInt));
}
}
Output:
163
10100011
Upvotes: 2
Reputation: 59
Hello I found several errors in your code
1.-
First, you are trying to convert a Textfield when it should be a String, you must first obtain the value of Textfield with the method .getText()
public void binaryConversion (double binary){
try {
Integer b = Integer.valueOf(hexString.getText(),16);
Integer.toBinaryString(b);
} catch (NumberFormatException ee) {
ee.printStackTrace();
}
}
2.Second, you are trying to assign a value to a variable that has been declared as a parameter of the method, so you should change the name of the variable of method.
public void decimalConversion(double decimal) {
String decimal2 = String.valueOf(decimal);
try {
Integer c = Integer.valueOf(hexString.getText(), 16);
Integer.parseInt(hexString.getText(), 16);
} catch (NumberFormatException ee) {
System.out.println(ee.getMessage());
}
}
And check point 1 also in this method
3.-
Third item, not found the event that causes the conversion, you should add a button for each conversion and assign a actionPerformed event.
Upvotes: 2