Was ist Falsch, Java-Swing?

Hallo,

Möchte, dass wenn man auf "Schreiben" klickt zum ActionListener kommt und dann auf einen neuen JFrame seinen Text schreiben kann, aber irgendwie geht das nicht, funktioniert das mit den Parametern nicht ?

  1. Klasse (beinhaltet main)
package Swing;

import javax.swing.*;

public class Menubar {

    static JMenuItem item1, item2, item3; // 2 Items für Menü

    public Menubar() {
        String text;
        JFrame frame = new  JFrame("Fenstertext");
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null); // Mitte
        frame.setLayout(null);
        frame.requestFocus();

        JMenuBar bar = new JMenuBar();
        JMenu menu = new JMenu("Datei");// Bekommt Items, Beispiel auf File klicken ist Menu,namens "Datei" darunter dann Item

        item1 = new JMenuItem("Neu"); // 1. Itemname
        item1.addActionListener(new MenuHandler());
        item2 = new JMenuItem("Beenden");
        item2.addActionListener(new MenuHandler());
        item3 = new JMenuItem("Schreiben");
        item3.addActionListener(new MenuHandler());


        menu.add(item1);
        menu.addSeparator();  // Strich für Abtrennung (organisierter)
        menu.add(item2);
        menu.addSeparator();
        menu.add(item3);

        bar.add(menu); // Menu zu Bar hinzufügen
        frame.setJMenuBar(bar); // Setzen, da nur eine bar...
        frame.setVisible(true);

    }

    public static void main(String[] args) {

        new Menubar();

    }

}

2 Klasse :

package Swing;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Scanner;

public class MenuHandler implements ActionListener {


    @Override
    public void actionPerformed(ActionEvent e) {
// WAS SOLL PASSIEREN ?

        if (e.getSource() == Menubar.item1) { // Ein bestimmtes Item ?
            System.out.println("Neu...");
        } else if (e.getSource() == Menubar.item2) {
            System.exit(0); // Programm beenden
        } else if (e.getSource() == Menubar.item3) {
            JFrame neuer = new JFrame();
            File file1 = new File("src/textSpeichern.txt");
            neuer.setSize(800, 600);
            neuer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            neuer.setResizable(false);
            neuer.setLocationRelativeTo(null); // Mitte
            neuer.setLayout(null);
            neuer.requestFocus();

            neuer.setVisible(true);

            System.out.println("Was möchtest du schreiben?");
            Scanner scanner = new Scanner(System.in);
            String as = scanner.next();
            JPanel panel = new JPanel();
            JLabel label = new JLabel(as);
            panel.add(label);
            neuer.add(panel);
        }

    }
}

Warum geht das nicht ???

Danke für die Antworten bzw. die Hilfe ! :)

Computer, programmieren, Java, Informatik
Verkapselung von Klasse?

Hallo Coder-Freunde,

meine Freundin hat in einer Übung für die Uni folgende Java-Aufgabe zu lösen:

// ähnlich java.io.File.listFiles(), gibt aber nicht ein Array sondern ein Filez-Objekt zurück, das die File-Objekte des Directory als List<File> verkapselt

public static Filez create(File directory);

Folgendes haben wir bisher gemacht:

class Filez {

public Filez() {

this.list = new ArrayList<File>();

}

public static Filez create(File directory) { // File directory ist ein Objekt mit dem Pfad zum zu durchsuchenden Verzeichnis.

//List<File> list = new ArrayList<File>();

File[] filesAndDirs = directory.listFiles();

// Daten aus Array werden zu Liste hinzugefügt

for(int i = 0; i < filesAndDirs.length; i++) {

list.add(i, filesAndDirs[i]);

System.out.println(i + ": " + list.get(i));

}

return XXXXX; // Hier hängt's... wie soll eine Liste als Filez-Objekt zurückgegeben werden?

}

}

Mir persönlich erschließt sich die Formulierung "[... gibt ein] Filez-Objekt zurück, das die File-Objekte des Directory als List<File> verkapselt" nicht. Wie soll eine Methode als Returnwert ein Filez-Objekt vom Typ list zurückgeben?

Wir stehen hier auf dem Schlauch, bitte Hilfe!

UPDATE:

Antwort, falls jemand ein ähnliches Problem hat: Konstruktor erstellen!

public Filez() {} oder public Filez(List<File> attrList) { this.list = attrList; }

Return von create() ist dann das gesamte Objekt.

Computer, programmieren, Java, OOP Programmierung
Java : Warum wartet der Scanner nicht auf meine Eingabe?

Hallo,

Gebt mal folgenden Code bei euch ein und startet :

package Lernen;

import java.util.Arrays;
import java.util.Scanner;

public class CarSeller {

    int rColor = (int)(Math.random()*9);
    int color;
    int ps;
    int mark;
    String color2;
    String ps2;
    String mark2;
    String[] colorArray = new String[10];

    public void price(int color, int ps, int mark) {
        this.color = color;
        this.ps = ps;
        this.mark = mark;
    }

    public int RandomColor() {
        System.out.println(colorArray[rColor]);
        return 1;
    }

    public static void main(String[] args) {
        CarSeller acess = new CarSeller();
        acess.colorArray[0] = "Black";
        acess.colorArray[1] = "Green";
        acess.colorArray[2] = "Red";
        acess.colorArray[3] = "Orange";
        acess.colorArray[4] = "Blue";
        acess.colorArray[5] = "Cyan";
        acess.colorArray[6] = "White";
        acess.colorArray[7] = "Brown";
        acess.colorArray[8] = "Yellow";
        acess.colorArray[9] = "Grey";

        System.out.println("Hey, what do you want to do ? | BUYCAR | DRIVE | GOINHOUSE | BUYPLOTS | REFUEL | QUIT |");
        Scanner start = new Scanner(System.in);

        if (start.equals("BUYCAR")) {
            System.out.println("What car do you want to buy? | Lamborghini | Porsche | VW | Mercedes | BMW  | Tesla | CANCEL|");
            Scanner a = new Scanner(System.in);
            
            if (a.equals("Lamborghini")) {
                
                  CarSeller Lamborghini = new CarSeller();
                  System.out.println("Which color do you want ? | Choose one ");
                  System.out.println(Arrays.toString(acess.colorArray));
                  System.out.println("Or do you want a random color ? | Random");
                  Scanner colorchoose = new Scanner(System.in);
                  
                  if (colorchoose.equals("Random")) {
                      System.out.println(acess.RandomColor());
                  }


            }

        }
    }

}

Bei mir wartet der irgendwie nicht auf eine Eingabe... Wie behebe ich diesen Fehler ?

PC, Computer, Technik, IT, programmieren, Java, Informatik
Android Studio: Wieso verändern die Elemente ihre Position?

Hey,

ich habe da ein kleines Problem. Auf dem PC sind alle Elemente dort, wo sie sein sollten, aber wenn ich die App auf dem Handy ausführe, sind die Elemente irgendwie bzw. irgendwo platziert. Kann mir jemand vielleicht sagen, woran es liegen könnte?

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/black_shade_1"
  tools:context=".MainActivity">
  <ImageView 
    android:layout_width="152dp"
    android:layout_height="185dp" 
    app:srcCompat="@drawable/iconqrlogo" 
    android:id="@+id/imageView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="32dp"
    app:layout_constraintHorizontal_bias="0.498" />
  <ProgressBar
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar"
    android:visibility="visible"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    android:layout_marginTop="56dp"
    app:layout_constraintTop_toBottomOf="@+id/imageView" />
  <Button
    android:text="@string/generateButton"
    android:layout_width="250dp"
    android:layout_height="60dp"
    android:id="@+id/generateQrCode"
    app:backgroundTint="@color/yellow"
    android:textColor="#000000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintHorizontal_bias="0.496"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="160dp" />
  <Button
    android:text="@string/scanCode"
    android:layout_width="250dp"
    android:layout_height="60dp"
    android:id="@+id/scanQrCode"
    app:backgroundTint="@color/yellow"
    android:textColor="#000000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/progressBar"
    android:layout_marginTop="48dp"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintBottom_toTopOf="@+id/generateQrCode"
    app:layout_constraintVertical_bias="0.061" />
</androidx.constraintlayout.widget.ConstraintLayout>
Bild zum Beitrag
Computer, App, programmieren, Java, Android, Android App, Android Studio

Meistgelesene Fragen zum Thema Java