Package test;
Import java. awt. Container;
Import java. awt. Rectangle;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. awt. event. KeyEvent;
Import java. awt. event. KeyListener;
Import java. awt. event. WindowAdapter;
Import java. awt. event. javaswevent;
Import javax. swing. BorderFactory;
Import javax. swing. JButton;
Import javax. swing. JFrame;
Import javax. swing. JLabel;
Import javax. swing. JOptionPane;
Import javax. swing. JPanel;
Import javax. swing. JTextField;
// Import javax. swing. event. deleentevent;
// Put the program in a file with only one class
Public class Computer extends JFrame implements KeyListener, ActionListener // text listener and action listener are required
{
Public static final long serialVersionUID = 1L;
// Declare controls and variables
Private int temp; // The intermediate variable for Numerical Conversion
Private boolean flag = false; // button flag
Private JPanel; // a panel container
Private JButton changeButton; // declare the conversion button
Private JLabel [] title = new JLabel [4]; // label Group
Private JTextField [] value = new JTextField [4]; // text box Group
// Construct
Public Computer (){
Super ("Java programming"); // JFrame Parameters
Init ();
}
Private void init (){
Container content = getContentPane (); // Container object Declaration
Content. setLayout (null); // the layout is empty.
Panel = new JPanel (null); // panel Definition
Panel. setBounds (new Rectangle (0, 0,300,350); // defines the display area of the panel.
Panel. setBorder (BorderFactory. createTitledBorder ("hexadecimal converter"); // panel title
Title [0] = new JLabel ("binary"); // label
Title [1] = new JLabel (" ");
Title [2] = new JLabel ("decimal ");
Title [3] = new JLabel ("hexadecimal ");
For (int I = 0; I <title. length; I ++ ){
Title [I]. setbounds (New rectangle (10, 20 + I * 60, 70, 35 ));
}
For (INT I = 0; I <value. length; I ++ ){
Value [I] = new jtextfield (20 );
Value [I]. setbounds (New rectangle (80, 20 + I * 60,180, 25 ));
Value [I]. addkeylistener (this );
}
Changebutton = new jbutton ("Conversion"); // conversion button
Changebutton. setbounds (New rectangle (60,260,210, 25 ));
Changebutton. addactionlistener (this );
For (INT I = 0; I <title. length; I ++)
// Add controls to the Panel
Panel. Add (title [I]);
For (INT I = 0; I <value. length; I ++)
Panel. Add (value [I]);
Panel. Add (changebutton );
Content. Add (panel); // Add a panel to the content
Setsize (300,350); // window size
This. setlocationrelativeto (null); // center
Setresizable (false); // disable window size change
Setvisible (true); // display
}
Public void keytyped (keyevent e ){
}
Public void keypressed (keyevent e ){
If (E. getsource () = value [0]) {
Clearothers (0 );
} Else if (E. getsource () = value [1]) {
Clearothers (1 );
} Else if (E. getsource () = value [2]) {
Clearothers (2 );
} Else if (E. getsource () = value [3]) {
Clearothers (3 );
}
}
Public void keyreleased (keyevent e ){
If (value [0]. gettext (). Length ()! = 0) {// because the four text boxes have different actions after listening, you must identify the changes in each text box separately.
Try {
Temp = integer. parseint (value [0]. gettext (), 2); // second to ten functions
Clearothers (0 );
Flag = true;
} Catch (numberformatexception ee ){
Joptionpane. showmessagedialog (null, "invalid input data", "message prompt ",
JOptionPane. PLAIN_MESSAGE );
ClearAll ();
}
} Else if (value [1]. getText (). length ()! = 0 ){
Try {
Temp = Integer. parseInt (value [1]. getText (), 8); // eight to ten functions
ClearOthers (1 );
Flag = true;
} Catch (NumberFormatException ee ){
JOptionPane. showMessageDialog (null, "invalid input data", "message prompt ",
JOptionPane. PLAIN_MESSAGE );
ClearAll ();
}
} Else if (value [2]. getText (). length ()! = 0 ){
Try {
Temp = Integer. parseInt (value [2]. getText (); // convert string to int Function
ClearOthers (2 );
Flag = true;
} Catch (NumberFormatException ee ){
JOptionPane. showMessageDialog (null, "invalid input data", "message prompt ",
JOptionPane. PLAIN_MESSAGE );
ClearAll ();
}
} Else if (value [3]. getText (). length ()! = 0 ){
Try {
Temp = Integer. parseInt (value [3]. getText (), 16); // convert sixteen to ten functions
ClearOthers (3 );
Flag = true;
} Catch (NumberFormatException ee ){
JOptionPane. showMessageDialog (null, "invalid input data", "message prompt ",
JOptionPane. PLAIN_MESSAGE );
ClearAll ();
}
}
}
Private void clearAll (){
For (int I = 0; I <value. length; I ++ ){
Value [I]. setText ("");
}
}
Private void clearOthers (int t ){
For (int I = 0; I <value. length; I ++ ){
If (I! = T ){
Value [I]. setText ("");
}
}
}
Public void actionreceivmed (ActionEvent e) // implemented after the button listens to the action
{
If (e. getSource () = changeButton ){
If (flag ){
Value [0]. setText (Integer. toBinaryString (temp); // second-to-second function
Value [1]. setText (Integer. toOctalString (temp); // 10 to 8 functions
Value [2]. setText (Integer. toString (temp); // convert int to string Function
Value [3]. setText (Integer. toHexString (temp); // The Ten to sixteen functions.
Flag = false;
Changebutton. settext ("clear ");
} Else if (! Flag) // clear
{
For (INT I = 0; I <4; I ++)
Value [I]. settext ("");
Temp = 0;
Changebutton. settext ("Conversion ");
}
}
}
Public static void main (string [] ARGs ){
Computer Cr = new computer (); // window generated after being constructed
Cr. addwindowlistener (New windowadapter () // close button response
{
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
});
}
}