Grundlagen der Programmierung 2 SS 2005 - Datei BinaryConverter.java
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class BinaryConverter extends JFrame
{
private JButton b7, b6, b5, b4, b3, b2, b1, b0;
private JLabel l7, l6, l5, l4, l3, l2, l1,l0;
private JPanel p7, p6, p5, p4, p3, p2, p1, p0;
private JLabel lErgebnis;
private boolean vSet7, vSet6, vSet5, vSet4, vSet3, vSet2, vSet1, vSet0;
BinaryConverter(String s)
{
super(s);
Container content = getContentPane();
content.setLayout(new GridLayout(9, 1));
b7 = new JButton("Toggle");
b6 = new JButton("Toggle");
b5 = new JButton("Toggle");
b4 = new JButton("Toggle");
b3 = new JButton("Toggle");
b2 = new JButton("Toggle");
b1 = new JButton("Toggle");
b0 = new JButton("Toggle");
l7 = new JLabel("");
l6 = new JLabel("");
l5 = new JLabel("");
l4 = new JLabel("");
l3 = new JLabel("");
l2 = new JLabel("");
l1 = new JLabel("");
l0 = new JLabel("");
p7 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p6 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p5 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p4 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p0 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p7.add(l7); p7.add(b7);
p6.add(l6); p6.add(b6);
p5.add(l5); p5.add(b5);
p4.add(l4); p4.add(b4);
p3.add(l3); p3.add(b3);
p2.add(l2); p2.add(b2);
p1.add(l1); p1.add(b1);
p0.add(l0); p0.add(b0);
content.add(p7);
content.add(p6);
content.add(p5);
content.add(p4);
content.add(p3);
content.add(p2);
content.add(p1);
content.add(p0);
lErgebnis = new JLabel("");
lErgebnis.setFont(new Font("SansSerif", Font.BOLD, 20));
content.add(lErgebnis);
BinaryListener binListener = new BinaryListener();
b7.addActionListener(binListener);
b6.addActionListener(binListener);
b5.addActionListener(binListener);
b4.addActionListener(binListener);
b3.addActionListener(binListener);
b2.addActionListener(binListener);
b1.addActionListener(binListener);
b0.addActionListener(binListener);
setSize(200, 300);
setLocation(10, 10);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setzeLabel();
this.setVisible(true);
}
void setzeLabel()
{
l7.setText("2^7: " + btoc(vSet7));
l6.setText("2^6: " + btoc(vSet6));
l5.setText("2^5: " + btoc(vSet5));
l4.setText("2^4: " + btoc(vSet4));
l3.setText("2^3: " + btoc(vSet3));
l2.setText("2^2: " + btoc(vSet2));
l1.setText("2^1: " + btoc(vSet1));
l0.setText("2^0: " + btoc(vSet0));
int dezimal = btoi(vSet7) * 128 + btoi(vSet6) * 64 + btoi(vSet5) * 32 + btoi(vSet4) * 16
+ btoi(vSet3) * 8 + btoi(vSet2) * 4 + btoi(vSet1) * 2 + btoi(vSet0);
lErgebnis.setText("Decimal: " + dezimal);
}
static char btoc(boolean b)
{ // boolean to char
if (b) return '1';
else return '0';
}
static char btoi(boolean b)
{ // boolean to int
if (b) return 1;
else return 0;
}
// Innere Klasse fuer den eingebetteten Listener:
class BinaryListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Das gerade gewaehlte Feld:
if (e.getSource() == b7) vSet7 = !vSet7;
else if (e.getSource() == b6) vSet6 = !vSet6;
else if (e.getSource() == b5) vSet5 = !vSet5;
else if (e.getSource() == b4) vSet4 = !vSet4;
else if (e.getSource() == b3) vSet3 = !vSet3;
else if (e.getSource() == b2) vSet2 = !vSet2;
else if (e.getSource() == b1) vSet1 = !vSet1;
else if (e.getSource() == b0) vSet0 = !vSet0;
setzeLabel();
}
}
public static void main(String[] args)
{
BinaryConverter d = new BinaryConverter("Byte -> Decimal");
}
}
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 13.05.2005


