LayoutManager Erster Versuch mit Cardlayout

Enigma228

Bekanntes Mitglied
Ziel: ein Dialogfeld im Borderlayout wobei im Center die Panels mittels eines CardLayout ausgetauscht werden sollen
Problem: Er zeigt die Karten nicht an. Das Centerfeld bleibt leer. Weiterhin möchte ich im TitelPanel (Norden) das Label zentriert wird, was nicht macht

Angezeigt werden aber Titel-Panel und Button-Panel, so dass ich vermute, dass ich einen Fehler beim CardLayout gemacht habe.. aber wo??


Java:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import myComponents.PrefilledTextfield;

public class FirstSettings extends JDialog implements ActionListener {

	private Color user_yellow, user_red, user_gray;
	private JLabel l_info, l_title;
	private JButton b_next, b_cancel, b_back;
	private JPanel p_title, p_dialog, p_buttons, p_first, p_second, p_third, p_fourth, p_fifth, p_sixth;
	private String[] info;
	private int step;
	private PrefilledTextfield ptf_name, ptf_mail, ptf_pop, ptf_smtp, ptf_accountname;
	private JPasswordField pf_access, pf_pass;
	
	public FirstSettings() {
		user_yellow = new Color(255,255,215);
		user_red = new Color(250, 210, 210);
		user_gray = new Color(212,208,200);

		this.setModal(true);
		this.setLocationRelativeTo(null);
		this.setLayout(new BorderLayout());
		this.setSize(600, 300);
		this.getContentPane().setBackground(user_gray);
		this.setTitle("Einrichtung ihres MailServers");
		
		step = 0;		
		info = new String[]{"Herzlichen willkommen zur Ersteinrichtung ihres MailServers",
				"Bitte geben Sie den Profilnamen und optional ein Passwort ein.",
				"Bitte geben Sie ihre E-Mail-Adresse ein.",
				"E-Mail-Server einrichten:",// POP und SMTP
				"Geben Sie den Kontonamen und das Kennwort ein, was von Ihrem Internetdienstanbieter erhalten haben",
				"Die Einrichtung ist jetzt abgeschlossen"};
		
		// Titel-Panel im Norden des Dialogfeldes
		p_title = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
		l_title = new JLabel(info[0]);
		p_title.add(l_title);
		
		// Dialog-Panel im Center des Dialogfeldes
		// auf ihm sollen die Karten liegen (z.B. p_first usw.)
		p_dialog = new JPanel(new CardLayout(10,10));
		
		// Karten auf dem Dialog-Panel
			// 1. Karte 
		p_first = new JPanel(new BorderLayout());
		p_first.add(new JLabel(info[0]),BorderLayout.CENTER);
			// 2. Karte
		p_second = new JPanel(new GridLayout(2,2,5,5));
		ptf_name = new PrefilledTextfield("Name des Profils");
		pf_access = new JPasswordField();
		pf_access.setEchoChar('*');
		pf_access.setToolTipText("Wenn keine Eingabe erfolgt, erfolgt der Zugriff auf das Profil ohne Passwort");
		p_second.add(new JLabel("Name des Profils"));
		p_second.add(ptf_name);
		p_second.add(new JLabel("Zugangspasswort zum Profil"));
		p_second.add(pf_access);
			// 3. Karte
		p_third = new JPanel();
		ptf_mail = new PrefilledTextfield("E-Mail-Adresse");
		p_third.add(ptf_mail);
			// 4. Karte
		p_fourth = new JPanel(new GridLayout(2,2,5,5));
		ptf_pop = new PrefilledTextfield("Posteingangsserver");
		ptf_smtp = new PrefilledTextfield("Postausgangsserver");
		p_fourth.add(new JLabel("Posteingangsserver (z.B. pop.gmx.net)"));
		p_fourth.add(ptf_pop);
		p_fourth.add(new JLabel("Postausgangsserver (z.B. mail.gmx.net)"));
		p_fourth.add(ptf_smtp);
			// 5. Karte
		p_fifth = new JPanel(new GridLayout(2,2,5,5));
		ptf_accountname = new PrefilledTextfield("Kontoname");
		pf_pass = new JPasswordField();
		pf_pass.setEchoChar('*');
		p_fifth.add(new JLabel("Kontoname "));
		p_fifth.add(ptf_accountname);
		p_fifth.add(new JLabel("Passwort "));
		p_fifth.add(pf_pass);
			// 6. Karte
		p_sixth = new JPanel();
		p_sixth.add(new JLabel(info[5]));
		
		// ButtonPanel im Süden des Dialogfeldes
		p_buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT,5,5));
		p_buttons.setBackground(user_gray);
			// Buttons
		b_back = new JButton("Zurück");
		b_back.addActionListener(this);
		b_next = new JButton("Weiter");
		b_next.addActionListener(this);
		b_cancel = new JButton("Abbrechen");
		b_cancel.addActionListener(this);
		
		p_buttons.add(b_back);
		p_buttons.add(b_next);
		p_buttons.add(b_cancel);
		
		p_dialog.add(p_first,"first");
		p_dialog.add(p_second,"second");
		p_dialog.add(p_third, "third");
		p_dialog.add(p_fourth, "fourth");
		p_dialog.add(p_fifth, "fifth");
		
		this.add(l_title, BorderLayout.NORTH);
		this.add(p_first, BorderLayout.CENTER);
		this.add(p_buttons,BorderLayout.SOUTH);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		CardLayout cl = (CardLayout)(p_dialog.getLayout());
		if(e.getSource()== b_next){
			step++;
			switch (step){
			case 0:	cl.show(p_dialog, "first");
					l_title.setText(info[0]);
					b_back.setEnabled(false);
				break;
			case 1: cl.show(p_dialog, "second");
					l_title.setText(info[1]);
					b_back.setEnabled(true);
				break;
			case 2:	cl.show(p_dialog, "third");
					l_title.setText(info[2]);
				break;
			case 3:	cl.show(p_dialog, "fourth");
					l_title.setText(info[3]);
				break;
			case 4:	cl.show(p_dialog, "fifth");
					l_title.setText(info[4]);
					b_next.setText("Weiter");
				break;
			case 5: cl.show(p_dialog, "sixth");
					l_title.setText("");
					b_next.setText("Speichern");
				break;
			case 6: //Profil hier noch speichern
					this.dispose();
				break; 
			}
		}
		
		if(e.getSource()== b_cancel){
			this.dispose();
		}
	}
}
 
In Zeile 118 musst du doch dann auch das Panel mit dem CardLayout in den Center adden (anstatt p_first)!
Also:
Java:
	this.add(p_dialog, BorderLayout.CENTER);
 

Neue Themen


Zurück
Oben