Hier ist der Source Code meiner GUI für ein Schachspiel. Ich habe hier folgende Probleme: 1. Die Buttons, die die Figuren darstellen, sollen eine bestimmte Größe haben, die in Zeile 49 festgelegt wird. Das funktioniert aber scheinbar nicht (auch nicht mit setMaximumSize() ). Das 2. Problem ist, dass das Spielfeld (8 Buttons x 8 Buttons) soll 680x680 groß sein (Zeile 40), damit das Feld mit 8x8 dargestellt wird. Das will aber irgendwie nicht funktionieren, die Darstellung ändert sich immer mit der Größe des Fensters (Zeile 35). Beachtet, das rechts und links vom Spielfeld noch der Friedhof für die Figuren Platz haben muss.
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Image.*;
import java.awt.Dimension.*;
public class Oberflaeche extends JFrame {
Steuerung steu;
JFrame fenster;
JPanel mitte,boxunten,friedr,friedl;
JLabel friedw,frieds,pl1,info;
JButton spielFeld[][] = new JButton[8][8];
class Listener implements ActionListener{
public void actionPerformed(ActionEvent e){
}
}
public Oberflaeche(Steuerung st) {
this.steu = st;
this.friedw = new JLabel("Friedhof Weiß");
this.frieds = new JLabel("Friedhof Schwarz");
this.info = new JLabel("Weiß beginnt");
this.mitte = new JPanel();
this.pl1 = new JLabel();
this.friedr = new JPanel();
this.friedl = new JPanel();
this.boxunten = new JPanel();
this.boxunten.add(info);
this.fenster = new JFrame("Schachspiel");
this.fenster.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
this.fenster.setSize(1400,800);
this.fenster.setLayout(new BorderLayout());
this.mitte.setLayout(new GridLayout(8,8));
this.friedr.add(frieds);
this.friedl.add(friedw);
this.mitte.setPreferredSize(new Dimension(680,680));
this.fenster.add(mitte,BorderLayout.CENTER);
this.fenster.add(pl1,BorderLayout.NORTH);
this.fenster.add(friedl,BorderLayout.WEST);
this.fenster.add(friedr,BorderLayout.EAST);
this.fenster.add(boxunten,BorderLayout.SOUTH);
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
this.spielFeld[i][j] = new JButton();
this.spielFeld[i][j].setPreferredSize(new Dimension(85,85));
this.mitte.add(spielFeld[i][j]);
this.spielFeld[i][j].addActionListener(new Listener());
}
}
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
this.spielFeld[i][j].setBackground(new Color(244, 224, 179));
j = j+1;
this.spielFeld[i][j].setBackground(new Color(179, 133, 91));
}
i = i+1;
}
for(int i=0;i<8;i++){
i = i+1;
for(int j=0;j<7;j++){
j = j+1;
this.spielFeld[i][j].setBackground(new Color(244, 224, 179));
j = j-1;
this.spielFeld[i][j].setBackground(new Color(179, 133, 91));
j = j+1;
}
}
Image image = new ImageIcon(getClass().getResource("pawnwH.gif")).getImage();
Image image2 = new ImageIcon(getClass().getResource("pawnwD.gif")).getImage();
Image image3 = new ImageIcon(getClass().getResource("bishopwH.gif")).getImage();
Image image4 = new ImageIcon(getClass().getResource("bishopwD.gif")).getImage();
Image image5 = new ImageIcon(getClass().getResource("kingbH.gif")).getImage();
Image image6 = new ImageIcon(getClass().getResource("kingbD.gif")).getImage();
Image image7 = new ImageIcon(getClass().getResource("kingwH.gif")).getImage();
Image image8 = new ImageIcon(getClass().getResource("kingwD.gif")).getImage();
Image image9 = new ImageIcon(getClass().getResource("bishopbD.gif")).getImage();
Image image10 = new ImageIcon(getClass().getResource("bishopbH.gif")).getImage();
Image image11 = new ImageIcon(getClass().getResource("queenbD.gif")).getImage();
Image image12 = new ImageIcon(getClass().getResource("queenbH.gif")).getImage();
Image image13 = new ImageIcon(getClass().getResource("queenwD.gif")).getImage();
Image image14 = new ImageIcon(getClass().getResource("queenwH.gif")).getImage();
Image image15 = new ImageIcon(getClass().getResource("rookwD.gif")).getImage();
Image image16 = new ImageIcon(getClass().getResource("rookwH.gif")).getImage();
Image image17 = new ImageIcon(getClass().getResource("rookbH.gif")).getImage();
Image image18 = new ImageIcon(getClass().getResource("rookbD.gif")).getImage();
Image image19 = new ImageIcon(getClass().getResource("knightbH.gif")).getImage();
Image image20 = new ImageIcon(getClass().getResource("knightbD.gif")).getImage();
Image image21 = new ImageIcon(getClass().getResource("knightwH.gif")).getImage();
Image image22 = new ImageIcon(getClass().getResource("knightwD.gif")).getImage();
this.fenster.setResizable(false);
this.fenster.setVisible(true);
}
}