Today im going to release my number system calculator.
I hope it is pretty useful for you
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.lang.Object.*; import java.lang.Math.*; //Zahlenrechner (Binär - Octal - Dezimal - DuoDezimal - HexaDezimal) class cZahlenRechner extends JFrame implements ActionListener { //globale Variablen JLabel BinaryLabel, OctalLabel, DecimalLabel, DuoDecimalLabel, HexaDecimalLabel = null; JButton BinaryButton, OctalButton, DecimalButton, DuoDecimalButton, HexaDecimalButton = null; JTextField BinaryTextField, OctalTextField, DecimalTextField, DuoDecimalTextField, HexaDecimalTextField = null; Dimension ScreenSize = Toolkit.getDefaultToolkit ().getScreenSize (); int iWindowX = 400; int iWindowY = 210; //Main Funktion //Hier wird lediglich eine neue Instanz auf die ZahlenRechner Klasse erstellt (dadurch der Konstruktor automatisch aufgerufen //Keine Parameter da für das Programm keine Variablen schon vor Programmstart vordefiniert werden müssen public static void main (String[] args) { new cZahlenRechner (); //Instanz auf die eigene Klasse weil die main static ist } //End main //Konstruktor //Hier werden die einzelnen GUI Elemente erzeugt cZahlenRechner () { //Hier werden die Wichtigsten Variablen für das Fenster gesetzt setLayout (null); setSize (iWindowX, iWindowY); setLocation (((ScreenSize.width - iWindowX) / 2), ((ScreenSize.height - iWindowY) / 2)); //Mitte des Bildschirms //Labels erstellen und adden BinaryLabel = new JLabel ("Binary:"); BinaryLabel.setBounds (10, 10, 80, 20); add (BinaryLabel); OctalLabel = new JLabel ("Octal:"); OctalLabel.setBounds (10, 40, 80, 20); add (OctalLabel); DecimalLabel = new JLabel ("Decimal:"); DecimalLabel.setBounds (10, 70, 80, 20); add (DecimalLabel); DuoDecimalLabel = new JLabel ("DuoDecimal:"); DuoDecimalLabel.setBounds (10, 100, 80, 20); add (DuoDecimalLabel); HexaDecimalLabel = new JLabel ("HexaDecimal:"); HexaDecimalLabel.setBounds (10, 130, 80, 20); add (HexaDecimalLabel); //TextFelder erstellen und adden BinaryTextField = new JTextField (""); BinaryTextField.setBounds (110, 10, 150, 20); add (BinaryTextField); OctalTextField = new JTextField (""); OctalTextField.setBounds (110, 40, 150, 20); add (OctalTextField); DecimalTextField = new JTextField (""); DecimalTextField.setBounds (110, 70, 150, 20); add (DecimalTextField); DuoDecimalTextField = new JTextField (""); DuoDecimalTextField.setBounds (110, 100, 150, 20); add (DuoDecimalTextField); HexaDecimalTextField = new JTextField (""); HexaDecimalTextField.setBounds (110, 130, 150, 20); add (HexaDecimalTextField); //Button erstellen und adden BinaryButton = new JButton ("Rechne!"); BinaryButton.addActionListener (this); BinaryButton.setBounds (280, 10, 80, 20); add (BinaryButton); OctalButton = new JButton ("Rechne!"); OctalButton.addActionListener (this); OctalButton.setBounds (280, 40, 80, 20); add (OctalButton); DecimalButton = new JButton ("Rechne!"); DecimalButton.addActionListener (this); DecimalButton.setBounds (280, 70, 80, 20); add (DecimalButton); DuoDecimalButton = new JButton ("Rechne!"); DuoDecimalButton.addActionListener (this); DuoDecimalButton.setBounds (280, 100, 80, 20); add (DuoDecimalButton); HexaDecimalButton = new JButton ("Rechne!"); HexaDecimalButton.addActionListener (this); HexaDecimalButton.setBounds (280, 130, 80, 20); add (HexaDecimalButton); //sichtbar machen setVisible (true); } //End Konstruktor //Der ActionListener //Diese Funktion reagiert wenn ein Button gedrückt wird public void actionPerformed (ActionEvent e) { if (e.getSource () == BinaryButton) { OctalTextField.setText (Calculator (BinaryTextField.getText (), 8, 2.0)); DecimalTextField.setText (Calculator (BinaryTextField.getText (), 10, 2.0)); DuoDecimalTextField.setText (Calculator (BinaryTextField.getText (), 12, 2.0)); HexaDecimalTextField.setText (Calculator (BinaryTextField.getText (), 16, 2.0)); } else if (e.getSource () == OctalButton) { BinaryTextField.setText (Calculator (OctalTextField.getText (), 2, 8.0)); DecimalTextField.setText (Calculator (OctalTextField.getText (), 10, 8.0)); DuoDecimalTextField.setText (Calculator (OctalTextField.getText (), 12, 8.0)); HexaDecimalTextField.setText (Calculator (OctalTextField.getText (), 16, 8.0)); } else if (e.getSource () == DecimalButton) { BinaryTextField.setText (DecimalWandler (DecimalTextField.getText (), 2)); OctalTextField.setText (DecimalWandler (DecimalTextField.getText (), 8)); DuoDecimalTextField.setText (DecimalWandler (DecimalTextField.getText (), 12)); HexaDecimalTextField.setText (DecimalWandler (DecimalTextField.getText (), 16)); } else if (e.getSource () == DuoDecimalButton) { BinaryTextField.setText (Calculator (DuoDecimalTextField.getText (), 2, 12.0)); OctalTextField.setText (Calculator (DuoDecimalTextField.getText (), 8, 12.0)); DecimalTextField.setText (Calculator (DuoDecimalTextField.getText (), 10, 12.0)); HexaDecimalTextField.setText (Calculator (DuoDecimalTextField.getText (), 16, 12.0)); } else if (e.getSource () == HexaDecimalButton) { BinaryTextField.setText (Calculator (HexaDecimalTextField.getText (), 2, 16.0)); OctalTextField.setText (Calculator (HexaDecimalTextField.getText (), 8, 16.0)); DecimalTextField.setText (Calculator (HexaDecimalTextField.getText (), 10, 16.0)); DuoDecimalTextField.setText (Calculator (HexaDecimalTextField.getText (), 12, 16.0)); } } //End ActionListener //Wandelt einen ZahlenTyp in einen anderen ZahlenTyp private String Calculator (String sCount, int iSize, double dSize) { //Locale Variablen String sNewCount = ""; int iLength = sCount.length (); char [] chCount = sCount.toCharArray (); int iDecimal = 0; String sDecimal = ""; double iCounter = 0.0; int iTemp = 0; //Jeden char entsprechend umschreiben for (int i = iLength - 1 ; i >= 0 ; i--) { switch (chCount [i]) { case ('A' | 'a'): iTemp = 10; break; case ('B' | 'b'): iTemp = 11; break; case ('C' | 'c'): iTemp = 12; break; case ('D' | 'd'): iTemp = 13; break; case ('E' | 'e'): iTemp = 14; break; case ('F' | 'f'): iTemp = 15; break; default: iTemp = (int)(chCount [i]) - 48; //Cast Wert entspricht nicht dem richtigen! } iDecimal += (iTemp * (int)(Math.pow (dSize, iCounter))); iCounter ++; } sDecimal += iDecimal; if (iSize != 10) { sNewCount = DecimalWandler (sDecimal, iSize); } else { sNewCount = sDecimal; } return sNewCount; } //End BinaryWandler //Wandelt eine DezimalZahl in einen anderen ZahlenTyp private String DecimalWandler (String sDecimal, int iSize) { //Locale Variablen String sNewCount = ""; int iDecimal = Integer.parseInt (sDecimal); int [] iToChange = new int [128]; int iRest = 0; int i = 0; do { iRest = 0; iRest = iDecimal%iSize; iToChange [i] = iRest; iDecimal /= iSize; i++; } while (iDecimal != 0); for (int j = i - 1 ; j >= 0 ; j--) { switch (iToChange [j]) { case (10): sNewCount += 'A'; break; case (11): sNewCount += 'B'; break; case (12): sNewCount += 'C'; break; case (13): sNewCount += 'D'; break; case (14): sNewCount += 'E'; break; case (15): sNewCount += 'F'; break; default: sNewCount += iToChange [j]; } } return sNewCount; } //End DecimalWandler } //End cZahlenWandler Class