Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Advocacy > Re: decimal to ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 2 Topic 2298 of 2478
Post > Topic >>

Re: decimal to binary converter - java

by The Ghost In The Machine <ewill@[EMAIL PROTECTED] > Jul 3, 2007 at 12:58 PM

In comp.lang.java.advocacy, sam20e
<sam20e@[EMAIL PROTECTED]
>
 wrote
on Tue, 03 Jul 2007 10:34:32 -0700
<1183484072.076382.256950@[EMAIL PROTECTED]
>:
> hi i need a help... i wanna write a java programmee to display the
> binary number of the entered decimal number.
>
> for ex : in t text area u should type a desimal number and press
> "Convert" button to display its binary in the next txt field
>
> need help mates, i need to write a programme.. i need the "JAVA CODES"
>
> p.s : should use only 1 statement; stack
>

Uh...good luck on the 1 statement bit, but the rest is
straightforward enough.  Disclaimer: I've not tested this,
so there may be syntax or other errors.

---8< DecimalToBinary.java  >8---

im****t java.awt.*;
im****t javax.swing.*;

public class DecimalToBinary extends JFrame
{
  JTextField input;
  JLabel output;

  public static void main(String[] args)
  {
    DecimalToBinary dtob = new DecimalToBinary();
    dtob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    dtob.setSize(new Dimension(640,480));
    dtob.setVisible(true);
  }

  public DecimalToBinary()
  {
    super();

    setTitle("Decimal To Binary Converter");
    getContentPane().setLayout(new BorderLayout());
    JPanel panel = createConversionPanel();
    getContentPane().add(panel, BorderLayout.CENTER);
  }

  public JPanel createConversionPanel()
  {
    JPanel panel = new JPanel();
    final DecimalToBinary me = this; // so that Listener can see it
    panel.setLayout(new GridLayout(2,2)); // simpler than GridBagLayout
    panel.add(new JLabel("Input:"));
    input = new JTextField();
    panel.add(input);
    input.addActionListener(new ActionListener(){
        void actionPerformed(ActionEvent ev)
	{
	  me.output.setText(me.convert(input.getText()));
	}
      }
    );
    panel.add(new JLabel("Output:"));
    output = new JLabel();
    panel.add(output);
  }

  public String convert(String inp)
  {
    try
    {
      // the meat of the conversion; all the rest is setup
      // for these two statements
      int i = Integer.parseInt(inp);
      return Integer.toBinaryString(i);
    }
    catch(Exception e)
    {
      // some sort of problem; return a message
      return "'" + inp + "': "
        + e.getClass().getName() + ":" + e.getMessage();
    }
  }
}

---8<  >8---

The implementation is very straightforward.  First, a frame
is set up with a panel; the panel contains four widgets.
Two of them are static labels; the third ('input') is a
text input widget with a listener, and the fourth is a
label whose value is changed whenever the ActionListener
is called.  (The ActionListener on a text field is called
when the user presses Enter when that widget has the focus.
The usage model here: click in the field, type in a number,
press Enter.  The binary result should display in the
output.)

The addition of an explicit Convert button is left to the
reader but is easily done; note that one should add an
ActionListener to both widgets in that case.  (A listener
can be shared.)

Beautifying the layout is also left to the reader;
GridLayout() has the habit of making every widget the
same size -- not at all desirable.  GridbagLayout() may
be preferable for the purpose of aesthetics but is more
complicated to set up.  FlowLayout() is pretty dumb but
works for dialog button rows.

Colorization, font bolding, font italicizing, and other
such niceties are also possible.

There are other Java widget sets available.

- Swing, the model I'm showing here.  The Swing GUI
  is distinctive but many do not like it, and it probably
  won't match other graphical elements on one's desktop,
  though it does offer several look and feels.

- AWT, an ancient and now rather creaky widget set that
  tends to implement each button, text field, frame, etc.
  using an external C/C++ widget set, traditionally Motif
  or Windows32.  Nowadays Gtk is used.  The AWT GUI won't
  match other elements either.

- SWT, an IBM offering that attempts to resurrect the
  native look and feel of the platform on which Java is
  running (it sup****ts at least three: Windows, Linux,
  and OSX).  It is more complicated to set up from a
  programming standpoint but can be very powerful, and
  includes an embedded Browser widget, either Internet
  Explorer (on Windows only) or Mozilla Firefox.

-- 
#191, ewill3@[EMAIL PROTECTED]
 your CPU can't stand the heat, get another fan.

-- 
Posted via a free Usenet account from http://www.teranews.com
 




 2 Posts in Topic:
decimal to binary converter - java
sam20e <sam20e@[EMAIL   2007-07-03 10:34:32 
Re: decimal to binary converter - java
The Ghost In The Machine   2007-07-03 12:58:48 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Dec 4 1:20:50 CST 2008.