12. Übung - oth

Werbung
Programmieren in Java
12. Übung
Lösungen
1. Aufgabe
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class Ueb601 extends Frame
{
private LineNumberReader ein;
public Ueb601() throws IOException
{
FileDialog d = new FileDialog(
this,
"Welche Datei soll geoeffnet werden?");
d.setFile("*.java");
d.setDirectory(".");
// Aktuelles Verzeichnis
d.show();
String datei = "*.*";
if ((datei = d.getFile()) != null)
{
try {
ein = new LineNumberReader(new FileReader(datei));
}
catch ( FileNotFoundException a)
{
throw a;
}
// dateiName.setText(datei);
// verzeichnis.setText(d.getDirectory());
try
{ dateiDarstellen(); }
catch (IOException a ) { throw a; }
}
else { }
}
public void dateiDarstellen() throws IOException
{
try {
String s = new String();
TextArea textBereich = new TextArea(s,24,80);
while ((s = ein.readLine()) != null)
{
s = "/* " + ein.getLineNumber() + " */ " + s + '\n';
textBereich.append(s);
}
textBereich.setFont(new Font("Helvetica",Font.PLAIN,10));
textBereich.setEditable( false);
this.add("Center",textBereich);
}
catch (IOException e) { }
this.show();
}
public static void main(String args[]) throws IOException
{
1
Programmieren in Java
Frame f = new Ueb601();
f.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.setVisible(true);
}
}
2. Aufgabe
a)
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
public class C2F extends Frame
{
// Einlesen und Ausgeben von Zeichenketten ueber
// eine grafische Benutzeroberflaeche
private static TextField celsiusFeld = new TextField(10);
private static TextField fahrenhFeld = new TextField(10);
public static void main(String args[])
{
Frame fenster = new Frame("Umrechnung Fahrenheit - Celsius");
fenster.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Label celsiusFeldLabel = new Label("Celsius:");
Label fahrenhFeldLabel = new Label("Fahrenheit:");
celsiusFeld.setEditable( true);
celsiusFeld.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
Float fRef = null;
String s = celsiusFeld.getText();
try {
fRef = new Float(s);
}
catch (NumberFormatException nfe)
{
System.err.println(nfe.toString());
}
float gz = fRef.floatValue();
double temp;
temp = gz * 9.0 / 5.0 + 32.0;
String s1 = String.valueOf(temp);
fahrenhFeld.setText(s1);
}
});
fahrenhFeld.setEditable( true);
fahrenhFeld.addActionListener( new ActionListener()
2
Programmieren in Java
{
public void actionPerformed(ActionEvent ae)
{
Float fRef = null;
String s = fahrenhFeld.getText();
try {
fRef = new Float(s);
}
catch (NumberFormatException nfe)
{
System.err.println(nfe.toString());
}
float gz = fRef.floatValue();
double temp;
temp = (gz - 32.0) * 5.0 / 9.0;
String s1 = String.valueOf(temp);
celsiusFeld.setText(s1);
}
});
Panel panel = new Panel();
panel.add(celsiusFeldLabel);
panel.add(celsiusFeld);
panel.add(fahrenhFeldLabel);
panel.add(fahrenhFeld);
// Frame fenster = new Frame("Echo");
fenster.add(panel);
fenster.pack();
fenster.setVisible(true);
}
}
b)
import java.io.*;
public class FnachC
{
public static void main(String args[])
{
double fahr, celsius;
double uWert, oWert, schritt;
uWert = 0.0;
oWert = 300.0;
schritt = 20.0;
fahr = uWert;
try
{
PrintWriter aus = new PrintWriter(
new BufferedWriter(
new FileWriter("test.txt")));
while (fahr <= oWert)
{
celsius = 5.0 * (fahr - 32.0) / 9.0;
aus.println(fahr + " " + celsius);
fahr += schritt;
}
aus.close();
}
catch (IOException a)
{
System.out.println("Fehler: " + a);
System.exit(0);
}
}
3
Programmieren in Java
}
3. Aufgabe
import java.awt.*;
import java.awt.event.*;
public class Ueb603 extends Frame
{
Button b1 = new Button("rot");
Button b2 = new Button("blau");
Button b3 = new Button("gruen");
Button b4 = new Button("pink");
Button b5 = new Button("orange");
Button b6 = new Button("gelb");
Button b7 = new Button("cyan");
public Ueb603()
{
// setLayout(new FlowLayout(FlowLayout.LEFT,20,20));
setLayout(new FlowLayout());
setBackground(Color.lightGray);
b1.addActionListener( new B1L());
b2.addActionListener( new B2L());
b3.addActionListener( new B3L());
b4.addActionListener( new B4L());
b5.addActionListener( new B5L());
b6.addActionListener( new B6L());
b7.addActionListener( new B7L());
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
// pack();
}
public class B1L implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
setBackground(Color.red);
}
}
public class B2L implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
setBackground(Color.blue);
}
}
public class B3L implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
setBackground(Color.green);
}
}
public class B4L implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
4
Programmieren in Java
setBackground(Color.pink);
}
}
public class B5L implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
setBackground(Color.orange);
}
}
public class B6L implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
setBackground(Color.yellow);
}
}
public class B7L implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
setBackground(Color.cyan);
}
}
public static void main(String args[])
{
Frame f = new Ueb603();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
// f.setVisible(false);
System.exit(0);
}
});
f.setSize(100,150);
f.setVisible(true);
}
}
4. Aufgabe
import java.awt.*;
import java.awt.event.*;
public class Ueb703 extends Frame
{
TextField t = new TextField(100);
Checkbox
cb1 = new Checkbox("Checkbox 1"),
cb2 = new Checkbox("Checkbox 2",true),
cb3 = new Checkbox("Checkbox 3",false);
public Ueb703()
{
// setLayout(new FlowLayout(FlowLayout.LEFT,20,20));
setLayout(new BorderLayout());
Panel p = new Panel();
p.setLayout(new GridLayout(3,1));
setBackground(Color.lightGray);
t.setEditable(true);
cb1.addItemListener( new CBL());
cb2.addItemListener( new CBL());
5
Programmieren in Java
cb3.addItemListener( new CBL());
add(t);
p.add(cb1);
p.add(cb2);
p.add(cb3);
add("North",t);
add("Center",p);
}
public class CBL implements ItemListener
{
public void itemStateChanged(ItemEvent ie)
{
Checkbox cb = (Checkbox) ie.getItemSelectable();
t.setText(cb.getLabel() + ": " + cb.getState());
System.out.println(cb.getLabel() + ": " + cb.getState());
}
}
public static void main(String args[])
{
Ueb703 f = new Ueb703();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
// f.setVisible(false);
System.exit(0);
}
});
f.setSize(150,150);
f.setVisible(true);
}
}
5. Aufgabe
import java.awt.*;
import java.awt.event.*;
public class Ueb704 extends Frame
{
TextField t = new TextField(100);
CheckboxGroup
cbg = new CheckboxGroup();
public Ueb704()
{
// setLayout(new FlowLayout(FlowLayout.LEFT,20,20));
setLayout(new BorderLayout());
Panel p = new Panel();
p.setLayout(new GridLayout(3,1));
Checkbox cb1 = new Checkbox("rot",cbg,false);
Checkbox cb2 = new Checkbox("blau",cbg,false);
Checkbox cb3 = new Checkbox("gruen",cbg,false);
// Checkbox cb4 = new Checkbox("pink",cbg,false);
// Checkbox cb5 = new Checkbox("orange",cbg,false);
add("North",t);
cb1.addItemListener( new CBIL());
cb2.addItemListener( new CBIL());
cb3.addItemListener( new CBIL());
// cb4.addItemListener( new CBIL());
// cb5.addItemListener( new CBIL());
p.add(cb1);
p.add(cb2);
p.add(cb3);
6
Programmieren in Java
// p.add(cb4);
// p.add(cb5);
add("West",p);
}
public class CBIL implements ItemListener
{
public void itemStateChanged(ItemEvent ie)
{
Checkbox cb = (Checkbox) ie.getItemSelectable();
if (cb.getLabel() == "rot")
{
if (cb.getState())
{
t.setText("Radio Button rot");
setBackground(Color.red);
}
}
if (cb.getLabel() == "blau")
{
if (cb.getState())
{
t.setText("Radio Button blau");
setBackground(Color.blue);
}
}
if (cb.getLabel() == "gruen")
{
if (cb.getState())
{
t.setText("Radio Button gruen");
setBackground(Color.green);
}
}
/*
if (cb.getLabel() == "pink")
{
if (cb.getState())
{
t.setText("Radio Button pink");
setBackground(Color.pink);
}
}
if (cb.getLabel() == "orange")
{
if (cb.getState())
{
t.setText("Radio Button orange");
setBackground(Color.orange);
}
}
*/
}
}
public static void main(String args[])
{
Ueb704 f = new Ueb704();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
// f.setVisible(false);
System.exit(0);
}
7
Programmieren in Java
});
f.setBackground(Color.lightGray);
f.setSize(200,200);
f.setVisible(true);
}
}
6. Aufgabe
import java.awt.*;
import java.awt.event.*;
public class Ueb705 extends Frame
{
TextField t
= new TextField(100);
Choice auswahl = new Choice();
public Ueb705()
{
auswahl.addItem("rot");
auswahl.addItem("blau");
auswahl.addItem("gruen");
auswahl.addItem("pink");
auswahl.addItem("orange");
auswahl.addItem("gelb");
auswahl.addItem("cyan");
t.setEditable(false);
setLayout(new BorderLayout());
add("North",t);
add("Center",auswahl);
// pack();
auswahl.addItemListener( new AuswMerkmL());
}
class AuswMerkmL implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
t.setText("Index: " + auswahl.getSelectedIndex() +
" " + e.toString());
if (auswahl.getSelectedItem() == "rot")
{
setBackground(Color.red);
}
else if (auswahl.getSelectedItem() == "blau")
{
setBackground(Color.blue);
}
else if (auswahl.getSelectedItem() == " gruen")
{
setBackground(Color.green);
}
else if (auswahl.getSelectedItem() == " pink")
{
setBackground(Color.pink);
}
else if (auswahl.getSelectedItem() == "orange")
{
setBackground(Color.orange);
}
else if (auswahl.getSelectedItem() == "gelb")
{
setBackground(Color.yellow);
}
else if (auswahl.getSelectedItem() == " cyan")
{
8
Programmieren in Java
setBackground(Color.cyan);
}
}
}
public static void main(String args[])
{
Ueb705 f = new Ueb705();
f.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.setBackground(Color.lightGray);
f.setSize(200,100);
f.setVisible(true);
}
}
7. Aufgabe
import java.awt.*;
import java.awt.event.*;
public class Ueb706 extends Frame
{
String[] farben = {"rot","blau","gruen","pink","orange",
" gelb","cyan"};
List lst = new List(6,true);
Button b = new Button("Test");
TextArea t = new TextArea(farben.length,10);
int zaehler = 0;
public Ueb706()
{
setLayout(new GridLayout(1,2));
// setLayout(new FlowLayout());
Panel p1 = new Panel();
p1.setLayout(new BorderLayout());
Panel p2 = new Panel();
t.setEditable(false);
for (int i = 0; i < 4; i++)
lst.addItem(farben[zaehler++]);
p2.add(t);
// add("North",t);
p1.add("Center",lst);
p1.add("South",b);
// add(t);
// add(lst);
// add(b);
add(p1);
add(p2);
lst.addItemListener(new LL());
b.addActionListener(new BL());
}
public class LL implements ItemListener
{
public void itemStateChanged(ItemEvent ie)
{
t.setText(" ");
String[] items = lst.getSelectedItems();
for (int i = 0; i < items.length; i++)
t.append(items[i] + "\n");
9
Programmieren in Java
}
}
class BL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (zaehler < farben.length)
{
lst.addItem(farben[zaehler++],0);
}
}
}
public static void main(String args[])
{
Ueb706 f = new Ueb706();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
// f.setVisible(false);
System.exit(0);
}
});
f.setBackground(Color.lightGray);
f.setSize(240,160);
f.setVisible(true);
}
}
10
Herunterladen