Verschachtelte JPanel



  • Guten Morgen.
    Ich bin gerade dabei mir eine kleine GUI zu bauen, für ein simples TicTacToe Spiel.
    Ich habe mir eine allgemeine Klasse (GuiPanel)geschrieben, die von JPanel abgeleitet ist. Diese Klasse macht nichts anderes als die Größe einzustellen und einen Verweis auf die Spiel - Engine sich zu merken.
    Diese Basisklasse hat 2 Kindklasse (ThreeWinsPlayingField und ThreeWinsField).
    Die Klasse ThreeWinsPlayingField soll einen Rahmen für die einzelnen 9 Felder darstellen. Das Zeichnen soll in der Klasse ThreeWinsField beschehen. Jede dieser 2 Klassen besitzt eine paint - Funktion. Nur wird sie für ThreeWinsField nie aufgerufen und ich weiß nicht wieso.
    Was habe ich schon versucht :
    Die 9 felder direkt im JFrame zu erzuegen (also ohne das ThreeWinsPlayingField) und da geht es .

    Hier mein COde :

    package GeneralGameGui;
    
    import java.awt.Dimension;
    import java.awt.event.MouseListener;
    
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public abstract class GuiPanel extends JPanel implements MouseListener{
    
    	public GuiFrame parentFrame	= null;
    
    	public GuiPanel(int width, int height, GuiFrame parentFrame){
    		setPreferredSize(new Dimension(width, height));
    		setParentFrame(parentFrame);
    		//setLayoutManager();		
    	}
    
    	public boolean setParentFrame(GuiFrame parent){
    		if (parent != null){
    			this.parentFrame	= parent;
    		}
    		return false;		
    	}
    
    	public abstract void setLayoutManager();
    
    	public void updateGuiPanel(){
    		this.validate();
    	}
    }
    
    package ThreeWins;
    
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowEvent;
    
    import ArtificialIntelligence.WorldException;
    import ArtificialIntelligence.WorldPlayer.PlayerColor;
    import GeneralGameGui.GuiFrame;
    import GeneralGameGui.GuiPanel;
    
    @SuppressWarnings("serial")
    public class ThreeWinsFrame extends GuiFrame{
    
    	private ThreeWinsWorld playingField	= null;
    
    	public ThreeWinsFrame(int width, int height){
    		super(width, height);
    
    		/* set resizeable false */
    		setResizable(false);
    
    		/* set visble true */
    		setVisible(true);
    
    		/* center teh window */
    		setLocationRelativeTo(null);		
    	}
    
    	public PlayerColor getFieldValue(int x, int y) throws WorldException{
    		if (playingField != null){
    			return playingField.getPlayingFieldValue(x, y);
    		}
    
    		return PlayerColor.NoPlayer;
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void windowOpened(WindowEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void windowClosing(WindowEvent e) {
    		/* finish the application */
    		System.exit(1);
    
    	}
    
    	@Override
    	public void windowClosed(WindowEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void windowIconified(WindowEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void windowDeiconified(WindowEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void windowActivated(WindowEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void windowDeactivated(WindowEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void setLayout() {
    		new BorderLayout(0, 0);
    
    		int sizePlayingField	= Math.min(getSize().width, getSize().height);
    		ThreeWinsPlayingField playingField	= new ThreeWinsPlayingField(sizePlayingField, sizePlayingField, this);
    		playingField.setLayoutManager();
    
    		add(playingField, BorderLayout.NORTH);
    
    	}
    
    	@Override
    	public void addGuiPanel(GuiPanel panel) {
    		panel.setParentFrame(this);
    		add(panel);
    	}
    
    	public static void main(String arg[]){
    		ThreeWinsFrame threeWins	= new ThreeWinsFrame(400, 400);
    
    	}
    
    }
    
    package ThreeWins;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.GridBagConstraints;
    import java.awt.GridLayout;
    import java.awt.event.MouseEvent;
    
    import GeneralGameGui.GuiPanel;
    
    @SuppressWarnings("serial")
    public class ThreeWinsPlayingField extends GuiPanel{
    
    	public ThreeWinsPlayingField(int width, int height, ThreeWinsFrame parentFrame) {
    		super(width, height, parentFrame);		
    	}
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void setLayoutManager() {
    		setLayout(new GridLayout(3, 3, 0, 0));
    		System.out.println(this.getClass().getName() + " SetLayoutManager");
    		int sizePlayingField	= Math.min(getSize().width, getSize().height) ;
    
    		ThreeWinsField tmpField	= null;
    		tmpField	= new ThreeWinsField(sizePlayingField, sizePlayingField, (ThreeWinsFrame) parentFrame, 0, 0);
    		tmpField.setLayoutManager();		
    		add(tmpField);
    
    		tmpField 	= new ThreeWinsField(sizePlayingField, sizePlayingField, (ThreeWinsFrame) parentFrame, 1, 0);
    		tmpField.setLayoutManager();		
    		add(tmpField);
    
    		tmpField 	= new ThreeWinsField(sizePlayingField, sizePlayingField, (ThreeWinsFrame) parentFrame, 2, 0);
    		tmpField.setLayoutManager();		
    		add(tmpField);
    
    		tmpField	= new ThreeWinsField(sizePlayingField, sizePlayingField, (ThreeWinsFrame) parentFrame, 0, 1);
    		tmpField.setLayoutManager();		
    		add(tmpField);
    
    		tmpField 	= new ThreeWinsField(sizePlayingField, sizePlayingField, (ThreeWinsFrame) parentFrame, 1, 1);
    		tmpField.setLayoutManager();		
    		add(tmpField);
    
    		tmpField 	= new ThreeWinsField(sizePlayingField, sizePlayingField, (ThreeWinsFrame) parentFrame, 2, 1);
    		tmpField.setLayoutManager();		
    		add(tmpField);
    
    		tmpField	= new ThreeWinsField(sizePlayingField, sizePlayingField, (ThreeWinsFrame) parentFrame, 0, 2);
    		tmpField.setLayoutManager();		
    		add(tmpField);
    
    		tmpField 	= new ThreeWinsField(sizePlayingField, sizePlayingField, (ThreeWinsFrame) parentFrame, 1, 2);
    		tmpField.setLayoutManager();		
    		add(tmpField);
    
    		tmpField 	= new ThreeWinsField(sizePlayingField, sizePlayingField, (ThreeWinsFrame) parentFrame, 2, 2);
    		tmpField.setLayoutManager();		
    		add(tmpField);
    
    	}
    
    	@Override
    	public void paint(Graphics g) {
    		System.out.println(this.getClass().getName() + "Paint");
    		g.setColor(new Color(0, 0, 0));
    		g.drawRect(0, 0, getSize().width - 1, getSize().height -1);
    
    	}
    
    }
    
    package ThreeWins;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    import GeneralGameGui.GuiPanel;
    
    @SuppressWarnings("serial")
    public class ThreeWinsField extends GuiPanel{
    
    	private JPanel fieldPanel	= new JPanel(true); /* use double buffer */
    	private int xPosition		= 0;
    	private int yPosition		= 0;
    
    	public ThreeWinsField(int width, int height, ThreeWinsFrame parentFrame, int x, int y) {
    		super(width, height, parentFrame);		
    		xPosition	= x;
    		yPosition	= y;
    	}	
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	//@Override
    	public void setLayoutManager() {
    		System.out.println(this.getClass().getName() + " SetLayoutManager");	
    	}
    
    	public void paint(Graphics g) {	
    		System.out.println(this.getClass().getName() + " Paint");
    		int widthBorder	= 5;
    		g.setColor(new Color(0, 0, 0));
    		/* draw the border row 0*/
    		if ((xPosition == 0) && (yPosition == 0)){			
    			/* draw bottom border */
    			g.fillRect(0, getSize().height - widthBorder, getSize().width, getSize().height);
    
    			/* draw right border */
    			g.fillRect(getSize().width - widthBorder, 0, widthBorder, getSize().height);			
    		}
    
    		if ((xPosition == 1) && (yPosition == 0)){
    			/* draw left border */
    			g.fillRect(0, 0, widthBorder, getSize().height);
    
    			/* draw bottom border */
    			g.fillRect(0, getSize().height - widthBorder, getSize().width, getSize().height);
    
    			/* draw right border */
    			g.fillRect(getSize().width - widthBorder, 0, widthBorder, getSize().height);			
    		}
    
    		if ((xPosition == 2) && (yPosition == 0)){
    			/* draw left border */
    			g.fillRect(0, 0, widthBorder, getSize().height);
    
    			/* draw bottom border */
    			g.fillRect(0, getSize().height - widthBorder, getSize().width, getSize().height);			
    		}
    
    		/* draw the border row 1*/
    		if ((xPosition == 0) && (yPosition == 1)){			
    			/* draw bottom border */
    			g.fillRect(0, getSize().height - widthBorder, getSize().width, getSize().height);
    
    			/* draw right border */
    			g.fillRect(getSize().width - widthBorder, 0, widthBorder, getSize().height);
    
    			/* draw top border */
    			g.fillRect(0, 0, getSize().width, widthBorder);
    		}
    
    		if ((xPosition == 1) && (yPosition == 1)){
    			/* draw left border */
    			g.fillRect(0, 0, widthBorder, getSize().height);
    
    			/* draw bottom border */
    			g.fillRect(0, getSize().height - widthBorder, getSize().width, getSize().height);
    
    			/* draw right border */
    			g.fillRect(getSize().width - widthBorder, 0, widthBorder, getSize().height);
    
    			/* draw top border */
    			g.fillRect(0, 0, getSize().width, widthBorder);
    		}
    
    		if ((xPosition == 2) && (yPosition == 1)){
    			/* draw left border */
    			g.fillRect(0, 0, widthBorder, getSize().height);
    
    			/* draw bottom border */
    			g.fillRect(0, getSize().height - widthBorder, getSize().width, getSize().height);
    
    			/* draw top border */
    			g.fillRect(0, 0, getSize().width, widthBorder);
    		}
    
    		/* draw the border row 2*/
    		if ((xPosition == 0) && (yPosition == 2)){			
    			/* draw right border */
    			g.fillRect(getSize().width - widthBorder, 0, widthBorder, getSize().height);
    
    			/* draw top border */
    			g.fillRect(0, 0, getSize().width, widthBorder);
    		}
    
    		if ((xPosition == 1) && (yPosition == 2)){
    			/* draw left border */
    			g.fillRect(0, 0, widthBorder, getSize().height);
    
    			/* draw right border */
    			g.fillRect(getSize().width - widthBorder, 0, widthBorder, getSize().height);
    
    			/* draw top border */
    			g.fillRect(0, 0, getSize().width, widthBorder);
    		}
    
    		if ((xPosition == 2) && (yPosition == 2)){
    			/* draw left border */
    			g.fillRect(0, 0, widthBorder, getSize().height);
    
    			/* draw top border */
    			g.fillRect(0, 0, getSize().width, widthBorder);
    		}
    
    	}
    
    }
    

    Hat jemand eine Idee ????



  • Nachdem du alle Komponenten zu deinem Haupt-Frame hinzugefügt hast solltest du noch pack() aufrufen, damit alle Komponenten auch am vorgegebenen Layout ausgerichtet werden. Außerdem solltest du nicht setLayout() überschreiben, um das Layout zu setzen und Komponenten hinzuzufügen, sondern dies direkt im Konstruktor oder in einer eigenen init()-Methode machen. Ansonsten sollte man bei Swing-Komponenten grundsätzlich paintComponent(Graphics) und nicht paint(Graphics) überschreiben.


Log in to reply