In comp.lang.JAVA.advocacy, sam20e
<sam20e@xxxxxxxxxxx>
wrote in message
on Tue, 03 Jul 2007 10:34:32 -0700
<1183484072.076382.256950@xxxxxxxxxxx>:
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 have not tested this,
so there may be
syntax or other errors.
---8
< DecimalToBinary.JAVA >8---
import JAVA.awt.*;
import 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
widget s.
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 don't 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 supports 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@xxxxxxxxxxx
If your CPU can not stand the heat, get another fan.
--
Posted via a free Usenet account from
http://www.teranews.com
Blogs related to decimal to binary converter - JAVA
Converting Java data type
Catching Illegal data
conversion —-try{ i = Integer.parseInt(aString); } catch(NumberFormatException e){}.
Convert Date to String ————–// Get today’s date and create a
java.sql.Date from it
java.util.Date dtNow = new
java.util.Date();
...
Hex Counter and Cells Within Cells
It's a modernization of sorts of Alan Hensel's 1994 "
Decimal Counter" pattern -- an animated
Java version of which is now available on the Web [collidoscope.com]. Another set of patterns by the same author, using some of the same
...
UNIX: Some nifty shell tricks
These situations include base
conversion from one string to another (
decimal to hex, hex to
decimal,
decimal to octal, and so on), reading the keyboard while in a piped loop, subshell execution, inline input, executing a command once
...
Decimal to binary conversion and vice versa
Hi my assignment is to
convert the users input from
binary to
decimal or
decimal to binary depending on the users input. If the user inputs a
binary.
Base64 conversion
15 in 6 bit Ascii is a U. Take the next set of 6 digets of the
binary, 010011 converts to a hex value of 13 13 in 6 bit Ascii is an S Ok, so after that painful explaination. Are there any
Java utils that can do this for me? thanks.
How to convert the decimal values and convert into Binary (in ...
import
java.util.*; public class P4 { public static void main (String [] args) { short num; Scanner keyboard=new Scanner.