Eine HTML Datei in eine Java Datei umschreiben

3rdNuss

Mitglied
Hallo,
Also zuerst einmal ich bin neu hier im Forum und hab gleich mal ein großes Problem.

Und zwar hab ich eine .html Datei die ich in eine Java Datei umschreiben möchte.
Die .html Datei besteht eigentlich nur aus einer großen Tabelle die in mehrere kleine Tabellen unterteilt ist. Die mittlere Tabelle ist die wichtigste Tabelle von allen. Dort befinden sich mehrere "Textboxen" in die man etwas eintragen kann. Am Ende der mittleren Tabelle befindet sich noch ein Button (Der Button mit seinen genaueren Funktionen ist vorerst unwichtig).

Damit ihr das auch besser nachvollziehen könnt hab ich hier mal ein Bild hochgeladen.

attachment.php


Die schwarzen Linie sind die Aufteilung meiner Tabelle (Das in der Mitte ist natürlich auch eine Tabelle bestehend aus 7 Zeilen und 2 Spalten)

Wundert euch nicht über die komische Aufteilung. Am Ende werden noch ein paar Bilder etc. eingefügt.

Mein Problem ist jetzt das ich keine Ahnung hab wie ich diese Datei in eine .java Datei umschreiben kann. Am Ende möchte ich weiterhin eine .html Datei haben die ich öffnen kann mit dem einzigen unterschied das die Tabelle nicht in .html sondern in .java geschrieben ist. Da ich selbst keine Ahnung hab wie ich das schaffe, hoffe ich mal das jemand von euch mir weiter helfen kann.

Ich möchte eigentlich nur von euch ein paar Ansätze wissen damit ich es selbst umschreiben kann.


Gruß
Erdnuss
 
Zuletzt bearbeitet von einem Moderator:
Vorneweg:
Dir ist klar, dass HTML eine Markup language ist und Java eine Programming language?
Dir sind die Implikationen daraus bewusst?

Dieser Satz zum Beispiel macht nur wenig Sinn:
3rdNuss hat gesagt.:
Am Ende möchte ich weiterhin eine .html Datei haben die ich öffnen kann mit dem einzigen unterschied das die Tabelle nicht in .html sondern in .java geschrieben ist.

-

Hier ein paar Stichworte für das Design:
  • Code:
    LayoutManager
    (für die Positionen und Grössen der Tabellen)
    • Würde ich zuerst mit
      Code:
      BorderLayout
      versuchen, könnte aber sein, dass das nicht gefällt
    • Der nächste Versuche wäre eine Mischung aus
      Code:
      FlowLayout
      ,
      Code:
      GridLayout
      und
      Code:
      BorderLayout
    • Ansonsten eben mit einem
      Code:
      GridBagLayout
      feste Werte setzen
    • (Neu entdeckt: MigLayout, glaube sogar von hier irgendwo)
  • Code:
    JComponent
    (für die mittlere Tabelle, mit swing)
    • Code:
      JLabel
      , für die kleinen Text-Komponenten
    • Code:
      JTextField
      , für die Eingabe-Felder
    • Code:
      JButton
      , für den Knopf

---

3rdNuss hat gesagt.:
Und zwar hab ich eine .html Datei die ich in eine Java Datei umschreiben möchte.
Wieso?
Was erhoffst du dir davon?
 
Also zuerst einmal Danke für die Schnell Hilfe ich werde mich morgen gleich mal dran setzen und das was du mir gesagt hast versuchen um zusetzen.
----------------
Das war vielleicht ein Fehler von mir. Ich hab mich glaub ich ein bisschen undeutlich ausgedrückt.

Am Ende möchte ich weiterhin eine .html Datei haben die ich öffnen kann mit dem einzigen unterschied das die Tabelle nicht in .html sondern in .java geschrieben ist.

Damit wollte ich eigentlich Bezug auf die Applet Funktion beziehen.

Einfaches Beispiel was ich mal gemacht hab:

Java:
import java.applet.*;
import java.awt.*;
public class Hallo01 extends java.applet.Applet
{
	public void paint(Graphics g)
	{
		g.drawString("Hallo, viel Erfolg mit Java!",100,50];
	}
}
[/Java]

[HTML]
<html>
<head>
	<title>
	Hallo Applet
	</title>
</head>
<body>
<applet code="Hallo01.class"width=350 height=90

</applet>
</body>
</html>
[/HTML]

Ich bin mir jetzt nicht sicher ob ich das ganze auf mit einer Tabelle umsetzen kann. Ich hoffe du verstehst jetzt ungefähr was ich meine :).
----------------
[QUOTE]
Wieso?
Was erhoffst du dir davon? 
[/QUOTE]

Das ganze dient eigentlich nur als ein Training für mich. Ich war einfach nur neugierig ob das ganze Funktioniert und ich in der Lage bin das um zusetzen.
----------------
Ich werd morgen berichten wie weit ich es geschaft habe.

Gruß
Erdnuss
 
Soooooo,
Ich hab mich heute Mittag mal 2 Stunden hingesetzt und hab mit den Tipps von Suinos und der Hilfe von google etwas zusammen gewürfelt.
Das was ich mir da zusammen gewürfelt hab sieht irgendwie kompliziert und viel aus. Also ich bin mir bei der Sache nicht 100% sicher.

Ich würde mich sehr über Vorschläge und Verbesserungen freuen. 🙂

[JAVA=1]
import java.awt.*;
import javax.swing.*;


public class Test {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;


public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}

JLabel label;
JTextField textfield;
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
c.fill = GridBagConstraints.HORIZONTAL;
}

label = new JLabel("Bild");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;
c.gridx = 2;
c.gridy = 0;
pane.add(label, c);

label = new JLabel("");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
pane.add(label, c);

label = new JLabel("Anzahl");
c.fill = GridBagConstraints.CENTER;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
pane.add(label, c);

label = new JLabel("Produkt");
c.fill = GridBagConstraints.CENTER;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 1;
pane.add(label, c);

label = new JLabel("");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 3;
c.gridy = 1;
pane.add(label, c);

textfield = new JTextField("");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 10;
c.weightx = 0.5;
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 2;
pane.add(textfield, c);

textfield = new JTextField("");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 10;
c.weightx = 0.5;
c.gridwidth = 1;
c.gridx = 2;
c.gridy = 2;
pane.add(textfield, c);

textfield = new JTextField("");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 10;
c.weightx = 0.5;
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 3;
pane.add(textfield, c);

textfield = new JTextField("");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 10;
c.weightx = 0.5;
c.gridwidth = 1;
c.gridx = 2;
c.gridy = 3;
pane.add(textfield, c);

textfield = new JTextField("");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 10;
c.weightx = 0.5;
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 4;
pane.add(textfield, c);

textfield = new JTextField("");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 10;
c.weightx = 0.5;
c.gridwidth = 1;
c.gridx = 2;
c.gridy = 4;
pane.add(textfield, c);

textfield = new JTextField("");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 10;
c.weightx = 0.5;
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 5;
pane.add(textfield, c);

textfield = new JTextField("");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 10;
c.weightx = 0.5;
c.gridwidth = 1;
c.gridx = 2;
c.gridy = 5;
pane.add(textfield, c);

textfield = new JTextField("");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 10;
c.weightx = 0.5;
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 6;
pane.add(textfield, c);

textfield = new JTextField("");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 10;
c.weightx = 0.5;
c.gridwidth = 1;
c.gridx = 2;
c.gridy = 6;
pane.add(textfield, c);

button = new JButton("Kaufen");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 10;
c.gridx = 2;
c.gridwidth = 1;
c.gridy = 7;
pane.add(button, c);
}

private static void createAndShowGUI() {
JFrame frame = new JFrame("Tabelle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
[/code]

und so sieht's aus:



Meine Frage:
  • Kann Ich das JTextField auf eine bestimmte größe festlegen?
  • Wie bekomm ich einen kleinen Abstand zwischen dem letzten JTextField und dem JButton?

Gruß Erdnuss
(Wie man sieht Java Anfänger, deswegen bitte Verständliche Antworten die auch ich verstehe 😀)
 
Das das GridBagLayout IST kompliziert (ganz subjektiv: Ich finde das eine gräßliche Klasse :autsch: gibt IMHO nur wenige Fälle, wo das wirklich Sinn macht...). Also, rein vom Layout her wäre sowas ja schon... kompakter
Java:
    public static void addComponentsToPane(Container pane)
    {
        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(new JLabel("Bild", JLabel.CENTER), BorderLayout.NORTH);

        JPanel p = new JPanel(new GridLayout(7,2));
        p.add(new JLabel("Anzahl"));
        p.add(new JLabel("Produkt"));

        p.add(new JTextField(""));
        p.add(new JTextField(""));

        p.add(new JTextField(""));
        p.add(new JTextField(""));

        p.add(new JTextField(""));
        p.add(new JTextField(""));

        p.add(new JTextField(""));
        p.add(new JTextField(""));

        p.add(new JTextField(""));
        p.add(new JTextField(""));

        p.add(new JLabel());
        p.add(new JButton("Kaufen"));

        panel.add(p, BorderLayout.CENTER);

        pane.add(panel);
    }

Dieser Rand, der da außen rum sein soll ist schon eher ungewöhnlich. Normalerweise hat man eher das Problem, zuwenig Platz zu haben. Man könnte da was mit einer EmptyBorder machen, aber ... üblich ist das nicht.


EDIT: Als nachtrag: Eine bestimmte Größe festzulegen macht man eben üblicherweise nicht - das überläßt man dem LayoutManager. Man kann den Components aber eine bevorzugte Größe geben, und die LayoutManager versuchen ggf., diesen Wunsch zu erfüllen:
someComponent.setPreferredSize(new Dimension(300,50));

Einen kleinen Abstrand kann man ggf. auch mit einem Rand oder einer anderen Layout-Verschachtelung hinkriegen. Aber leg' dich nicht so auf pixelgenaues aussehen fest.


EDIT2: Das Aussehen der ursprünglichen Tabelle ist zufällig genau das, wofür man GridBagLayout verwenden würde, aber nicht ganz in der Form, wie es bisher gemacht wurde. (Es macht z.B. keinen Sinn, eigenschaften in GridBagConstraints zu setzen, die man vorher schon gesetzt hat)
 
Zuletzt bearbeitet:
Danke schön für deine sehr schnelle Antwort.
Deine Umsetzung ist wesentlich leichter und verständlicher (auch für mich 🙂).

Ich werd mich morgen Mittag wieder darum kümmern und werd dann auch gleich zum nächsten Schritt über gehen. Da ich da bestimmt wieder Problem hab meld ich mich wieder hier.

Schönen Abend noch :toll:

Gruß
Erdnuss
 
Sooo mein nächstes Problem läss mal wiedern icht lange auf sich warten. Ich konnte jetzt dank der Hilfe von Marco13 meine Tabelle um einiges leichter gestalten. Nun möchte ich dem Button Funktionen zuweißen.

Bis jetzt wird ja nur ein Button angezeigt:
[JAVA=1]p.add(new JButton("Kaufen"));[/code]

Ich möchte das aber so machen:

  • In die Textfelder kann ich etwas rein schreiben (funktioniert ja jetzt schon^^) [JAVA=1]p.add(new JTextField(""));[/code]
  • Das was ich in die Textfelder rein schreibe soll, peer klick auf den Button unten angezeigt werden.(In meinem PHP-Dokument hab ich das mit Hilfe von html/mysql/php gemacht). [JAVA=1]
    p.add(new JLabel("Produkt", JLabel.CENTER));
    p.add(new JLabel("Besteller", JLabel.CENTER));
    [/code]
  • Danach sollen die Textfelder wieder leer sein nur bei Produkt/Besteller soll etwas drin stehen
  • Muss ich die PHP sachen überhaupt umschreiben oder kann ich das ganze irgendwie mit einander Verknüpfen?

Ich hoffe mal dass, das irgendwie unkompliziert funktioniert. (Bestimmt nicht 😀)


HTML:
...
<td><label>
        <input type="text" name="bezeichnung1" id="bezeichnung"  stye="width:100px"/>
      </label></td>
...
        <div align="center">
          <input type="submit" name="button" id="button" value="Kaufen" />
          </div>
...
	$anzahl1 = $_POST['anzahl1'];
	$bezeichnung1 = $_POST['bezeichnung1'];
...
if($anzahl1 != "" && $bezeichnung1 != ""){
	  $SQL = "INSERT INTO bestellungen VALUES ('','$anzahl1','$bezeichnung1','$user', 'NOW()')";
	  echo $SQL;
	  $query = mysql_query($SQL,$connect);
...
Fals es euch weiterhilft. So ungefähr hab ich das gemacht. (Ist jetzt halt nur ein kleiner Ausschnitt).

Gruß Erdnuss
 
Das, was ich gepostet hatte, hat (wie deine ursprüngliche Lösung) nur die Components in einem bestimmten Layout angezeigt. Um auf diese Components zugreifen zu können, muss man sie natürlich noch irgendwo vorhalten.

Java:
class X
{
    private List<JTextField> produktTextFields = new ArrayList<JTextField>();
    private JLabel dasProduktLabelDaUnten = new JLabel();

    void initGUI()
    {
        ....
        JTextField textField = new JTextField();
        produktTextFields.add(textField);
        somePanel.add(textField);
        ....
    }

    void auslesen()
    {
        String text = produktTextFields.get(0).getText();
        dasProduktLabelDaUnten.setText(text);
    }
 
Dankeschön für die schnelle Antwort.
Aber ich versteh nicht genau was du mit diesem Satz hier meinst.

Marco13 hat gesagt.:
Um auf diese Components zugreifen zu können, muss man sie natürlich noch irgendwo vorhalten.

Könntest du mir das bitte vielleicht noch einmal erklären?

Gruß
Erdnuss
 
Na, wie in dem Beispiel
Java:
class X
{
    void init()
    {
        somePanel.add(new JTextField()); // Tschüss
        somePanel.add(new JTextField()); // Tschüss
    }
}
An die dort erstellten JTextFields kommt man (ohne Hacks) nicht mehr ran. Stattdessen
Java:
class X
{
    JTextField produktTextField = null;
    JTextField sonstwasTextField = null;
    void init()
    {
        produktTextField = new JTextField();
        somePanel.add(produktTextField); 

        sonstwasTextField = new JTextField();
        somePanel.add(sonstwasTextField); 

    }

    void woandersKannManDannSowasMachenWie()
    {
        sonstwasTextField.setText("sonstwas");
    }

}
Und da es bei dir um mehrere JTextFields geht, packt man sie halt in eine List...
 
Soweit bin ich bis jetzt
[JAVA=1]
import java.awt.*;
import javax.swing.*;

public class Test {

JTextField AnzahlTextField = null;
JTextField ProduktTextField = null;

void init()
{
JPanel p = new JPanel(new GridLayout());
p.add(new JLabel("Anzahl", JLabel.CENTER));
p.add(new JLabel("Produkt", JLabel.CENTER));

AnzahlTextField = new JTextField();
p.add(AnzahlTextField);

ProduktTextField = new JTextField();
p.add(ProduktTextField);

p.add(new JLabel());
p.add(new JButton("Kaufen"));
}

void auslesen()
{
AnzahlTextField.setText("");
ProduktTextField.setText("");
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
[/code]

Nur beim Anzeigen gibt es ein Problem
 
Das Panel, das in "init" erzeugt wird, wird nie dem Frame hinzugefügt. (Und wenn du gesagt hättest, was das Problem ist, hätte man das vielleicht auch ohne Test erkannt). Du solltest meine geposteten Codeschnipsel nicht als 1:1-Referenzlösungen in dein Programm packen (und wenn, dann die kleingeschriebenen Variablennamen beibehalten!). Um ein bißchen Überlegen wirst du nicht drumrumkommen... 🙁
 
ich würde behaupten die init()-Methode wird in diesem Fall nicht mal aufgerufen...
Da hat Deros recht und das JPanel wird auch gar nicht dem Frame hinzugefügt.

Nur beim Anzeigen gibt es ein Problem
Sag nächstes mal was das Problem ist, da die die helfen wollen dein Code ggf. rauskopieren müssen um zu sehen was das Problem ist. Wenn du schon von Beginn an sagst was das Problem ist, wird es für die anderen einfacher nach dem Fehler zu suchen. 😛
 
cz3kit hat gesagt.:
Sag nächstes mal was das Problem ist, da die die helfen wollen dein Code ggf. rauskopieren müssen um zu sehen was das Problem ist. Wenn du schon von Beginn an sagst was das Problem ist, wird es für die anderen einfacher nach dem Fehler zu suchen. 😛

Ja das nächstemal schreib ich gleich dazu was für ein Problem ich hab^^
War nur in eile und wollte das so schnell wie möglich posten. Kommt nicht wieder vor :applaus:.

Gruß
Erdnuss
 
Sooo ich hab jetzt mit der Hilfe von Deros das Problem fast gelöst. (Danke nohchmal an dich 🙂).
Der Java Code sieht jetzt wie folgt aus:
[JAVA=1]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestWindow extends JFrame {

private static final long serialVersionUID = -1261578283155362972L;

public TestWindow() {

setTitle("Auftrag");
int windowWidth = 275;
int windowHeight = 500;
setBounds(50, 100, windowWidth, windowHeight);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel p = new JPanel(null);
p.setBackground(new Color(3,64,240));
p.setBounds(0, 0, windowWidth, windowHeight);

//-------Anzahl-------\\
JLabel anzahlLabel = new JLabel("Anzahl");
anzahlLabel.setBounds( 30, 40, 210, 20 );
anzahlLabel.setForeground(new Color(255,255,255));
//----------1----------\\
JTextField anzahlField1 = new JTextField(20);
anzahlField1.setName("Anzahl1");
anzahlField1.setBounds( 20, 65, 60, 20 );
//----------2----------\\
JTextField anzahlField2 = new JTextField(20);
anzahlField2.setName("Anzahl2");
anzahlField2.setBounds( 20, 90, 60, 20 );
//----------3----------\\
JTextField anzahlField3 = new JTextField(20);
anzahlField3.setName("Anzahl3");
anzahlField3.setBounds( 20, 115, 60, 20 );
//----------4----------\\
JTextField anzahlField4 = new JTextField(20);
anzahlField4.setName("Anzahl4");
anzahlField4.setBounds( 20, 140, 60, 20 );
//----------5----------\\
JTextField anzahlField5 = new JTextField(20);
anzahlField5.setName("Anzahl5");
anzahlField5.setBounds( 20, 165, 60, 20 );


//-------Produkt-------\\
JLabel produktLabel = new JLabel("Produkt");
produktLabel.setBounds( 150, 40, 205, 20 );
produktLabel.setForeground(new Color(255,255,255));
//----------1----------\\
JTextField produktField1 = new JTextField(20);
produktField1.setName("Produkt1");
produktField1.setBounds( 100, 65, 140, 20 );
//----------2----------\\
JTextField produktField2 = new JTextField(20);
produktField2.setName("Produkt2");
produktField2.setBounds( 100, 90, 140, 20 );
//----------3----------\\
JTextField produktField3 = new JTextField(20);
produktField3.setName("Produkt3");
produktField3.setBounds( 100, 115, 140, 20 );
//----------4----------\\
JTextField produktField4 = new JTextField(20);
produktField4.setName("Produkt4");
produktField4.setBounds( 100, 140, 140, 20 );
//----------5----------\\
JTextField produktField5 = new JTextField(20);
produktField5.setName("Produkt5");
produktField5.setBounds( 100, 165, 140, 20 );


JButton button=new JButton("Kaufen");
button.setBounds(100, 190, 140, 20);


ActionListener al = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
JButton button=(JButton) e.getSource();
JPanel panel=(JPanel) button.getParent();

String anzahl1="";
String produkt1="";
String anzahl2="";
String produkt2="";
String anzahl3="";
String produkt3="";
String anzahl4="";
String produkt4="";
String anzahl5="";
String produkt5="";


for(int i=0;i<panel.getComponentCount();i++){
Component comp=panel.getComponent(i);
//----------1----------\\
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField anzahlField1 = (JTextField) comp;
anzahl1=anzahlField1.getText();
}
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField produktField1 = (JTextField) comp;
produkt1=produktField1.getText();
}
//----------2----------\\
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField anzahlField2 = (JTextField) comp;
anzahl2=anzahlField2.getText();
}
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField produktField2 = (JTextField) comp;
produkt2=produktField2.getText();
}
//----------3----------\\
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField anzahlField3 = (JTextField) comp;
anzahl3=anzahlField3.getText();
}
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField produktField3 = (JTextField) comp;
produkt3=produktField3.getText();
}
//----------4----------\\
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField anzahlField4 = (JTextField) comp;
anzahl4=anzahlField4.getText();
}
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField produktField4 = (JTextField) comp;
produkt4=produktField4.getText();
}
//----------5----------\\
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField anzahlField5 = (JTextField) comp;
anzahl5=anzahlField5.getText();
}
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField produktField5 = (JTextField) comp;
produkt5=produktField5.getText();
}

}
System.out.println(anzahl1+" "+produkt1);
System.out.println(anzahl2+" "+produkt2);
System.out.println(anzahl3+" "+produkt3);
System.out.println(anzahl4+" "+produkt4);
System.out.println(anzahl5+" "+produkt5);

}
};
button.addActionListener(al);
p.add(anzahlLabel);
p.add(anzahlField1);
p.add(anzahlField2);
p.add(anzahlField3);
p.add(anzahlField4);
p.add(anzahlField5);
p.add(produktLabel);
p.add(produktField1);
p.add(produktField2);
p.add(produktField3);
p.add(produktField4);
p.add(produktField5);
p.add(button);
JScrollPane scrollPane = new JScrollPane(p);
add(scrollPane);

setVisible(true);
}
}
[/code]

Einziges Problem bei der Ausgabe. (Siehe Bild)



Ich denke das Bild sagt alles und ich muss nichts weiter dazu sagen.

Schönen Abend noch :toll:
Erdnuss
 
Java:
                        if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
                            JTextField anzahlField1 = (JTextField) comp;
                            anzahl1=anzahlField1.getText();
                        }
                        if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
                            JTextField produktField1 = (JTextField) comp;
                            produkt1=produktField1.getText();
                        }
                        //----------2----------\\
                        if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
                            JTextField anzahlField2 = (JTextField) comp;
                            anzahl2=anzahlField2.getText();
                        }
                        if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
                            JTextField produktField2 = (JTextField) comp;
                            produkt2=produktField2.getText();
                        }
In wie fern soll das Programm an der identischen Abfrage erkennen, ob es sich um produkt 1 oder 2 handelt?
Mach die JTextField in ein Array rein, dann brauchst du auch nicht über die Componenten zu iterieren.
 
jop entweder das JTextField anhand des Namen (.getName()) identifizieren oder sie halt anderweitig abspeichern, funktionieren würde wohl beides, aber so ist Käse, kriegst für alle den Wert des letzten Feldes
 
Sooo, hab jetzt wieder mit der Hilfe von Deros meine Problem gelöst.

[JAVA=1]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestWindow extends JFrame {

private static final long serialVersionUID = -1261578283155362972L;

public TestWindow() {

setTitle("Auftrag");
int windowWidth = 275;
int windowHeight = 500;
setBounds(50, 100, windowWidth, windowHeight);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel p = new JPanel(null);
p.setBackground(new Color(3,64,240));
p.setBounds(0, 0, windowWidth, windowHeight);

//-------Anzahl-------\\
JLabel anzahlLabel = new JLabel("Anzahl");
anzahlLabel.setBounds( 30, 40, 210, 20 );
anzahlLabel.setForeground(new Color(255,255,255));
//----------1----------\\
JTextField anzahlFielda = new JTextField(20);
anzahlFielda.setName("Anzahla");
anzahlFielda.setBounds( 20, 65, 60, 20 );
//----------2----------\\
JTextField anzahlFieldb = new JTextField(20);
anzahlFieldb.setName("Anzahlb");
anzahlFieldb.setBounds( 20, 90, 60, 20 );
//----------3----------\\
JTextField anzahlFieldc = new JTextField(20);
anzahlFieldc.setName("Anzahlc");
anzahlFieldc.setBounds( 20, 115, 60, 20 );
//----------4----------\\
JTextField anzahlFieldd = new JTextField(20);
anzahlFieldd.setName("Anzahld");
anzahlFieldd.setBounds( 20, 140, 60, 20 );
//----------5----------\\
JTextField anzahlFielde = new JTextField(20);
anzahlFielde.setName("Anzahle");
anzahlFielde.setBounds( 20, 165, 60, 20 );


//-------Produkt-------\\
JLabel produktLabel = new JLabel("Produkt");
produktLabel.setBounds( 150, 40, 205, 20 );
produktLabel.setForeground(new Color(255,255,255));
//----------1----------\\
JTextField produktFielda = new JTextField(20);
produktFielda.setName("Produkta");
produktFielda.setBounds( 100, 65, 140, 20 );
//----------2----------\\
JTextField produktFieldb = new JTextField(20);
produktFieldb.setName("Produktb");
produktFieldb.setBounds( 100, 90, 140, 20 );
//----------3----------\\
JTextField produktFieldc = new JTextField(20);
produktFieldc.setName("Produktc");
produktFieldc.setBounds( 100, 115, 140, 20 );
//----------4----------\\
JTextField produktFieldd = new JTextField(20);
produktFieldd.setName("Produktd");
produktFieldd.setBounds( 100, 140, 140, 20 );
//----------5----------\\
JTextField produktFielde = new JTextField(20);
produktFielde.setName("Produkte");
produktFielde.setBounds( 100, 165, 140, 20 );


JButton button=new JButton("Kaufen");
button.setBounds(100, 190, 140, 20);


ActionListener al = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
JButton button=(JButton) e.getSource();
JPanel panel=(JPanel) button.getParent();

String anzahla="";
String produkta="";
String anzahlb="";
String produktb="";
String anzahlc="";
String produktc="";
String anzahld="";
String produktd="";
String anzahle="";
String produkte="";


for(int i=0;i<panel.getComponentCount();i++){
Component comp=panel.getComponent(i);
//----------1----------\\
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField anzahlFielda = (JTextField) comp;
if(anzahlFielda.getName().equals("Anzahla"))
anzahla = anzahlFielda.getText();
}
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField produktFielda = (JTextField) comp;
if(produktFielda.getName().equals("Produkta"))
produkta = produktFielda.getText();
}
//----------2----------\\
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField anzahlFieldb = (JTextField) comp;
if(anzahlFieldb.getName().equals("Anzahlb"))
anzahlb = anzahlFieldb.getText();
}
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField produktFieldb = (JTextField) comp;
if(produktFieldb.getName().equals("Produktb"))
produktb = produktFieldb.getText();
}
//----------3----------\\
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField anzahlFieldc = (JTextField) comp;
if(anzahlFieldc.getName().equals("Anzahlc"))
anzahlc = anzahlFieldc.getText();
}
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField produktFieldc = (JTextField) comp;
if(produktFieldc.getName().equals("Produktc"))
produktc = produktFieldc.getText();
}
//----------4----------\\
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField anzahlFieldd = (JTextField) comp;
if(anzahlFieldd.getName().equals("Anzahld"))
anzahld = anzahlFieldd.getText();
}
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField produktFieldd = (JTextField) comp;
if(produktFieldd.getName().equals("Produktd"))
produktd = produktFieldd.getText();
}
//----------5----------\\
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField anzahlFielde = (JTextField) comp;
if(anzahlFielde.getName().equals("Anzahle"))
anzahle = anzahlFielde.getText();
}
if(comp instanceof JTextField && !(comp instanceof JFormattedTextField)){
JTextField produktFielde = (JTextField) comp;
if(produktFielde.getName().equals("Produkte"))
produkte = produktFielde.getText();
}

}
System.out.println(anzahla+" "+produkta);
System.out.println(anzahlb+" "+produktb);
System.out.println(anzahlc+" "+produktc);
System.out.println(anzahld+" "+produktd);
System.out.println(anzahle+" "+produkte);

}
};
button.addActionListener(al);
p.add(anzahlLabel);
p.add(anzahlFielda);
p.add(anzahlFieldb);
p.add(anzahlFieldc);
p.add(anzahlFieldd);
p.add(anzahlFielde);
p.add(produktLabel);
p.add(produktFielda);
p.add(produktFieldb);
p.add(produktFieldc);
p.add(produktFieldd);
p.add(produktFielde);
p.add(button);
JScrollPane scrollPane = new JScrollPane(p);
add(scrollPane);

setVisible(true);
}
}
[/code]

Hab das jetzt sooo gestaltet damit ich es auch verstehe^^

Die letzte Frage die ich jetzt noch hätte.
  • Wie kann ich diese Datei mit einer HTML Datei "Verknüpfen" (Deros müsste wissen wie ich es meine^^)

Beispiel:

HTML:
<html>
<head>
	<title>
	Hallo Applet
	</title>
</head>
<body>
<applet code="Hallo01.class"width=350 height=90

</applet>
</body>
</html>

[JAVA=1]
import java.applet.*;
import java.awt.*;
public class Hallo01 extends java.applet.Applet
{
public void paint(Graphics g)
{
g.drawString("Hallo, viel Erfolg mit Java!",100,50];
}
}
[/code]

Danke schonmal im vorraus 🙂

Gruß
Erdnuss
 
An die dort erstellten JTextFields kommt man (ohne Hacks) nicht mehr ran. Stattdessen

Und genau dieses getComponentCount/getComponent/getName/instanceof :autsch: -Gekrampfe war das, was ich mit "Hacks" meinte: Das ist grober Unfug :noe: Wenn du die TextFields brauchst, dann speichere sie irgendwo... wie oben schon geschrieben
Und da es bei dir um mehrere JTextFields geht, packt man sie halt in eine List...
 
~80 statt ~200 Zeilen - und flexibler. (Füg' bei dir mal noch zwei weitere TextFields ein...). Das mit dem null-Layout ist zwar auch nicht so sinnvoll, aber wenn du drauf bestehst...

Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.*;

public class TextFieldsTestWindow extends JFrame {

    private static final long serialVersionUID = -1261578283155362972L;

    private List<JTextField> anzahlTextFields = new ArrayList<JTextField>();
    private List<JTextField> produktTextFields = new ArrayList<JTextField>();


        public TextFieldsTestWindow() {

            setTitle("Auftrag");
            int windowWidth = 275;
            int windowHeight = 500;
            setBounds(50, 100, windowWidth, windowHeight);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel p = new JPanel(null);
            p.setBackground(new Color(3,64,240));
            p.setBounds(0, 0, windowWidth, windowHeight);

            //-------Anzahl-------\\
            JLabel anzahlLabel = new JLabel("Anzahl");
            anzahlLabel.setBounds( 30, 40, 210, 20 );
            anzahlLabel.setForeground(new Color(255,255,255));
            p.add(anzahlLabel);


            for (int i=0; i<5; i++)
            {
                JTextField anzahlField = new JTextField(20);
                anzahlField.setBounds( 20, 65+i*25, 60, 20 );
                anzahlTextFields.add(anzahlField);
                p.add(anzahlField);
            }


            //-------Produkt-------\\
            JLabel produktLabel = new JLabel("Produkt");
            produktLabel.setBounds( 150, 40, 205, 20 );
            produktLabel.setForeground(new Color(255,255,255));
            p.add(produktLabel);

            for (int i=0; i<5; i++)
            {
                JTextField produktField = new JTextField(20);
                produktField.setBounds( 100, 65+i*25, 140, 20 );
                produktTextFields.add(produktField);
                p.add(produktField);
            }

            JButton button=new JButton("Kaufen");
            button.setBounds(100, 190, 140, 20);
            p.add(button);

            ActionListener al = new ActionListener() {
                public void actionPerformed( ActionEvent e )
                {
                    for (int i=0; i<5; i++)
                    {
                        JTextField anzahlTextField = anzahlTextFields.get(i);
                        JTextField produktTextField = produktTextFields.get(i);

                        String anzahl = anzahlTextField.getText();
                        String produkt = produktTextField.getText();
                        System.out.println(anzahl+" "+produkt);
                    }

                }
            };
            button.addActionListener(al);

            JScrollPane scrollPane = new JScrollPane(p);
            add(scrollPane);

            setVisible(true);
        }

    public static void main(String args[])
    {
        new TextFieldsTestWindow();
    }
}
 
Glückwunsch da haste es dem Java-Anfänger ja so richtig gezeigt...aber um auf die eigentliche Frage zurückzukommen:

Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.*;
 
public class TestWindow2 extends JApplet {
 
    private static final long serialVersionUID = -1261578283155362972L;
 
    private List<JTextField> anzahlTextFields = new ArrayList<JTextField>();
    private List<JTextField> produktTextFields = new ArrayList<JTextField>();
 
 
        public void createGUI() {
 
            int windowWidth = 275;
            int windowHeight = 500;
            setBounds(50, 100, windowWidth, windowHeight);
 
            JPanel p = new JPanel(null);
            p.setBackground(new Color(3,64,240));
            p.setBounds(0, 0, windowWidth, windowHeight);
 
            //-------Anzahl-------\\
            JLabel anzahlLabel = new JLabel("Anzahl");
            anzahlLabel.setBounds( 30, 40, 210, 20 );
            anzahlLabel.setForeground(new Color(255,255,255));
            p.add(anzahlLabel);
 
            //-------Produkt-------\\
            JLabel produktLabel = new JLabel("Produkt");
            produktLabel.setBounds( 150, 40, 205, 20 );
            produktLabel.setForeground(new Color(255,255,255));
            p.add(produktLabel);
            
            for (int i=0; i<5; i++)
            {
                JTextField anzahlField = new JTextField(20);
                anzahlField.setBounds( 20, 65+i*25, 60, 20 );
                anzahlTextFields.add(anzahlField);
                p.add(anzahlField);
                
                JTextField produktField = new JTextField(20);
                produktField.setBounds( 100, 65+i*25, 140, 20 );
                produktTextFields.add(produktField);
                p.add(produktField);
            }
 
            JButton button=new JButton("Kaufen");
            button.setBounds(100, 190, 140, 20);
            p.add(button);
 
            ActionListener al = new ActionListener() {
                public void actionPerformed( ActionEvent e )
                {
                    for (int i=0; i<5; i++)
                    {
                        JTextField anzahlTextField = anzahlTextFields.get(i);
                        JTextField produktTextField = produktTextFields.get(i);
 
                        String anzahl = anzahlTextField.getText();
                        String produkt = produktTextField.getText();
                        System.out.println(anzahl+" "+produkt);
                    }
 
                }
            };
            button.addActionListener(al);
 
            JScrollPane scrollPane = new JScrollPane(p);
            getContentPane().add(scrollPane);
            setSize(400,400);
            setVisible(true);
        }
 
        public void init() {
            
            try {
                javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        createGUI();
                    }
                });
            } catch (Exception e) {
                System.err.println("Fehler und so... "+e);
            }
        }

}

Java:
<html>
<head>
<title>Auftrag</title>
</head>
<body>
<applet code="TestWindow2.class"width=450 height=450></applet>
</body>
</html>
 
Zuerst einmal Danke an Marco13 für die verkürtze Version. Da muss ich mich erstmal rein arbeiten und schauen wie das ganze aufgebaut.

Und natürlich auch Danke an Deros das du meine eigentlich gestellte Frage beantwortet hast 🙂

Schönes Wochenende
3rdNuss
 
Glückwunsch da haste es dem Java-Anfänger ja so richtig gezeigt...

Falls das so zynisch gemeint war, wie es klang, (ist mir das egal 😉 aber) als kleine Rechtfertigung: Wenn man das Programm mit dem bisherigen Ansatz weitergemacht hätte, wäre man damit nicht glücklich geworden :noe: Ich durfte und darf mich selbst mit Software rumplagen, bei der zu jedem Zeitpunkt nur "das gemacht wurde, was gerade jetzt unbedingt gemacht werden muss" (d.h. die bennendste Frage wurde (irgendwie) beantwortet). Was da mittel- und langfristig rauskommt, kann man sich vielleicht vorstellen... 🙁
 

Zurück
Oben