J
J-Mee
Gast
hi!
warum ist bei diesem frame ein rahmen? :x
kann mir jemand helfen?
schonmal danke im vorraus!
:arrow:
warum ist bei diesem frame ein rahmen? :x
kann mir jemand helfen?
schonmal danke im vorraus!
:arrow:
Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ButtonArray2 extends JFrame implements ActionListener {
static final long serialVersionUID = 0L;
private int rows = 7, cols = 11;
private JButton[][] buttons = new JButton[rows][cols];
private int[][] counts = new int[rows][cols];
public ButtonArray2() {
setTitle("Button array");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setFrameLocation(596, 398);
Container windowFlaeche = getContentPane();
windowFlaeche.setLayout(new GridLayout(rows, cols));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
buttons[i][j] = new JButton();
buttons[i][j].setActionCommand("" + i + " " + j);
buttons[i][j].addActionListener(this);
buttons[i][j].setText(counts[i][j]+"");
windowFlaeche.add(buttons[i][j]);
}
}
setVisible(true);
}
public void setFrameLocation(int frameWidth, int frameHeight) {
setSize(frameWidth, frameHeight);
Dimension screen = getToolkit().getScreenSize();
setLocation((screen.width - frameWidth)/2, (screen.height - frameHeight)/2);
}
public void actionPerformed(ActionEvent e) {
String coordinates = e.getActionCommand();
setTitle(coordinates);
String[] indexes = coordinates.split(" ");
int r = Integer.parseInt(indexes[0]);
int c = Integer.parseInt(indexes[1]);
buttons[r][c].setText(++counts[r][c]+"");
// print counts
for (int[] row : counts) {
for (int i : row)
System.out.print(i + " ");
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
new ButtonArray2();
}
}