Frames schließen

Status
Nicht offen für weitere Antworten.

schlydi

Mitglied
Hallo,

ich habe folgendes Problem: mit einem Thread starte ich einen JFrame, mit dem man bestimmte Dinge erledigen kann, die beinhalten, daß ich zwischen zwei Frames hin- und herwechsle, indem der eine geschlossen und der andere neu erzeugt wird - geht immer abwechselnd. Nur würde ich gerne, nachdem eine bestimmte Zeit vergangen ist, ein komplett anderes Fenster aufrufen (klappt) und das Fenster, was gerade offen ist, mittels dispose() entfernen. Wie kann ich den Frame ermitteln? Alle meine Frames sind eigene Klassen, wie auch der Thread.

Gruß,
Schlydi
 
der Thread/Timer/whatever der das neue Fenster öffnet braucht die Referenzen auf beide anderen JFrames um auf beide dispose aufzurufen. Für genauere Infos müssten wird deinen Code sehen.

Mach mal ein Kskb...
warum das denn? Die Frage hier bezieht sich auf die Architektur des Programmes und nicht darum, irgendein unerklärbares Verhalten in Java klarzustellen. Der richtige Code ist sinvoller.
 
In deinem Thread musst du doch dann die Steuerung bzgl. der Anzeige der derzeitigen Frames haben (setVisable) haben. Einfach dann auf dispose setzen und neues Frame anzeigen. Vielleicht solltest du Code zeigen von dem besagten Thread bzw. den Klassen


Edit
Drei Leute zur gleichen Zeit... 😉
 
Mein Thread:
Java:
package presentation;

import java.awt.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Timer;

import javax.swing.*;

public class MyThread extends Thread {
	
	Timer timerS;
	Timer timerE;
	JPanel p;
	JLabel l1;
	JFrame jf;
	Calendar c;
	Date s;
	Date e;
	String uhrzeit;
	
	public TryThread() {
		
	}
	
	public TryThread(Date s, Date e)  {	
		this.s = s;
		this.e = e;
		
		timerS = new Timer();	
		timerS.schedule(new StartTask(), s);
	}	
	
	
	public void buildFrame() {
		
		jf = new JFrame("Uhrzeit");
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		p = new JPanel();
		l1 = new JLabel();
		
		l1.setHorizontalAlignment(SwingConstants.CENTER);
		
		l1.setFont(new Font("Arial", Font.BOLD, 18));
		
		p.setBorder(BorderFactory.createEmptyBorder(5, 25, 5, 25));
		p.add(l1);
		
		jf.add(p);
		
		jf.setSize(576, 300);
		jf.setVisible(true);
		
		for(;true; ) {
			getDTime();
		}
		
	}
	
	public void getDTime() {
		
		long cTime = System.currentTimeMillis();
		long sTime = s.getTime();
		
		long diff = sTime - cTime; 
		Date dd = new Date();
		dd.setTime(diff);
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
		
		uhrzeit = sdf.format(dd);
		l1.setText("<html><center>Der Frame wird in<br><b>"+uhrzeit+" Stunden</b><br>aufgerufen</html>");
		
	}
	
	
	class StartTask extends TimerTask {	
		
		public void run() {
			
			try {
				String[] args = null;
				CodeWindow.main(args);	
				jf.setVisible(false);
				timerE = new Timer();
				timerE.schedule(new EndTask(), e);
				timerS.cancel();	
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
	
	class EndTask extends TimerTask {
		public void run() {
			String[] args = null;
			try {
				jf.dispose(); <--- hier müßte dann die Referenz zu meinem Frame hin...
				MainWindow.main(args);
				timerE.cancel();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	public static void main(String[] args) {
		
	}
}

CodeWindow:
Java:
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

import javax.swing.*;

public class CodeWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	static CodeWindow show;
	String contentCodeField;
	JTextField tf = new JTextField(1);
	JLabel text1;
	JLabel text2;
	

	public CodeWindow(String name) {
		super(name);
		setResizable(false);
	}
	
	public void build() throws IOException {
		
		Container pane = getContentPane();
		
		GridLayout gl = new GridLayout(3,0);
		JPanel p0 = new JPanel();
		JPanel p1 = new JPanel(gl);
		text1 = new JLabel("Your Code");
		text2 = new JLabel("Bitte geben Sie Ihren Code ein:");
		JButton j;
		
		gl.setHgap(15);
		gl.setVgap(15);
		
		text1.setFont(new Font("Arial", Font.BOLD, 24));
		text2.setFont(new Font("Arial", Font.PLAIN, 18));
		text2.setHorizontalAlignment(SwingConstants.CENTER);
		tf.setFont(new Font("Arial", Font.PLAIN, 18));
		//final String code = tf.getText();
		
		p0.setPreferredSize(new Dimension(500,60));
		p0.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
		p0.add(text1);
		
		p1.setPreferredSize(new Dimension(100,230));
		p1.setBorder(BorderFactory.createEmptyBorder(15, 100, 50, 100));
		p1.add(text2);
		p1.add(tf);
		j = new JButton("prüfen");
		j.setFont(new Font("Arial", Font.BOLD, 18));
		VerifyC vc = new VerifyC();
		j.addActionListener(vc);
		p1.add(j);
		
		pane.add(p0, BorderLayout.NORTH);
		pane.add(p1, BorderLayout.CENTER);

	}

	public void createAndShow() throws IOException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //rufe Methode build auf 
        build();
        //Fenster anzeigen
        pack();
        setVisible(true);
	}
	
	public class VerifyC implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			contentCodeField = tf.getText();
			try {
				VerifyCode vc = new VerifyCode(contentCodeField);
				if (vc.verifyC()) {
					String[] args = null;
					Ballot.main(args);
					dispose();
				}
				else {
					tf.setText("");
					text2.setText("<html><center>Fehler im Code<br>bitte geben Sie den Code erneut ein!</html>");
					text2.setForeground(Color.RED);
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
	}

}

Und Ballot:
Java:
package presentation;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

import javax.imageio.ImageIO;
import javax.swing.*;

import application.*;

public class Ballot extends JFrame {
		

		private static final long serialVersionUID = 1L;
		static final UIDefaults uidef;
		
		static {
	        uidef = UIManager.getDefaults();
	        uidef.put( "ToggleButton.select", Color.RED );
	    }

		GridLayout gl = new GridLayout(0,2);
		GridLayout gl2 = new GridLayout(9,0);
		
		AbstractButton[] tb = new AbstractButton[8];
		AbstractButton eins, zwei;
		AbstractButton[] ab = new AbstractButton[2];
		
		ButtonGroup bg1, bg2;		
		ButtonModel bm1, bm2;
		JPanel p0, p1, p2, p3, p4;
                JLabel sz1, sz2, sz3, es, zs;
        
		String [][]cd = new String[7][3]; //kommt aus der Klasse Cast_ballot
		String [][]list = new String[7][2];      //ebenfalls
		String[] sab = new String[2];
		
		int i;
		boolean klicked;
        
		
		Map<ButtonModel, AbstractButton> map1 = new HashMap<ButtonModel, AbstractButton>();
		Map<ButtonModel, AbstractButton> map2 = new HashMap<ButtonModel, AbstractButton>();
		
		static Ballot show;

		    
		public Ballot(String name) {
		    super(name);
		    setResizable(false);
		}
		    
		public void build() throws IOException, SQLException {

	        Container pane = getContentPane();
	        
	        //hier folgt jetzt ein Haufen an GUI-Elementen

	        //wichtig vielleicht das hier: Button, der Decorator aufruft
	        JButton se = new JButton("Decorator");
	        se.setForeground(Color.red);
	        se.setFont(new Font("Arial", Font.BOLD, 20));
	        se.setFocusPainted(false);
			
	        GetDecorator gd = new GetDecorator();
	        se.addActionListener(gd);
	        p4.setPreferredSize(new Dimension(50,50));
	        p4.add(se);

	        //fuege alles zusammen
	        pane.add(p0, BorderLayout.NORTH);
	        pane.add(p1, BorderLayout.CENTER);
	        pane.add(p4, BorderLayout.SOUTH);
	        
	    }
		
		//ActionListener ruft die Klasse Decorator auf
		public class GetDecorator implements ActionListener {
			
			AbstractButton eins;
			AbstractButton zwei;
			
			public void actionPerformed(ActionEvent e) {
				AbstractButton[] asb = getVote();
								
				if ((asb[0] == null && klicked == false) || 
					(asb[1] == null && klicked == false) ||
					(asb[0] != null && asb[1] != null)) {
				
				try {
					d = new Decorator(show, asb, l1, l2, l3, p4, map1, map2, klicked);
					d.createAndShow();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
		
		
	    public void createAndShow() throws IOException, SQLException {
	       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	       build();
	       pack();
	       setVisible(true);
	    }
	    
	    /**
		 * @return ab
		 */
	    //liefert die Auswahl der Buttons
	    public AbstractButton[] getVote() {
	    	
	    	bm1 = bg1.getSelection();
			eins = map1.get(bm1);
			bm2 = bg2.getSelection();
	        zwei = map2.get(bm2);
	        
	        if (bm2 == null) {
	        	ab[0] = eins;
	        	}
	        
	        else if (bm1 == null) {
	        	ab[1] = zwei;
	        	}
	        
	        else {
	        	ab[0] = eins;
	        	ab[1] = zwei;
	        	}
	        System.out.println(bm1);
	        System.out.println(bm2);
	        return ab;
	        
	    }
}

und zur Vervollständigung den Decorator

Java:
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

import javax.swing.*;



public class Decorator extends Ballot {
	
	private static final long serialVersionUID = 1L;
	
	Ballot bt;
	AbstractButton[] asb;
	JLabel l1, l2, l3;
	JPanel p;
	Map<ButtonModel, AbstractButton> map1, map2;
	JButton b;
	String text;
	boolean klicked;
	
	//Constructor, hier werden die zu aendernden Argumente uebergeben
	public Decorator(Ballot bt, AbstractButton[] asb, JLabel l1, JLabel l2, JLabel l3, JPanel panel, Map<ButtonModel, AbstractButton> map1,	Map<ButtonModel, AbstractButton> map2, boolean klicked) {
		super("Decorator");
		this.bt = bt;
		this.asb = asb;
		this.l1 = l1;
		this.l2 = l2;
		this.l3 = l3;
		this.p = panel;
		this.map1 = map1;
		this.map2 = map2;
		this.klicked = klicked;
	}
	
	//hier werden die Elemente geändert
	public void build() throws IOException {
		
		
	}
	
	public void createAndShow() throws IOException {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //rufe Methode build auf 
        build();
        //Fenster anzeigen
        pack();
        setVisible(false);
    }
	
	public void changeLabel(JLabel label, String text) {
		label.getParent().invalidate();
		label.setText(text);
		label.setForeground(Color.red);
		label.getParent().validate();
		pack();
	}
	
	public class UpdateDB implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			CastBallot cb = new CastBallot();
			String partyC = asb[0].getName();
			String partyL = asb[1].getName();
			try {
				cb.updateDB("CANDIDATE", partyC);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			try {
				cb.updateDB("LIST", partyL);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			bt.dispose(); <---- Ballot wird geschlossen
			String[] args = null;
			try {
				CodeWindow.main(args); <----- CodeWindow wird aufgerufen
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
		}
	}
	
	public class NewBallot implements ActionListener {
		
		public void actionPerformed(ActionEvent e) {
			
			bt.dispose();
			String[] args = null;
			try {
				Ballot.main(args);
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (SQLException e2) {
				e2.printStackTrace();
			} 
		}	
	}
	
	
	
	public static void main(String[]args) throws IOException {
		
	}
}
 
Mit grossen Mengen nicht kompilierbarem Code kann man imho schwer etwas anfangen. "CodeWindow", "Ballot" und "Decorator" sind in der Tat alles JFrames, aber in der Methode "TryThread#buildFrame" wird ein standard JFrame angelegt (keine eigene Klasse, wie du behauptest). Unklar ist auch, wo diese Methode überhaupt aufgerufen wird. Vielleicht könntest du entweder dein Problem genauer beschreiben, oder - noch besser - einfach ein kurzes, selbständiges und kompilierbares Beispiel bauen, wie oben vorgeschlagen wurde. Letzteres ist zugegebenermassen etwas aufwendig, dürfte aber trotzdem am schnellsten zur optimalsten Lösung führen.
 
Zuletzt bearbeitet:
Habe jetzt kompilierbaren Code - der Thread:
Java:
import java.awt.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Timer;

import javax.swing.*;

public class MyThread extends Thread {
	
	Timer timerS;
	Timer timerE;
	JPanel p;
	JLabel l1;
	JLabel l2;
	JFrame jf;
	Calendar c;
	Date s;
	Date e;
	String uhrzeit;
	static MyThread mt;
	
	public MyThread()  {
		
		s = new Date();
		s.setTime(System.currentTimeMillis() + 15000);
		timerS = new Timer();
		timerS.schedule(new StartTask(), s);
	}
	
	
	public void buildFrame() {
		
		GridLayout g = new GridLayout(2,1);
		jf = new JFrame("Uhrzeit");
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		p = new JPanel();
		l1 = new JLabel("Wir warten auf den Beginn des Threads");
		l2 = new JLabel();
		
		l1.setHorizontalAlignment(SwingConstants.CENTER);
		l2.setHorizontalAlignment(SwingConstants.CENTER);
		
		l1.setFont(new Font("Arial", Font.BOLD, 18));
		l2.setFont(new Font("Arial", Font.PLAIN, 14));
		
		g.setHgap(18);
		p.setLayout(g);
		p.setBorder(BorderFactory.createEmptyBorder(5, 25, 5, 25));
		p.add(l1);
		p.add(l2);
		
		jf.add(p);
		
		jf.setSize(576, 300);
		jf.setVisible(true);
		
		for(;true; ) {
			getDTime(); // Endlosschleife
		}
		
	}
	
	public void getDTime() {
		
		long cTime = System.currentTimeMillis();
		long sTime = s.getTime();
		
		long diff = sTime - cTime; 
		Date dd = new Date();
		dd.setTime(diff);
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
		
		uhrzeit = sdf.format(dd);
		l2.setText("<html><center>Der Thread wird in<br><b>"+uhrzeit+" Stunden</b><br>beginnen</html>");
		
	}
	
	class StartTask extends TimerTask {		
		
		public void run() {	
			e = new Date();
			e.setTime(System.currentTimeMillis() + 20000);
			
			try {
				String[] args = null;
				Window1.main(args);	
				jf.setVisible(false);
				timerE = new Timer();
				timerE.schedule(new EndTask(), e);
				timerS.cancel();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	};
	
	class EndTask extends TimerTask {
		public void run() {
			String[] args = null;
			jf.dispose();
			Window3.main(args);
			timerE.cancel();
			
		}
	}
	
	public static void main(String[] args) {
		mt = new MyThread();
		mt.buildFrame();
	}
}

Und dann die vier Windows:
Java:
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

import javax.swing.*;

public class Window1 extends JFrame {

	private static final long serialVersionUID = 1L;
	static Window1 show;
	String contentCodeField;
	JTextField tf = new JTextField(1);
	JLabel text1;
	JLabel text2;
	

	public Window1(String name) {
		super(name);
		setResizable(false);
	}
	
	public void build() throws IOException {
		
		Container pane = getContentPane();
		
		GridLayout gl = new GridLayout(3,0);
		JPanel p0 = new JPanel();
		JPanel p1 = new JPanel(gl);
		text1 = new JLabel("Your Code");
		text2 = new JLabel("Geben Sie ein, was sie wollen:");
		JButton j;
		
		gl.setHgap(15);
		gl.setVgap(15);
		
		text1.setFont(new Font("Arial", Font.BOLD, 24));
		text2.setFont(new Font("Arial", Font.PLAIN, 18));
		text2.setHorizontalAlignment(SwingConstants.CENTER);
		tf.setFont(new Font("Arial", Font.PLAIN, 18));
		
		p0.setPreferredSize(new Dimension(500,60));
		p0.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
		p0.add(text1);
		
		p1.setPreferredSize(new Dimension(100,230));
		p1.setBorder(BorderFactory.createEmptyBorder(15, 100, 50, 100));
		p1.add(text2);
		p1.add(tf);
		j = new JButton("überprüfen");
		j.setFont(new Font("Arial", Font.BOLD, 18));
		VerifyC vc = new VerifyC();
		j.addActionListener(vc);
		p1.add(j);
		
		pane.add(p0, BorderLayout.NORTH);
		pane.add(p1, BorderLayout.CENTER);

	}

	public void createAndShow() throws IOException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //rufe Methode build auf 
        build();
        //Fenster anzeigen
        pack();
        setVisible(true);
	}
	
	public class VerifyC implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			String[] args = null;
				try {
					Window2.main(args);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				dispose();
		}
		
	}
	
	public static void main(String[] args) throws IOException {
		show = new Window1("Window1");
		show.createAndShow();
		
	}
}

Java:
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;

import javax.swing.*;


public class Window2 extends JFrame {
		

		private static final long serialVersionUID = 1L;
		static final UIDefaults uidef;
		
		static {
	        uidef = UIManager.getDefaults();
	        uidef.put( "ToggleButton.select", Color.RED );
	    }

		GridLayout gl = new GridLayout(0,2);
		GridLayout gl2 = new GridLayout(6,0);
		
		AbstractButton[] tb = new AbstractButton[5];
		AbstractButton eins, zwei;
		AbstractButton[] ab = new AbstractButton[2];
		
		ButtonGroup bg1, bg2;
		
		ButtonModel bm1, bm2;
		
		JPanel p0, p1, p2, p3, p4;
        
        JLabel sz3, es, zs;
        
		int i;
		boolean klicked;
        
		
		Map<ButtonModel, AbstractButton> map1 = new HashMap<ButtonModel, AbstractButton>();
		Map<ButtonModel, AbstractButton> map2 = new HashMap<ButtonModel, AbstractButton>();
		
		static Window2 show;

		    
		public Window2(String name) {
		    super(name);
		    setResizable(false);
		}
		    
		public void build() throws IOException {

	        Container pane = getContentPane();
	        
	        sz3 = new JLabel();
	        sz3.setText("Dies Label wird verändert");
	        
	        sz3.setFont(new Font("Arial", Font.PLAIN, 28));
	        sz3.setHorizontalAlignment(SwingConstants.CENTER);
	        
	        //Panel fuer die Beschreibung
	        p0 = new JPanel();
	        p0.setPreferredSize(new Dimension (50, 50));
	        p0.add(sz3);
	        
	        es = new JLabel("dieses auch");
	        es.setFont(new Font("Arial", Font.BOLD, 12));
	        
	        //Panel li
	        add(p2 = new JPanel(gl2));
	        bg1 = new ButtonGroup();
	        p2.setBorder(BorderFactory.createEmptyBorder(5,50,5,5));
	        p2.add(es);
	        
	        //erstelle Buttons mittels Array
	        for (i=0; i<5; i++) {
	        	tb[i] = new JToggleButton("linker Button " + i);
	    		tb[i].setActionCommand("li" + i);
	    		tb[i].setName("li" + i);
	    		tb[i].setFocusPainted(false);
	    		bg1.add(tb[i]);
	    		map1.put(tb[i].getModel(), tb[i]);
	    		tb[i].setFont(new Font("Arial", Font.PLAIN, 11));	
	    		tb[i].setHorizontalAlignment(SwingConstants.LEFT);
	    		p2.add(tb[i]);		
	    	}
	        
	        zs = new JLabel("oder das");
	        zs.setFont(new Font("Arial", Font.BOLD, 12));
	        zs.setForeground(new Color(65,105,225));
	        
	        //Panel re
	        add(p3 = new JPanel(gl2));
	        bg2 = new ButtonGroup();
	        p3.setBorder(BorderFactory.createEmptyBorder(5,5,5,50));
	        p3.add(zs);
	        
	        //erstelle Buttons mittels Array
	        for (i=0; i<5; i++) {
	        	tb[i] = new JToggleButton("rechter Button " + i);
	    		tb[i].setActionCommand("re" + i);
	    		tb[i].setName("re" + i);
	    		tb[i].setFocusPainted(false);
	    		bg2.add(tb[i]);
	    		map2.put(tb[i].getModel(), tb[i]);
	    		tb[i].setFont(new Font("Arial", Font.PLAIN, 11));	
	    		tb[i].setForeground(new Color(65,105,225));
	    		tb[i].setHorizontalAlignment(SwingConstants.LEFT);
	    		p3.add(tb[i]);		
	    	}
	        
	        gl2.setVgap(5); //setze vertikale Luecke
	        
	        p1 = new JPanel();
	        p1.setLayout(gl);
	        p1.setPreferredSize(new Dimension(570,300));
	        gl.setHgap(25); //setze horizontale Luecke
	        p1.add(p2);
	        p1.add(p3);
	        
	        //erstelle Button "Stimmzettel einreichen"
	        JButton se = new JButton("Decorator");
	        se.setForeground(Color.red);
	        se.setFont(new Font("Arial", Font.BOLD, 20));
	        se.setFocusPainted(false);
			
	        GetDecorator gd = new GetDecorator();
	        se.addActionListener(gd);
	        p4 = new JPanel();
	        p4.setPreferredSize(new Dimension(50,50));
	        p4.add(se);

	        //fuege alles zusammen
	        pane.add(p0, BorderLayout.NORTH);
	        pane.add(p1, BorderLayout.CENTER);
	        pane.add(p4, BorderLayout.SOUTH);
	        
	    }
		
		//ActionListener ruft den Decorator auf
		public class GetDecorator implements ActionListener {
			
			AbstractButton eins;
			AbstractButton zwei;
			
			public void actionPerformed(ActionEvent e) {
				
				AbstractButton[] asb = getSelButton();
				Window2D d;
				
				JLabel l1 = sz3;
				JLabel l2 = es;
				JLabel l3 = zs;
								
				if ((asb[0] == null && klicked == false) || 
					(asb[1] == null && klicked == false) ||
					(asb[0] != null && asb[1] != null)) {
				
				try {
					d = new Window2D(show, asb, l1, l2, l3, p4, map1, map2, klicked);
					d.createAndShow();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				}
			}
		}
		
		
		//erstellt neuen Stimmzettel
	    public void createAndShow() throws IOException {
	       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	       build();
	       pack();
	       setVisible(true);
	    }
	    
	    /**
		 * @return ab
		 */
	    //liefert die Auswahl der Buttons
	    public AbstractButton[] getSelButton() {
	    	
	    	bm1 = bg1.getSelection();
			eins = map1.get(bm1);
			bm2 = bg2.getSelection();
	        zwei = map2.get(bm2);
	        
	        if (bm2 == null) {
	        	ab[0] = eins;
	        	}
	        
	        else if (bm1 == null) {
	        	ab[1] = zwei;
	        	}
	        
	        else {
	        	ab[0] = eins;
	        	ab[1] = zwei;
	        	}
	        System.out.println(bm1);
	        System.out.println(bm2);
	        return ab;
	        
	    }
	    
	    public static void main(String[] args) throws IOException {
	        //rufe Methode createAndShow auf
	    	show = new Window2("Window2");
	    	show.createAndShow();
	    }
}

Java:
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;

import javax.swing.*;



//Klasse Decorator aendert Aussehen und Funktionen einzelner Elemente der Klasse Ballot
public class Window2D extends Window2 {
	
	private static final long serialVersionUID = 1L;
	
	Window2 w2;
	AbstractButton[] asb;
	JLabel l1;
	JLabel l2;
	JLabel l3;
	JPanel p;
	Map<ButtonModel, AbstractButton> map1;
	Map<ButtonModel, AbstractButton> map2;
	JButton b;
	String text;
	boolean klicked;
	
	//Constructor, hier werden die zu aendernden Argumente uebergeben
	public Window2D(Window2 w2, AbstractButton[] asb, JLabel l1, JLabel l2, JLabel l3, JPanel panel, Map<ButtonModel, AbstractButton> map1,	Map<ButtonModel, AbstractButton> map2, boolean klicked) {
		super("Window2D");
		this.w2 = w2;
		this.asb = asb;
		this.l1 = l1;
		this.l2 = l2;
		this.l3 = l3;
		this.p = panel;
		this.map1 = map1;
		this.map2 = map2;
		this.klicked = klicked;
	}
	
	//Methode build() aus der Klasse Ballot wird ueberschrieben
	public void build() throws IOException {
		
		System.out.println(klicked);
		
		AbstractButton[] abArr1;
		Collection<AbstractButton> values1 = map1.values();
		int x = map1.size();
		abArr1 = values1.toArray(new AbstractButton[x]);
		
		AbstractButton[] abArr2;
		Collection<AbstractButton> values2 = map2.values();
		int y = map2.size();
		abArr2 = values2.toArray(new AbstractButton[y]);
		
		if (asb[0] == null && asb[1] == null) { 
			text = "Verändertes Label 1";
			changeLabel(l1, text);  
		}
		
		else if (asb[0] == null && klicked == false) {
			text = "Verändertes Label 1";
			changeLabel(l1, text);
			text ="Verändertes Label rechts";
			changeLabel (l3, text);
			asb[1].setFont(new Font("Arial", Font.BOLD, 11));
			for (int i = 0; i < y; i++) {
				abArr2[i].setEnabled(false);
			}
			
			GridLayout g = new GridLayout(2,1);
			g.setVgap(10);
			
			b = new JButton("Auswahl löschen");
			b.setFont(new Font("Arial", Font.BOLD, 18));
			b.setPreferredSize(new Dimension(50,50));
			b.setFocusPainted(false);
			NewBallot nb = new NewBallot();
			b.addActionListener(nb);
			p.add(b);
			
			p.setLayout(g);
			p.setPreferredSize(new Dimension(50,90));
			p.setBorder(BorderFactory.createEmptyBorder(5, 155, 10, 155));
			w2.setSize(575, 473);
			
			w2.klicked = true;
		}
		
		else if (asb[1] == null && klicked == false) {
			text = "Verändertes Label 1";
			changeLabel(l1, text);
			text = "Verändertes Label links";
			changeLabel (l2, text);
			asb[0].setFont(new Font("Arial", Font.BOLD, 11));
			for (int i = 0; i < x; i++) {
				abArr1[i].setEnabled(false);
			}
			
			GridLayout g = new GridLayout(2,1);
			g.setVgap(10);
			
			b = new JButton("Auswahl löschen");
			b.setFont(new Font("Arial", Font.BOLD, 18));
			b.setPreferredSize(new Dimension(50,50));
			b.setFocusPainted(false);
			NewBallot nb = new NewBallot();
			b.addActionListener(nb);
			p.add(b);
			
			p.setLayout(g);
			p.setPreferredSize(new Dimension(50,90));
			p.setBorder(BorderFactory.createEmptyBorder(5, 155, 10, 155));
			w2.setSize(575, 473);
			
			w2.klicked = true;
			
		}
		
		else {
			text = "Verändertes Label 1";
			changeLabel(l1, text);
			text = "Verändertes Label links";
			changeLabel (l2, text);
			text ="Verändertes Label rechts";
			changeLabel (l3, text);
			
			asb[0].setFont(new Font("Arial", Font.BOLD, 11));
			for (int i = 0; i < x; i++) {
				abArr1[i].setEnabled(false);
			}
			
			asb[1].setFont(new Font("Arial", Font.BOLD, 11));
			for (int i = 0; i < y; i++) {
				abArr2[i].setEnabled(false);
			}
			
			GridLayout g = new GridLayout(2,1);
			g.setVgap(10);
			
			p.removeAll();
			p.repaint();
			p.setLayout(g);
			p.setPreferredSize(new Dimension(50,90));
			p.setBorder(BorderFactory.createEmptyBorder(5, 155, 10, 155));
			w2.setSize(575, 473);
			
			b = new JButton("Window 1 aufrufen");
			b.setFont(new Font("Arial", Font.BOLD, 18));
			b.setFocusPainted(false);
			getW1 gw1 = new getW1();
			b.addActionListener(gw1);
			p.add(b);
			
			b = new JButton("Auswahl löschen");
			b.setFont(new Font("Arial", Font.BOLD, 18));
			b.setFocusPainted(false);
			NewBallot nb = new NewBallot();
			b.addActionListener(nb);
			p.add(b);
				
		}
		
	}
	
	public void createAndShow() throws IOException {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //rufe Methode build auf 
        build();
        //Fenster anzeigen
        pack();
        setVisible(false);
    }
	
	public void changeLabel(JLabel label, String text) {
		label.getParent().invalidate();
		label.setText(text);
		label.setForeground(Color.red);
		label.getParent().validate();
		pack();
	}
	
	public class getW1 implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			w2.dispose();
			String[] args = null;
			try {
				Window1.main(args);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
		}
	}
	
	public class NewBallot implements ActionListener {
		
		public void actionPerformed(ActionEvent e) {
			
			w2.dispose();
			String[] args = null;
			try {
				Window2.main(args);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}	
	}
	
	
	
	public static void main(String[]args) throws IOException {
		
	}
}

Java:
import java.awt.Container;

import javax.swing.*;

public class Window3 extends JFrame {
	private static final long serialVersionUID = 1L;
	static Window3 show;
	
	public Window3() {
		super("Thread-Ende");
		setResizable(true);
	}
	
	public void build() {
		JPanel p = new JPanel();
		JLabel l = new JLabel("Dies ist das Thread-Ende");
		Container c = getContentPane();
		p.add(l);
		c.add(p);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    pack();
	    setVisible(true);
	}

	public static void main(String[] args) {
		show = new Window3();
		show.build();
	}
}

Das ist in etwa meine Struktur. Ich würde halt gerne bei Erscheinen von Window 3 das Fenster schließen, das gerade noch offen ist. Aber da ich ja zum Beispiel nur Window1 aus dem Thread heraus starte, das dann eben Window2 aufruft, weiß ich nicht, wie ich eine Referenz darauf haben kann. Und da ich ja immer wieder neue Fenster aufrufe (nur den Inhalt zu löschen und den Frame beizubehalten funktioniert auch nicht, schon ausprobiert), wäre ja Window1 im Thread nicht das Window1, was ich sehe, oder doch????:L
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben