Problem mit erben von JButton

el.noxx

Mitglied
Ich komm nicht dahinter, vllt aber auch weil es 5 Uhr morgens ist. Ich erzeuge ein JFrame auf dem ein JPanel ist. Auf das JPanel kommen kommen 42 Button (7x6) mit Hilfe eines GridLayouts. Funktioniert auch alles so wie ich es mir vorstelle. Aber sobald ich die JButtons durch eine selbst erzeugte Klasse die von JButton erbt ersetze, liegen auf einmal alle Buttons nur noch übereinander nicht mehr so wie vorher (7 in der Reihe x 6 in den Spalten)...... ich weiß einfach nicht woran es liegt. ich veränder eig gar nichts am code nur das ich eben JButton durch meine Klasse ersetze.. kennt jemand vllt das Problem?
 
Java:
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;



public class ApplicationWindow {
	private JFrame frame=null;
	private JPanel panel=null;
	private Square[][] square;
//	private JButton setButton =null;
	private int FIELDROW = 7;
	private int FIELDCOLUMN =6;
	private boolean forPicture = true;
	private final String projPath = System.getProperty("user.dir");
	private final String imagesPath = "/src/tud/project/gameWindowPictures/";
	private final String pathGreen = projPath + imagesPath + "green.jpg";
	private final String pathLGreen = projPath + imagesPath + "lgreen.jpg";
//	private final String arrowTop = projPath + imagesPath + "oben.png";
//	private final String arrowRight = projPath + imagesPath + "rechts.png";
//	private final String arrowDown = projPath + imagesPath + "unten.png";
//	private final String arrowLeft = projPath + imagesPath + "links.png";

	private ImageIcon iILGreen=new ImageIcon(pathLGreen);
	private ImageIcon iIGreen=new ImageIcon(pathGreen);
//	private ImageIcon top=new ImageIcon(arrowTop);

	public void initializePanel(){
		panel = new JPanel();
		panel.setVisible(true);
		panel.setLayout(new GridLayout(FIELDCOLUMN, FIELDROW,0,0));
		panel.setBorder(BorderFactory.createEmptyBorder());
		for (int i=0;i < FIELDCOLUMN;i++)
			for (int k=0;k < FIELDROW;k++)
				panel.add(square[i][k]);
	}
	
	public void initializeSquare(){
		square = new Square[FIELDCOLUMN][FIELDROW];
		
		for (int i=0;i< FIELDCOLUMN;i++){
			for (int k=0;k<FIELDROW;k++){
				
				if (forPicture){
//					setButton = new JButton();
					square[i][k] = new Square(i,k,iILGreen);
					forPicture = !forPicture;
				}
				else{
//					setButton = new JButton();
					square[i][k] = new Square(i,k,iIGreen);
					forPicture = !forPicture;
				}
//				square[i][k] = setButton;
			}
		}
	}	
	public void initializeFrame(){
		frame = new JFrame("GameWindow");
		frame.setBounds(100, 100, 500, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.add(panel);
	}
	public void showGUI() {
		frame.setVisible(true);		
	}
	
	public ApplicationWindow (){
		initializeSquare();
		initializePanel();
		initializeFrame();
	}
	public static void main (String[] args){
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ApplicationWindow window1 = new ApplicationWindow();
				window1.showGUI();
			}
		});		
	}
}
 
Zuletzt bearbeitet:
Hier noch die Klasse Square:

Java:
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class Square extends JButton{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int row;
	private int column;
	
	Square(int x, int y, ImageIcon icon){
		super();
		setIcon(icon);
		setDisabledIcon(icon);
		this.row = x;
		this.column = y;

		setBorder(BorderFactory.createEmptyBorder());
	}
	
	public int getX(){
		return row;
	}
	public int getY(){
		return column;
	}
	@Override
	public void setIcon(Icon icon) {
		super.setIcon(icon);
		setDisabledIcon(icon);
	}
	
	
}
 
Hier nochmal etwas kürzer zusammen gefasst:
Java:
package window;

import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GUI extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JFrame frame;
	private JPanel panel;
	private Square[] square;
	private int LENGTH =7;
	private final String projPath = System.getProperty("user.dir");
	private final String imagesPath = "/src/tud/project/gameWindowPictures/";
	private final String pathLGreen = projPath + imagesPath + "lgreen.jpg";
	private ImageIcon iILGreen=new ImageIcon(pathLGreen);

	
	private void initializeSquare(){
		square = new Square[LENGTH];
		for (int i=0; i<LENGTH; i++){
			square[i] = new Square(i, i, iILGreen);
		}
	}
	private void initializePanel(){
		panel = new JPanel();
		panel.setLayout(new GridLayout(5, 0, 0, 0));
		for (int i=0; i<LENGTH; i++){
			panel.add(square[i]);	
		}
	}
	private void initializeFrame(){
		frame = new JFrame("Window");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(panel);
	}
	public void showGUI(){
		frame.setVisible(true);
	}
	public GUI(){
		initializeSquare();
		initializePanel();
		initializeFrame();
	}
	public static void main (String[] args){
		GUI gui = new GUI();
		gui.showGUI();
	}
}
 
Ich hatte damals das gleiche Problem...

schau mal links neben deiner getX()-Methode. (Ich hoffe deine IDE zeigt dir da ein Overrides an...)

Ja, du überschreibst hier die Methode JComponent (Java Platform SE 7 ) , welche intern für die Positionierung genutzt wird.
 
Zuletzt bearbeitet:
Sorry, dass keine Antwort mehr kam. Ich hatte den Quellcode gelesen und keinen Fehler entdeckt.

Wollte Dänin eclipse Kopien musste aber weg ;(
 

Zurück
Oben