NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Java Programming Help (conversions)

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
jxmst32



Joined: 17 Mar 2010
Posts: 4

PostPosted: Wed Mar 17, 2010 5:52 pm    Post subject: Java Programming Help (conversions) Reply with quote

I am currently writing a program in Netbeans 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. The code is below:

Code:


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);
            panel.add(b2d);
            }
        }

        static class Action2 implements ActionListener{
           
            public void actionPerformed (ActionEvent e){
            JFrame frame3 = new JFrame ("Decimal to Binary Conversion");
            frame3.setVisible(true);
            frame3.setSize(500,300);
            JLabel label = new JLabel ("Decimal");
            JPanel panel = new JPanel();
            JButton d2b = new JButton("Convert to Binary Now!");
            JTextField text = new JTextField(25);
            frame3.add(panel);
            panel.add(text);
            panel.add(label);
            panel.add(d2b);
               
            }
        }

         static class Action3 implements ActionListener{

            public void actionPerformed (ActionEvent e){
            JFrame frame4 = new JFrame ("Decimal to Hexadecimal Conversion");
            frame4.setVisible(true);
            frame4.setSize(500,300);
            JLabel label = new JLabel ("Decimal");
            JPanel panel = new JPanel();
            JButton d2h = new JButton("Convert to Hexadecimal Now!");
            JTextField text = new JTextField(25);
            frame4.add(panel);
            panel.add(text);
            panel.add(label);
            panel.add(d2h);
            }
        }
     static class Action4 implements ActionListener{

            public void actionPerformed (ActionEvent e){
            JFrame frame5 = new JFrame ("Hexadecimal to Decimal Conversion");
            frame5.setVisible(true);
            frame5.setSize(500,300);
            JLabel label = new JLabel ("Hexadecimal");
            JPanel panel = new JPanel();
            JButton h2d = new JButton("Convert to Decimal Now!");
            JTextField text = new JTextField(25);
            frame5.add(panel);
            panel.add(text);
            panel.add(label);
            panel.add(h2d);
            }
        }

    static class Action5 implements ActionListener{

            public void actionPerformed (ActionEvent e){
            JFrame frame6 = new JFrame ("Library of Boolean Algebra Rules");
            frame6.setVisible(true);
            frame6.setSize(500,300);
            JLabel label = new JLabel ("You clicked me!");
            JPanel panel = new JPanel();
            frame6.add(panel);
            panel.add(label);
            }
        }

     static class Action6 implements ActionListener{

            public void actionPerformed (ActionEvent e){
            JFrame frame7 = new JFrame ("1's Complement Conversion");
            frame7.setVisible(true);
            frame7.setSize(500,300);
            JLabel label = new JLabel ("You clicked me!");
            JPanel panel = new JPanel();
            frame7.add(panel);
            panel.add(label);
            }
        }

     static class Action7 implements ActionListener{

            public void actionPerformed (ActionEvent e){
            JFrame frame8 = new JFrame ("2's Complement Conversion");
            frame8.setVisible(true);
            frame8.setSize(500,300);
            JLabel label = new JLabel ("You clicked me!");
            JPanel panel = new JPanel();
            frame8.add(panel);
            panel.add(label);
            }
        }
    }






Last edited by jxmst32 on Wed Mar 17, 2010 6:01 pm; edited 1 time in total
Back to top
Glenn Holmer
Posted via mailing list.





PostPosted: Wed Mar 17, 2010 5:54 pm    Post subject: Java Programming Help (conversions) Reply with quote

On Wed, 2010-03-17 at 17:52 +0000, jxmst32 wrote:
Quote:
Please help me.

This is not the place to ask questions about how to code Java. This
mailing list is for questions about using NetBeans.

--
____________________________________________________________
Glenn Holmer address-removed
Software Engineer phone: 414-908-1809
Weyco Group, Inc. fax: 414-908-1601
Back to top
Javier.Ortiz
Posted via mailing list.





PostPosted: Wed Mar 17, 2010 5:59 pm    Post subject: Java Programming Help (conversions) Reply with quote

What is missing is an action listener (with the corresponding action) for the buttons in the sub menus.

This is one example:

http://www.techimo.com/forum/webmastering-programming/16409-binary-decimal-java.html

You should have the math equations somewhere, just trial and error. Also googling something like java binary decimal convertor (that gave me the link I gave you) might help.

Javier A. Ortiz Bultr
Back to top
jxmst32



Joined: 17 Mar 2010
Posts: 4

PostPosted: Wed Mar 17, 2010 7:29 pm    Post subject: Thanks Reply with quote

I took the action listeners off of the buttons because I cannot seem to figure out what code must be placed in there to convert user input from a JTextField to decimal/binary/hexadecimal javascript:emoticon('Sad')

Although I challenge myself, this seems to be way over my head.....
Back to top
Catherine Helwig
Posted via mailing list.





PostPosted: Wed Mar 17, 2010 8:53 pm    Post subject: Java Programming Help (conversions) Reply with quote

Keep the listeners on the buttons, when pressed get the appropriate value
in the JTexfield which is a String.

int result = Integer.parseInt(textfield.getText()); // all values from a
textfield are strings

Look at methods in the Integer class which will cover the conversions

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html


Then look at this java tip

http://www.java-tips.org/java-se-tips/java.lang/how-to-get-the-hexadecimal-v
alue-of-an-int.html


-----Original Message-----
From: jxmst32 [mailto:address-removed]
Sent: Wednesday, March 17, 2010 3:30 PM
To: address-removed
Subject: [nbusers] Java Programming Help (conversions)

I took the action listeners off of the buttons because I cannot seem to
figure out what code must be placed in there to convert user input from a
JTextField to decimal/binary/hexadecimal javascript:emoticon('Sad')

Although I challenge myself, this seems to be way over my head.....
Back to top
jxmst32



Joined: 17 Mar 2010
Posts: 4

PostPosted: Thu Mar 18, 2010 4:31 am    Post subject: Still Working On It Reply with quote

I'm getting closer, but I am still coming up with errors...This is the second button's Action Listener and the coding I have developed thus far:

Code:


 static class Action2 implements ActionListener{
           
            public void actionPerformed (ActionEvent e){
            JFrame frame3 = new JFrame ("Decimal to Binary Conversion");
            frame3.setVisible(true);
            frame3.setSize(500,300);
            JLabel label = new JLabel ("Decimal");
            JPanel panel = new JPanel();
            JButton d2b = new JButton("Convert to Binary Now!");
            JTextField text = new JTextField(25);
            frame3.add(panel);
            panel.add(text);
            panel.add(label);
            panel.add(d2b);

            class Actiond2b implements ActionListener{
            public void actionPerformed (ActionEvent e){
            int result = Integer.parseInt(JTextField.getText(Integer.toBinaryString(i)));
            }
            }
            d2b.addActionListener(new Actiond2b());


            }
        }

Back to top
Catherine Helwig
Posted via mailing list.





PostPosted: Thu Mar 18, 2010 11:45 am    Post subject: Java Programming Help (conversions) Reply with quote

First you need to separate out the code that creates the panels and
textfields into their own class. I don't know
What the rest of your code looks like but you should add the listener to the
button in this class.
d2b.addActionListener(action2Db).

The listener class ONLY implements ActionListener.

All the listener classes will be nested inside the main class where you
declare the components.



Add the listeners in the main class.

You should also get the contentPane and not add directly to the frame.
Since 1.5, java does this for you but
Should know what is going on first

Really there should be a separate main class but for now just get this
working

I don't know where you are putting the resulting binary string but to put it
back in the textfields use:

text.setText(result)

You really need to get a beginning java book or do the tutorials on Java on
the Sun site

-----Original Message-----
From: jxmst32 [mailto:address-removed]
Sent: Thursday, March 18, 2010 12:32 AM
To: address-removed
Subject: [nbusers] Java Programming Help (conversions)

I'm getting closer, but I am still coming up with errors...This is the
second button's Action Listener and the coding I have developed thus far:


Code:


class MainGUI
{
JTextField text;
JButton d2b; // declare all the
components - the text fields and buttons here.

public MainGUI
{
JFrame frame3 = new JFrame ("Decimal to Binary Conversion");
frame3.setVisible(true);
frame3.setSize(500,300);
JLabel label = new JLabel ("Decimal");
JPanel panel = new JPanel();

d2b = new JButton("Convert to Binary Now!"); // declare at the
top , instantiate inside the constructor
d2b.addActionListener(new Actiond2b()); // add listener here
text = new JTextField(25);

Container c = getContentPane();
panel.add(text);
panel.add(label);
panel.add(d2b);
c.add(panel);
} // close constructor

class Actiond2b implements ActionListener

{
public void actionPerformed (ActionEvent e)

{
int result =
Integer.parseInt(text.getText(Integer.toBinaryString(i)));

} // close actionperformed
} // close listener


} // close class
Back to top
Glenn Holmer
Posted via mailing list.





PostPosted: Thu Mar 18, 2010 12:47 pm    Post subject: Java Programming Help (conversions) Reply with quote

On Thu, 2010-03-18 at 07:43 -0400, Catherine Helwig wrote:
Quote:
You really need to get a beginning java book or do the tutorials on Java on
the Sun site

+1

Again, this list is not for learning Java. It is for learning how to
use NetBeans. The list has quite enough traffic as it is.

http://java.sun.com/developer/onlineTraining/index.jsp

--
____________________________________________________________
Glenn Holmer address-removed
Software Engineer phone: 414-908-1809
Weyco Group, Inc. fax: 414-908-1601
Back to top
Javier.Ortiz
Posted via mailing list.





PostPosted: Thu Mar 18, 2010 1:18 pm    Post subject: Java Programming Help (conversions) Reply with quote

As someone mentioned earlier this is not the place for java coding questions. Try StackOverflow for example.

Javier A. Ortiz Bultr
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo