Swing Buttons werden nicht angezeigt

RambaZamba

Neues Mitglied
Hallo Community,
vorerst: ich bin blutiger Anfänger.

Ich habe ein kleines (sicherlich maximal unsauber geschriebenes) 4-gewinnt Spiel programmiert.
Läuft auch soweit. Nun wollte ich statt den AWT Frames auf JFrames umsteigen.
Leider sind jetzt die Buttons bei Programm-Start nur teilweise sichtbar (nur die ersten zwei Reihen).
Die fehlenden und nicht deaktivierten Buttons werden bei Mouse-over sichtbar. Der Rest erst nachdem ein Spieler gewonnen hat und somit alle Spielfeld Buttons wieder weiß gefärbt werden.
Woran liegt?
(ja ich weiß, layouts sollte ich mir mal anschauen - es hat ohne aber auch gut geklappt mit normalen frames)

Meine Google-Recherche ergab, das es meistens an einer selbst gebauten paint Methode lag oder das pack() nicht ausgeführt wurde. Wenn ich letzteres mache hat das Fenster allerdings eine Größe von 0 - liegt wohl daran das ich kein Layout verwende (?).

Hier der Code:
Java:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class VierGewinnt {

	static int[][] feld = new int [10][10];
	static JFrame window = new JFrame("4 Gewinnt - by SEIBT");
	static JButton [][] lfeld = new JButton [10][10];
	static int player = (int) (Math.random()*2+1);
	static Color[] playercolor = new Color[5];
	static int[] scores = new int [3];
	static JLabel[] score = new JLabel [3];
	static JLabel label = new JLabel("Spieler: "+String.valueOf(player)+" ist an der Reihe!");
	static JButton [] crow = new JButton [10];
	static int[] xcorrect = new int [5];
	static int[] ycorrect = new int [5];
	
	public static void win (int P){
		scores[P]+=1;
		score[P].setText("Spieler"+String.valueOf(P)+":  "+scores[P]);
		for (int i=0; i<4;i++)
			lfeld[xcorrect[i]][ycorrect[i]].setBackground(Color.ORANGE);
		JOptionPane.showMessageDialog(label, "Spieler "+String.valueOf(P)+" hat Spiel "+String.valueOf(scores[1]+scores[2])+" gewonnen!", "Sieger:",JOptionPane.OK_CANCEL_OPTION);
		for (int i=0; i<7; i++){
			for (int i2=0;i2<6;i2++){
				feld[i][i2] = 0;
				lfeld[i][i2].setBackground(Color.WHITE);
			}
		}
	}
	
	public static void check (int x, int y, int P){
		int correct = 0;														// OBEN UNTEN
		for (int y2=0; y2 < 6; y2++){
			if (feld[x][y2]==P){
				xcorrect[correct]=x;
				ycorrect[correct]=y2;
				correct += 1;
				if (correct  == 4)
					win(P);
			}
			else {
				correct = 0;	
				}
		}

		correct = 0;															// LINKS RECHTS
		for (int x2=0; x2 < 7; x2++){
			if (feld[x2][y]==P){
				xcorrect[correct]=x2;
				ycorrect[correct]=y;
				correct += 1;
				if (correct  == 4)
					win(P);
			}
			else {
				correct = 0;	
				}
		}	
		
		correct = 0;
		int schritte=0,startx=0,starty=0;										// DIAGONAL UNTEN LINKS OBEN RECHTS
		if (x > y) {
			startx = x-y;
			starty = y-y;
			schritte = (x+(6-x))-(x-y)+1;
		}
		if (x == y){
			startx = x-y;
			starty = y-y;
			schritte = 6;
		}
		if (x < y){
			startx = x-x;
			starty = y-x;
			schritte =((x+(5-y))-(x-x))+1;
		}
		
		if (schritte > 3) {
			for (int i = 0; i < schritte; i++) {
				if (feld[startx][starty] == P) {
					xcorrect[correct]=startx;
					ycorrect[correct]=starty;
					correct += 1;
					if (correct == 4)
						win(P);
				} else {
					correct = 0;
				}
				startx += 1;
				starty += 1;
			}
		}
		
		correct = 0;															// DIAGONAL OBEN LINKS UNTEN RECHTS
		startx = x+y;
		if (startx > 6){
			starty = startx - 6;
			startx = startx - starty;
		}
		else
			starty = 0;
		
		if (startx+starty < 6){
			schritte = startx+starty+1;
		}
			
		if (startx+starty == 6)
			schritte = 6;
		if (startx+starty > 6)
			schritte = 6-((startx+starty)-6);
		
		if (schritte > 3){
			
			for (int i=0; i < schritte; i++){		
				if (feld[startx][starty]==P){
					xcorrect[correct]=startx;
					ycorrect[correct]=starty;
					correct += 1;
				
					if (correct  == 4)
						win(P);
				}
				else {
					correct = 0;	
					}
				startx -=1;
				starty +=1;
			}
		}
	}
	
	public static void place (int x){
		boolean b = false;
		int end = 6;
		for (int y=0; y < end; y++){
			if (feld[x][y] == 0){
				lfeld[x][y].setBackground(playercolor[player]);
				feld[x][y] = player;
				b = true;
				check(x,y,player);
				y = end;
				if (player == 1)
					player = 2;
				else
					player = 1;
				label.setText("Spieler: "+String.valueOf(player)+" ist an der Reihe!");
				label.setBackground(playercolor[player]);
			}	
		}
		if (b == false)
			JOptionPane.showMessageDialog(window, "Feld ist voll!", "Error",JOptionPane.OK_CANCEL_OPTION);
	}
	
	public static void draw(){
		int abstand = 55;
		for (int x=0; x<7; x++){
			crow[x]=new JButton();
			crow[x].setBounds(abstand*x+110,95,25,25);
			crow[x].setBackground(Color.YELLOW);
			window.add(crow[x]);
			crow[x].setActionCommand(String.valueOf(x));
			crow[x].addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String s = e.getActionCommand();
					place(Integer.parseInt(s));
				}
			});
			for (int y=0;y<6;y++){
				feld[x][y] = 0; // Felder unbesetzt
				 lfeld[x][y] = new JButton("x"+String.valueOf(x)+" y"+String.valueOf(y));
				 lfeld[x][y].setBounds(abstand*x+100,405-(abstand*y),50,50);
				 lfeld[x][y].setEnabled(false);
				 window.add(lfeld[x][y] );
			}
		}
		//window.pack();
		//label.setBackground(playercolor[player]);
		//JOptionPane.showMessageDialog(window, "SPIELER "+String.valueOf(player)+" beginnt!");	
	}
	
	public static void main(String[] args) {

		window.setLayout(null);
		window.setResizable(false);
		window.setVisible(true);
		window.setBounds(200, 200, 580, 500);
		window.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				window.dispose();
				System.exit(0);
			}});
		window.setBackground(Color.DARK_GRAY);
		label.setBounds(100,50,160,25);
		window.add(label);
		
		playercolor[1] = Color.cyan;
		playercolor[2] = Color.RED;
		
		score[1] = new JLabel("Spieler "+String.valueOf(1)+":  "+scores[1]);
		score[1].setBackground(playercolor[1]);
		score[2] = new JLabel("Spieler "+String.valueOf(2)+":  "+scores[2]);
		score[2].setBackground(playercolor[2]);
		
		window.add(score[1]);
		window.add(score[2]);
		score[1].setBounds(290,50,85,25);
		score[2].setBounds(395,50,85,25);
		
		draw();
	}

}

Auch sonstige Vorschläge zur Code-Optimierung sind interessant für mich 🙂
 
Zuletzt bearbeitet:
Hallo RambaZamba,

setze einfach zum Schluss, nachdem du die Methode draw(); aufgerufen hast, noch ein window.repaint(); Dann sollte es funktionieren.

Gruß
TB94
 
Ich habe ein kleines (sicherlich maximal unsauber geschriebenes) 4-gewinnt Spiel programmiert.

Java:
	static int[][] feld = new int [10][10];
	static JFrame window = new JFrame("4 Gewinnt - by SEIBT");
	static JButton [][] lfeld = new JButton [10][10];
	static int player = (int) (Math.random()*2+1);
	static Color[] playercolor = new Color[5];
	static int[] scores = new int [3];
	static JLabel[] score = new JLabel [3];
	static JLabel label = new JLabel("Spieler: "+String.valueOf(player)+" ist an der Reihe!");
	static JButton [] crow = new JButton [10];
	static int[] xcorrect = new int [5];
	static int[] ycorrect = new int [5];
	
	
	public static void main(String[] args) {

		window.setLayout(null);
		window.setResizable(false);
		window.setVisible(true);
		window.setBounds(200, 200, 580, 500);
		window.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				window.dispose();
				System.exit(0);
			}});
		window.setBackground(Color.DARK_GRAY);
		label.setBounds(100,50,160,25);
		window.add(label);
		
		playercolor[1] = Color.cyan;
		playercolor[2] = Color.RED;
		
		score[1] = new JLabel("Spieler "+String.valueOf(1)+":  "+scores[1]);
		score[1].setBackground(playercolor[1]);
		score[2] = new JLabel("Spieler "+String.valueOf(2)+":  "+scores[2]);
		score[2].setBackground(playercolor[2]);
		
		window.add(score[1]);
		window.add(score[2]);
		score[1].setBounds(290,50,85,25);
		score[2].setBounds(395,50,85,25);
		
		draw();
	}

}

1. "static": Java ist eine OOP, daher solltest du schauen das du diese Konzept auch anwendest!
2. "window.setVisible(true);" sorgt dafür das dein Fenster angezeigt wird. Wenn du nach dieser Zeile neue Elemente hinzufügst werden diese nicht angezeigt. Verschiebe diese Zeile als ganz ans ende.
 
so wie du deine Label ins Window einfügst, musst du auch deine Buttons einfügen.
Code:
window.add(button);
 
so wie du deine Label ins Window einfügst, musst du auch deine Buttons einfügen.
Code:
window.add(button);
Das geschieht in der draw function.

Danke an TB94 und Joose, beide Lösungen funktionieren. Ich verwende window.setVisible(true) nun nach dem erstellen des letzten Buttons. repaint() werde ich zukünftig verwenden wenn ich bei Laufzeit noch Elemente hinzufüge, richtig?
 
Zuletzt bearbeitet:

Neue Themen


Zurück
Oben