Hallo,
ich wollte auf einem Panel Buttons anordnen und das Panel dann zum Frame hinzufügen.
Das Panel hat die Größe 600*600, aber wenn ich es dem Frame(800*800) hinzufüge, verteilen sich die Buttons auf dem gesamten Frame.
Weiß vielleicht wer woran das liegt?
Schon mal danke im Vorraus.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72:
| import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class SudokuGUI extends JFrame { JPanel SudokuPanel; JButton button[][] = new JButton[9][9]; int Sudoku[][] = new int[9][9];
public SudokuGUI() { setSize(800, 800); setLocation(0, 0); setTitle("Sudoku"); add(new SudokuPanel()); setVisible(true); }
class SudokuPanel extends JPanel implements ActionListener { SudokuPanel() { setBackground(Color.lightGray); setLocation(0, 0); setSize(600, 600); setLayout(new GridLayout(9, 9)); for (int i = 0; i < 9; i++) { // Buttons werden hinzugefügt for (int j = 0; j < 9; j++) { button[i][j] = new JButton(); button[i][j].setName("" + i + ":" + "" + j); // Koordinaten vom Button button[i][j].addActionListener(this); button[i][j].setText(""); add(button[i][j]); } } }
@Override public void actionPerformed(ActionEvent e) { JButton klick = (JButton) e.getSource(); String name = klick.getName(); String[] string = name.split(":"); int[] integer = new int[string.length];
for (int i = 0; i < string.length; i++) { String numbertoString = string[i]; integer[i] = Integer.parseInt(numbertoString); }
int a = integer[0]; int b = integer[1]; for (int number : integer) { System.out.println(number); }
int option = JOptionPane.showConfirmDialog(null, "Zahl eingeben");
int eingabe = Integer.parseInt((String) JOptionPane.showInputDialog(SudokuPanel, "Text", null, JOptionPane.INFORMATION_MESSAGE, null, null, "[9]")); if (((JButton) e.getSource()).getText().equals("")) { klick.setText("" + eingabe); Sudoku[a][b] = eingabe; System.out.println(Sudoku[a][b]); }
}
}
public static void main(String[] args) { new SudokuGUI(); } } |