Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For the compiler errors; Your method <code>binaryConversion</code> has an argument <code>double decimal</code> which will hide the <code>JLabel</code> called decimal. This means you're actually calling <code>getText()</code> on a <code>double</code>. Also you have defined the String to be called decimal, which further confuses the compiler. Try this instead:</p> <pre><code>public void decimalConversion(double d) { String s = decimal.getText(); try { Integer c = Integer.valueOf(s); } catch (NumberFormatException ee) { ee.printStackTrace(); } } </code></pre> <p>And in <code>decimalConversion</code> <code>Integer.valueOf(hexString,16);</code> should be <code>Integer.valueOf(hexString.getText(),16);</code></p> <hr> <p>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:</p> <pre><code>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(); } } } </code></pre> <hr> <p>Here's an example of converting the text from hex to int and binary:</p> <pre><code>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)); } } </code></pre> <p>Output:</p> <pre><code>163 10100011 </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