Swing Konsolen Programm in GUI - Suche Hilfe bei Konsolenausgabe

Kjeld

Mitglied
Hallo zusammen,

vorab möchte ich erwähnen, dass ich das recht neu in Java bin und dies mein erster Versuch mit einer Benutzer-Oberfläche ist.
Ich habe ein kleines funktionsfähiges Konsolenprogramm, welches ich jetzt gerne über eine Benutzeroberfläche steuern möchte. Es scheitert leider schon bei der Konsolenausgabe.

Die Funktion die ich mir wünsche:

- Button "Spiel starten" drücken
- Ausgabe der Konsole wird in der Benutzeroberfläche ausgegeben

Ich habe schon einiges an Büchern und Foren durchgelesen, aber bisher keine Lösung dazu gefunden.
Derzeit klappt zwar mein Button, aber der startet natürlich auch nur die Konsolenausgabe.

Habt Ihr einen Tipp für mich, wie ich dieses Problem angehen kann?


Java:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fussballmanager;

import com.fussballmanager.kader.Mannschaft;
import com.fussballmanager.kader.Namen;
import com.fussballmanager.kader.Spieler;
import com.fussballmanager.kader.Torwart;
import com.fussballmanager.kader.Trainer;
import com.fussballmanager.spielverlauf.Spielverlauf;
import com.fussballmanager.stadionsprecher.Stadionsprecher;
//import javax.swing.DefaultListModel;

/**
 *
 * @author Mario Koenig <mario.koenig@fernuni-hagen.de>
 */
public class HauptFenster extends javax.swing.JFrame {

  /**
   * Creates new form HauptFenster
   */
  public HauptFenster() {
//    listModel = new DefaultListModel<>();
    initComponents();
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">
  private void initComponents() {

    jTabbedPane1 = new javax.swing.JTabbedPane();
    jPanel1 = new javax.swing.JPanel();
    beendenButton = new javax.swing.JButton();
    hinzufuegenButton = new javax.swing.JButton();
    jPanel2 = new javax.swing.JPanel();
    ausgabeFeld = new javax.swing.JScrollPane();
    jMenuBar1 = new javax.swing.JMenuBar();
    dateiMenue = new javax.swing.JMenu();
    jMenu2 = new javax.swing.JMenu();
    dateiMenue3 = new javax.swing.JMenu();
    dateiMenue4 = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jTabbedPane1.addContainerListener(new java.awt.event.ContainerAdapter() {
      public void componentAdded(java.awt.event.ContainerEvent evt) {
        jTabbedPane1ComponentAdded(evt);
      }
    });

    beendenButton.setText("Beenden");
    beendenButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        beendenButtonActionPerformed(evt);
      }
    });

    hinzufuegenButton.setText("Spiel starten");
    hinzufuegenButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        hinzufuegenButtonActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(ausgabeFeld)
        .addContainerGap())
    );
    jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(ausgabeFeld, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
        .addContainerGap())
    );

    ausgabeFeld.getAccessibleContext().setAccessibleName("");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(hinzufuegenButton, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addComponent(beendenButton, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(0, 735, Short.MAX_VALUE))
          .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(hinzufuegenButton)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(beendenButton)
        .addContainerGap())
    );

    jTabbedPane1.addTab("Mannschaft", jPanel1);

    dateiMenue.setText("Datei");
    jMenuBar1.add(dateiMenue);

    jMenu2.setText("Ansicht");
    jMenuBar1.add(jMenu2);

    dateiMenue3.setText("Einstellungen");
    jMenuBar1.add(dateiMenue3);

    dateiMenue4.setText("Hilfe");
    jMenuBar1.add(dateiMenue4);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 865, javax.swing.GroupLayout.PREFERRED_SIZE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 426, javax.swing.GroupLayout.PREFERRED_SIZE)
    );

    pack();
  }// </editor-fold>

  private void jTabbedPane1ComponentAdded(java.awt.event.ContainerEvent evt) {
  }

  private void hinzufuegenButtonActionPerformed(java.awt.event.ActionEvent evt) {
    //**********************
    int kGroesse = 15;

    //Mannschaft 1
    Trainer t1 = new Trainer(Namen.name(), (int) (Math.random() * 30 + 18), (int) (Math.random() * 10 + 1));
    Torwart tw1 = new Torwart(Namen.name(), (int) (Math.random() * 20 + 18), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1));

    Spieler[] sp1 = new Spieler[kGroesse];
    Spieler[] sp2 = new Spieler[kGroesse];
    for (int i = 0; i < kGroesse; i++) {
      sp1[i] = new Spieler("D_" + Namen.name(), (int) (Math.random() * 20 + 18), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1));
      sp2[i] = new Spieler("B_" + Namen.name(), (int) (Math.random() * 20 + 18), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1));
    }

    //********************
    //Mannschaft 2
    Trainer t2 = new Trainer(Namen.name(), (int) (Math.random() * 30 + 18), (int) (Math.random() * 10 + 1));
    Torwart tw2 = new Torwart(Namen.name(), (int) (Math.random() * 10 + 1), (int) (Math.random() * 20 + 18), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1), (int) (Math.random() * 10 + 1));

    Mannschaft m1 = new Mannschaft("Deutschland", t1, tw1, sp1);
    Mannschaft m2 = new Mannschaft("Brasilien", t2, tw2, sp2);
    Spielverlauf f1 = new Spielverlauf();

    Stadionsprecher.Spielstatistik(m1, m2);
    f1.starteSpiel(m1, m2);
    Stadionsprecher.Spiel(f1);
  }

  private void beendenButtonActionPerformed(java.awt.event.ActionEvent evt) {
    System.exit(0);
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) throws Exception {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] 
     */
    try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;


        }
      }
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(HauptFenster.class
              .getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(
            new Runnable() {
              @Override
              public void run() {
                new HauptFenster().setVisible(true);
              }
            });



  }
//  private DefaultListModel<String> listModel;
  // Variables declaration - do not modify
  private javax.swing.JScrollPane ausgabeFeld;
  private javax.swing.JButton beendenButton;
  private javax.swing.JMenu dateiMenue;
  private javax.swing.JMenu dateiMenue3;
  private javax.swing.JMenu dateiMenue4;
  private javax.swing.JButton hinzufuegenButton;
  private javax.swing.JMenu jMenu2;
  private javax.swing.JMenuBar jMenuBar1;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JTabbedPane jTabbedPane1;
  // End of variables declaration
}
 
Entschuldigung, aber ich verstehe dein Problem bzw. deine Frage nicht. Wenn du noch nicht viel mit GUIs gearbeitet hast, würde ich dir raten mal ein paar Swing Tutorials zu den ganzen Komponenten durchzumachen. Mit dem GUI-Builder anzufangen ist eine ganz schlechte Idee, da steckt einfach mehr dahinter als sich lustig was zusammenzuklicken. Für den Anfang würde ich an deiner Stelle einfach mal ein HelloWorld in Swing programmieren. Ohne Gui-Builder.
 
Du sagst ja selbst: Du hast meine Frage nicht verstanden.
Ich habe mich vielleicht auch undeutlich ausgedrückt.

Ich wollte nicht eine step-by-step Anleitung wie ich das umsetze, sondern einen Tipp, wie ich die Konsolenausgabe in ein jTextArea bekomme.

Habe das Problem aber schon gelöst. Musste dafür halt nur etwas länger probieren.
Es war hier auch weniger ein Swing-Problem, wie ich feststellen konnte. Das bisher gelernte auf neue Situationen umzusetzen ist bei mir noch das Problem

Habe die Ausgabe jetzt mit setOut, setErr umgeleitet.

Dennoch vielen Dank.
 

Zurück
Oben