Java SWING/GUI JTextArea klopot

0

Witam.
Pisze prosta aplikacje gui i natknalem sie na problem z ktorym nie moge sobie poradzic.
Bardzo prosilbym was o pomoc.

Chcialbym aby po wpisaniu imienia oraz nazwiska i kliknieciu przycisku dodaj, wprowadzone dane wpisywaly sie do textarea znajdujacego sie ponizej oraz zapisywaly sie.

Domyslam sie ze kod moze byc troche chaotycznie napisany ale to dlatego, ze jestem jeszcze dosc slaby w programowaniu i jak na razie wszystko przychodzi metoda prob i bledow. pola btn_dodaj oraz actionPerformed zostawilem swiadomie puste, poniewaz rozne proby konczyly sie niepowodzeniem.

Licze na wasza pomoc.
pozdrawiam

package projekt67550;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
*

  • @Author Damian
    */
    public class NewJFrame extends javax.swing.JFrame {

    private dodajUcznia dodajUcznia;
    private JLabel imie, nazwisko;
    private JTextField txt_imie, txt_nazwisko;
    private JButton btn_Dodaj, btn_Cancel;
    private boolean okData;
    private JTextArea notatnik;

    public NewJFrame() {
    initComponents();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

     jLabel1 = new javax.swing.JLabel();
     jLabel2 = new javax.swing.JLabel();
     jLabel3 = new javax.swing.JLabel();
     jMenuBar1 = new javax.swing.JMenuBar();
     jMenu1 = new javax.swing.JMenu();
     mn_Zapisz = new javax.swing.JMenuItem();
     mn_Wyjdz = new javax.swing.JMenuItem();
     jMenu2 = new javax.swing.JMenu();
     mn_DodajUcznia = new javax.swing.JMenuItem();
     mn_DodajOcene = new javax.swing.JMenuItem();
    
     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     setTitle("Damian Bochenek 67550");
     setBackground(new java.awt.Color(0, 204, 0));
     setForeground(new java.awt.Color(0, 204, 0));
    
     jLabel1.setFont(new java.awt.Font("Stencil", 0, 24)); // NOI18N
     jLabel1.setText("Elektroniczny dziennik");
    
     jLabel2.setFont(new java.awt.Font("Stencil", 1, 20)); // NOI18N
     jLabel2.setText("Klasa Va");
    
     jLabel3.setFont(new java.awt.Font("Segoe Print", 0, 24)); // NOI18N
     jLabel3.setText("Szkoła Podstawowa \nim. Jana Pawła II");
    
     jMenuBar1.setToolTipText("");
    
     jMenu1.setMnemonic('F');
     jMenu1.setText("File");
    
     mn_Zapisz.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
     mn_Zapisz.setMnemonic('P');
     mn_Zapisz.setText("Zapisz");
     jMenu1.add(mn_Zapisz);
    
     mn_Wyjdz.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
     mn_Wyjdz.setMnemonic('W');
     mn_Wyjdz.setText("Wyjdź");
     mn_Wyjdz.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
             mn_WyjdzActionPerformed(evt);
         }
     });
     jMenu1.add(mn_Wyjdz);
    
     jMenuBar1.add(jMenu1);
    
     jMenu2.setText("Edit");
    
     mn_DodajUcznia.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
     mn_DodajUcznia.setText("Lista uczniów");
     mn_DodajUcznia.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
             mn_DodajUczniaActionPerformed(evt);
         }
     });
     jMenu2.add(mn_DodajUcznia);
    
     mn_DodajOcene.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
     mn_DodajOcene.setMnemonic('O');
     mn_DodajOcene.setText("Dodaj ocenę");
     mn_DodajOcene.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
             mn_DodajOceneActionPerformed(evt);
         }
     });
     jMenu2.add(mn_DodajOcene);
    
     jMenuBar1.add(jMenu2);
    
     setJMenuBar(jMenuBar1);
    
     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
     getContentPane().setLayout(layout);
     layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                 .addGroup(layout.createSequentialGroup()
                     .addGap(30, 30, 30)
                     .addComponent(jLabel3))
                 .addGroup(layout.createSequentialGroup()
                     .addGap(186, 186, 186)
                     .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)))
             .addContainerGap(50, Short.MAX_VALUE))
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
             .addGap(0, 0, Short.MAX_VALUE)
             .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE)
             .addGap(77, 77, 77))
     );
     layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
             .addContainerGap(45, Short.MAX_VALUE)
             .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
             .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
             .addComponent(jLabel3)
             .addGap(53, 53, 53))
     );
    
     pack();
    

    }// </editor-fold>

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

    private void mn_DodajOceneActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void mn_DodajUczniaActionPerformed(java.awt.event.ActionEvent evt) {

    if (dodajUcznia == null) {
    dodajUcznia = new dodajUcznia(null);
    }
    dodajUcznia.setVisible(true);

    }

    /**

    • @param args the command line arguments
      /
      public static void main(String args[]) {
      /
      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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
        */
        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 ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

      /* Create and display the form */
      java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
      new NewJFrame().setVisible(true);
      }
      });
      }
      // Variables declaration - do not modify
      private javax.swing.JLabel jLabel1;
      private javax.swing.JLabel jLabel2;
      private javax.swing.JLabel jLabel3;
      private javax.swing.JMenu jMenu1;
      private javax.swing.JMenu jMenu2;
      private javax.swing.JMenuBar jMenuBar1;
      private javax.swing.JMenuItem mn_DodajOcene;
      private javax.swing.JMenuItem mn_DodajUcznia;
      private javax.swing.JMenuItem mn_Wyjdz;
      private javax.swing.JMenuItem mn_Zapisz;
      // End of variables declaration
      }

class dodajUcznia extends JDialog implements ActionListener {

private JLabel imie, nazwisko;
private JTextField txt_imie, txt_nazwisko;
private JButton btn_Dodaj, btn_Cancel;
private boolean okData;
private JTextArea notatnik;

public dodajUcznia(JFrame owner) {
    
    super(owner, "Dodaj ucznia", true);
    setSize(700, 600);
    setLayout(null);


    imie = new JLabel("Imię:", JLabel.RIGHT);
    imie.setBounds(0, 0, 100, 20);
    add(imie);

    txt_imie = new JTextField();
    txt_imie.setBounds(110, 0, 100, 25);
    add(txt_imie);
    
    nazwisko = new JLabel("Nazwisko:", JLabel.RIGHT);
    nazwisko.setBounds(0, 30, 100, 20);
    add(nazwisko);

    txt_nazwisko = new JTextField();
    txt_nazwisko.setBounds(110, 30, 100, 25);
    add(txt_nazwisko); 
    
    btn_Dodaj = new JButton("Dodaj ucznia");
    btn_Dodaj.setBounds(30, 80, 100, 25);
    btn_Dodaj.getActionListeners();
    add(btn_Dodaj);
    
    btn_Cancel = new JButton("Cancel");
    btn_Cancel.setBounds(150, 80, 100, 25);
    btn_Cancel.getActionListeners();
    add(btn_Cancel);
    
    notatnik = new JTextArea();
    notatnik.setBounds(30, 120, 620, 380 );
    add(notatnik);
}

public String getImie()
{
    return txt_imie.getText();
}

public String getNazwisko()
{
    return txt_nazwisko.getText();
}

public boolean isOk()
{
    return okData;
}

public void setFocus()
{
    txt_imie.requestFocusInWindow();
}

private void btn_DodajActionPerformed(java.awt.event.ActionEvent evt) 
{
     
    
}

@Override
public void actionPerformed(ActionEvent e) 
{
   
    
        
    
}

}

0

Masakra co tam się dzieje. Coś takiego dodaje Ci uczniów do TextArea:

public void actionPerformed(ActionEvent e) {
   notatnik.append(getImie() + " " + getNazwisko()+"\n");
}

Nie zapomnij dopisać wyżej:

btn_Dodaj.addActionListener(this);
0

dziekuje

1 użytkowników online, w tym zalogowanych: 0, gości: 1