PDA

View Full Version : راهنمایی برای رسم یک جدول



Sina.iRoid
سه شنبه 30 دی 1393, 09:11 صبح
سلام دوستان. خروجی کد های زیر رو نگاه کنید:


import java.awt.Color;
import java.awt.Graphics;


import javax.swing.JFrame;


@SuppressWarnings("serial")
public class Grid extends JFrame {


@Override
public void paint(Graphics g) {
super.paint(g);

g.setColor(Color.BLACK);
for (int x = 50; x <= 400; x+= 30) {
for (int y = 50; y <= 400; y += 30) {
g.draw3DRect(x, y, 30, 30, true);
}
}
}
}


خروجی:

127742

من می خوام در این خانه های جدول مقادیری و بنویسم و ازشون استفاده کنم. چطور باید اینکار و انجام بدم!؟
ممنون

ahmad.mo74
سه شنبه 30 دی 1393, 12:23 عصر
سلام

از label یا textField استفاده میکردی بهتر بود. اونطوری راحت میتونی توش چیزی هم بنویسی.
تو گوگل مثال های sudoku رو سرچ کن ببین چجوری ui زدن.

مثلا اینطوری :


import javax.swing.*;
import java.awt.*;


/**
* @author avb
*/
public class Grid extends JFrame {


public Grid() {
int c = 0;
JTextField[][] fields = new JTextField[9][9];
for (int x = 0; x < fields.length; x++) {
for (int y = 0; y < fields.length; y++) {
fields[x][y] = new JTextField(String.valueOf(++c), 1);
fields[x][y].setHorizontalAlignment(SwingConstants.CENTER);
}
c = 0;
}
JPanel[][] panels = new JPanel[3][3];
for (int x = 0; x < panels.length; x++) {
for (int y = 0; y < panels.length; y++) {
panels[x][y] = new JPanel(new GridLayout(3, 3));
}
}
setLayout(new GridLayout(3, 3, 3, 3));
for (int px = 0; px < panels.length; px++) {
for (int py = 0; py < panels.length; py++) {
for (int fx = 0; fx < fields.length; fx++) {
for (int fy = 0; fy < fields.length; fy++) {
panels[px][py].add(fields[px * panels.length + py][fy]);
}
}
add(panels[px][py]);
}
}
}


public static void main(String[] args) {
Grid grid = new Grid();
grid.setSize(new Dimension(600, 600));
grid.setDefaultCloseOperation(WindowConstants.EXIT _ON_CLOSE);
grid.setLocationRelativeTo(null);
grid.setVisible(true);
}


}