Note that there are some explanatory texts on larger screens.

plurals
  1. POHexadecimal to binary to decimal GUI
    text
    copied!<p>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.</p> <p>Driver class:</p> <pre><code>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); } } </code></pre> <p>Here's my panel class:</p> <pre><code>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); } } </code></pre> <p>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.</p> <p>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.</p> <p>EDIT:</p> <p>Ok, I just ran it in JGrasp, and these are the errors I'm getting. It runs in Eclipse tho. </p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload