Methode aus Klasse eins in Klasse zwei verwenden?

ezio

Mitglied
Hallo hoffe ihr könnt mir bei dem Problem helfen

Ich habe eine Klasse die eine Verzeichnis Struktur anlegt. In der Zweiten befindet sich ein JButton und die main Methode. Ich bekomme es nicht hin das der Button die Methode aufruft.

Das ist die erste Klasse
Java:
import java.io.File;

import javax.swing.JOptionPane;

	public class RomVerzeichnis {

		 public void erstellen()  {
		        
	    	
	    	
	    	String hilf = JOptionPane.showInputDialog("Pfand für Roms angeben");
	    	
	    	
	    	String path = hilf;
	        String dirROMS = "roms";
	        File dir = new File(path + "/" + dirROMS);
	        dir.mkdir();
	      
			System.out.println(dir);												       
			
			String path2 = path + "/" + dirROMS;
			String dirNES = "nes";
			File dir2 = new File(path2 + "/" + dirNES);
			dir2.mkdir();
			System.out.println("****nes****" +dir2);
	 
			//String path2 = path + dir;
			String dirSNES = "snes";
			File dir3 = new File(path2 + "/" + dirSNES);
			dir3.mkdir();
			System.out.println("SNES*******"+dir3);
	    
			String dirGB = "Gameboy";
			File dir4 = new File(path2 + "/" + dirGB);
			dir4.mkdir();
			System.out.println("GAMEBOY*******"+dir4);
	    
	    
	    
	    
	    
	    
	    }
	}

das die zweite

Java:
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class gui  {

	


private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			RomVerzeichnis Romv = new RomVerzeichnis();
			
			public void run() {
				try {
					gui window = new gui();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public gui() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JButton btnNewButton = new JButton("New button");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Romv.erstellen();
			}
		});
		frame.getContentPane().add(btnNewButton, BorderLayout.CENTER);
	}

}
 
Du deklarierst und erzeugst deine Variable "RomV" innerhalb der main-Methode. Daher ist diese Variable auch nur in diesem Scope gültig und du kannst in der Methode initialize nicht darauf zugreifen.

Deklariere die Variable RomV als Klassenattribut dann sollte es funktionieen 🙂
 
Dafür verwendet man Getter- und Setter-Methoden

Java:
public class Foo {
    private int i = 512;

    public Foo(int i) { this.i = i; }

    public int getI() {
        return i;
    }

    public void setI(int value) {
        this.i = value;
    }
}

public class Bar {
 
        private final Foo m_foo;

    public Bar(Foo m_foo) {
        this.m_foo = m_foo;
    }

    void doSomething() {
        System.out.println(m_foo.getI());
    }
}
 

Neue Themen


Zurück
Oben