Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Java Conversions

Java Conversions

Scheduled Pinned Locked Moved Java
javaarchitecturehelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    James WG Murphy
    wrote on last edited by
    #1

    I am currently writing a program which will perform binary to decimal, decimal to binary, hexadecimal to decimal, decimal to hexadecimal conversions, 1's compliment, 2's compliment, and show the list of boolean algebra rules. I have created the GUI however, I cannot figure out how to make the Jbuttons generate the coding needed for a binary to decimal conversion of the input in a JTextField. Please help me. The code is below:

    package swing1;

    /**
    *
    * @author James
    */
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;
    import java.io.*;

    public class swing {

    public static void main (String\[\] args) {
    
        JFrame frame = new JFrame("Computer Architecture Conversions");
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE);
        
        JPanel panel = new JPanel ();
        frame.add(panel);
    
        JButton button = new JButton ("Binary to Decimal") ;
        JButton button2 = new JButton ("Decimal to Binary");
        JButton button3 = new JButton ("Decimal to Hexadecimal");
        JButton button4 = new JButton ("Hexadecimal to Decimal");
        JButton button5 = new JButton ("Library of Boolean Algebra Rules");
        JButton button6 = new JButton ("1's Complement");
        JButton button7 = new JButton ("2's Complement");
        panel.add(button);
        panel.add(button2);
        panel.add(button3);
        panel.add(button4);
        panel.add(button5);
        panel.add(button6);
        panel.add(button7);
        button.addActionListener(new Action());
        button2.addActionListener(new Action2());
        button3.addActionListener(new Action3());
        button4.addActionListener(new Action4());
        button5.addActionListener(new Action5());
        button6.addActionListener(new Action6());
        button7.addActionListener(new Action7());
    }
        static class Action implements ActionListener{
    
            public void actionPerformed (ActionEvent e){
            JFrame frame2 = new JFrame ("Binary to Decimal Conversion");
            frame2.setVisible(true);
            frame2.setSize(500,300);
            JLabel label = new JLabel ("Binary");
            JPanel panel = new JPanel();
            JButton b2d = new JButton("Convert to Decimal Now!");
            JTextField text = new JTextField(25);
            frame2.add(panel);
            panel.add(text);
            panel.add(label);
    
    L T 2 Replies Last reply
    0
    • J James WG Murphy

      I am currently writing a program which will perform binary to decimal, decimal to binary, hexadecimal to decimal, decimal to hexadecimal conversions, 1's compliment, 2's compliment, and show the list of boolean algebra rules. I have created the GUI however, I cannot figure out how to make the Jbuttons generate the coding needed for a binary to decimal conversion of the input in a JTextField. Please help me. The code is below:

      package swing1;

      /**
      *
      * @author James
      */
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.lang.*;
      import java.io.*;

      public class swing {

      public static void main (String\[\] args) {
      
          JFrame frame = new JFrame("Computer Architecture Conversions");
          frame.setVisible(true);
          frame.setSize(500,500);
          frame.setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE);
          
          JPanel panel = new JPanel ();
          frame.add(panel);
      
          JButton button = new JButton ("Binary to Decimal") ;
          JButton button2 = new JButton ("Decimal to Binary");
          JButton button3 = new JButton ("Decimal to Hexadecimal");
          JButton button4 = new JButton ("Hexadecimal to Decimal");
          JButton button5 = new JButton ("Library of Boolean Algebra Rules");
          JButton button6 = new JButton ("1's Complement");
          JButton button7 = new JButton ("2's Complement");
          panel.add(button);
          panel.add(button2);
          panel.add(button3);
          panel.add(button4);
          panel.add(button5);
          panel.add(button6);
          panel.add(button7);
          button.addActionListener(new Action());
          button2.addActionListener(new Action2());
          button3.addActionListener(new Action3());
          button4.addActionListener(new Action4());
          button5.addActionListener(new Action5());
          button6.addActionListener(new Action6());
          button7.addActionListener(new Action7());
      }
          static class Action implements ActionListener{
      
              public void actionPerformed (ActionEvent e){
              JFrame frame2 = new JFrame ("Binary to Decimal Conversion");
              frame2.setVisible(true);
              frame2.setSize(500,300);
              JLabel label = new JLabel ("Binary");
              JPanel panel = new JPanel();
              JButton b2d = new JButton("Convert to Decimal Now!");
              JTextField text = new JTextField(25);
              frame2.add(panel);
              panel.add(text);
              panel.add(label);
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      This is a continuation of your question below which you removed; please leave questions intact as the thread is now unintelligible to others viewing it for the first time. One thing that is quite noticeable is the commonality between all the different buttons - a classic example of the need for OO - but we can leave that till later. As a first step try removing all buttons 2 to 7 and just work on one button until you get the idea working properly. You can then add the others on top with a lot less work - and return to the OO model to do it. First thing is to decide what you want to do when the button is clicked, probably something like:

      • get the text from the 'input' text box
      • parse and validate the text to ensure it's a number
      • convert to an integer, based on the input format
      • reformat it in the output format desired
      • display on the 'output' label

      You could develop this code in a simple console app using the Integer class to parse the input and the printf() function to display the output. Once again, use the tutorials as they contain a wealth of information to help you learn some of the basic classes and functions.

      txtspeak is the realm of 9 year old children, not developers. Christian Graus

      1 Reply Last reply
      0
      • J James WG Murphy

        I am currently writing a program which will perform binary to decimal, decimal to binary, hexadecimal to decimal, decimal to hexadecimal conversions, 1's compliment, 2's compliment, and show the list of boolean algebra rules. I have created the GUI however, I cannot figure out how to make the Jbuttons generate the coding needed for a binary to decimal conversion of the input in a JTextField. Please help me. The code is below:

        package swing1;

        /**
        *
        * @author James
        */
        import javax.swing.*;
        import java.awt.*;
        import java.awt.event.*;
        import java.lang.*;
        import java.io.*;

        public class swing {

        public static void main (String\[\] args) {
        
            JFrame frame = new JFrame("Computer Architecture Conversions");
            frame.setVisible(true);
            frame.setSize(500,500);
            frame.setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE);
            
            JPanel panel = new JPanel ();
            frame.add(panel);
        
            JButton button = new JButton ("Binary to Decimal") ;
            JButton button2 = new JButton ("Decimal to Binary");
            JButton button3 = new JButton ("Decimal to Hexadecimal");
            JButton button4 = new JButton ("Hexadecimal to Decimal");
            JButton button5 = new JButton ("Library of Boolean Algebra Rules");
            JButton button6 = new JButton ("1's Complement");
            JButton button7 = new JButton ("2's Complement");
            panel.add(button);
            panel.add(button2);
            panel.add(button3);
            panel.add(button4);
            panel.add(button5);
            panel.add(button6);
            panel.add(button7);
            button.addActionListener(new Action());
            button2.addActionListener(new Action2());
            button3.addActionListener(new Action3());
            button4.addActionListener(new Action4());
            button5.addActionListener(new Action5());
            button6.addActionListener(new Action6());
            button7.addActionListener(new Action7());
        }
            static class Action implements ActionListener{
        
                public void actionPerformed (ActionEvent e){
                JFrame frame2 = new JFrame ("Binary to Decimal Conversion");
                frame2.setVisible(true);
                frame2.setSize(500,300);
                JLabel label = new JLabel ("Binary");
                JPanel panel = new JPanel();
                JButton b2d = new JButton("Convert to Decimal Now!");
                JTextField text = new JTextField(25);
                frame2.add(panel);
                panel.add(text);
                panel.add(label);
        
        T Offline
        T Offline
        TorstenH
        wrote on last edited by
        #3

        Lets start with simple things: 1. do not use main for greating a GUI - let another object do it for you:

        public static void main (String[] args) {
        View oView = new View();
        }

        2. do not set up several static classes with nearly same content. Again, use objects - in this case you need 1 class for the actions which you can use serveral times. Put your classes in seperate files!

        public class View extends JFrame{

        // members
        JPanel oPanel = new JPanel();
        JButton oButton = new JButton("click");

        // constructor
        public View(){
        oButton.addActionListener(oActionListener);
        oPanel.add(oButton);

        // anonymous class
        ActionListener oActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent oEvent) {
        if (oEvent.equals("something")){
        //do any();
        }
        else if(...){
        }
        }
        };

        this.add(oPanel);

        }

        You could even use a method for creating all the buttons - any line just needs to be writen once:

        JButton oButton = createButton("click");

        private JButton createButton(final String strCommand){
        JButton oButton = new JButton(strCommand);
        oButton.addActionListener(oActionListener);
        return oButton;
        }

        The ActionListener can again use some object which does the conversion... for information on how to convert things you should search the net. EDIT: please do not name classes by simular keywords - this will ruin your day someday! greets Torsten

        I never finish anyth...

        modified on Thursday, March 18, 2010 8:15 AM

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups