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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
import javax.swing.BorderFactory;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.UIManager;
import javax.swing.text.MaskFormatter;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.text.ParseException;
/**
* Created by IntelliJ IDEA.
* User: bebe
* Date: 29-juin-2006
* Time:
* To change this template use File | Settings | File Templates.
*/
public class SudokuGrid {
public static void main(String[] args) {
final Font f = new Font("Verdana", Font.PLAIN, 30);
UIManager.put("FormattedTextField.font", f);
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame myFrame = new JFrame("My sudoku grid using GridBagLayout");
// myFrame.getContentPane().setBackground(Color.DARK_GRAY);
myFrame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(1, 1, 1, 1);
gbc.gridy = 0;
gbc.gridx = 0;
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
JFormattedTextField formattedTextField = null;
try {
MaskFormatter formatter = new MaskFormatter("#");
formatter.setPlaceholderCharacter('_');
formattedTextField = new JFormattedTextField(formatter);
formattedTextField.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
formattedTextField.setMinimumSize(new Dimension(40, 40));
formattedTextField.setPreferredSize(new Dimension(40, 40));
formattedTextField.setHorizontalAlignment(JFormattedTextField.CENTER);
} catch (ParseException e) {
e.printStackTrace();
}
myFrame.add(formattedTextField, gbc);
gbc.gridx ++;
}
gbc.gridx = 0;
gbc.gridy++;
}
myFrame.pack();
myFrame.setLocation(50, 50);
myFrame.setResizable(false);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
});
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame myFrame = new JFrame("My sudoku grid using SprintLayout");
myFrame.setLayout(new SpringLayout());
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
JFormattedTextField formattedTextField = null;
try {
formattedTextField = new JFormattedTextField(new MaskFormatter("#"));
formattedTextField.setMinimumSize(new Dimension(40, 40));
formattedTextField.setPreferredSize(new Dimension(40, 40));
formattedTextField.setHorizontalAlignment(JFormattedTextField.CENTER);
formattedTextField.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
} catch (ParseException e) {
e.printStackTrace();
}
myFrame.add(formattedTextField);
}
}
makeGrid(myFrame.getContentPane(), 9, 9, 1, 1, 1, 1);
myFrame.pack();
myFrame.setLocation(460, 50);
myFrame.setResizable(false);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
});
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame myFrame = new JFrame("My sudoku grid using GridLayout");
myFrame.setLayout(new GridLayout(9, 0, 1, 1));
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
JFormattedTextField formattedTextField = null;
try {
formattedTextField = new JFormattedTextField(new MaskFormatter("#"));
formattedTextField.setMinimumSize(new Dimension(40, 40));
formattedTextField.setPreferredSize(new Dimension(40, 40));
formattedTextField.setHorizontalAlignment(JFormattedTextField.CENTER);
formattedTextField.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
} catch (ParseException e) {
e.printStackTrace();
}
myFrame.add(formattedTextField);
}
}
myFrame.pack();
myFrame.setLocation(50, 480);
myFrame.setResizable(false);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
});
}
/*
* A 1.4 file that provides utility methods for creating form- or grid-style layouts with SpringLayout.
* These utilities are used by several programs, such as SpringBox and SpringCompactGrid.
* See: http://java.sun.com/docs/books/tutorial/uiswing/layout/example-1dot4/SpringUtilities.java
*/
/**
* Aligns the first <code>rows</code> * <code>cols</code>
* components of <code>parent</code> in
* a grid. Each component is as big as the maximum
* preferred width and height of the components.
* The parent is made just big enough to fit them all.
*
* @param rows number of rows
* @param cols number of columns
* @param initialX x location to start the grid at
* @param initialY y location to start the grid at
* @param xPad x padding between cells
* @param yPad y padding between cells
*/
public static void makeGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad) {
SpringLayout layout;
try {
layout = (SpringLayout) parent.getLayout();
} catch (ClassCastException exc) {
System.err.println("The first argument to makeGrid must use SpringLayout.");
return;
}
Spring xPadSpring = Spring.constant(xPad);
Spring yPadSpring = Spring.constant(yPad);
Spring initialXSpring = Spring.constant(initialX);
Spring initialYSpring = Spring.constant(initialY);
int max = rows * cols;
//Calculate Springs that are the max of the width/height so that all
//cells have the same size.
Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
for (int i = 1; i < max; i++) {
SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
}
//Apply the new width/height Spring. This forces all the
//components to have the same size.
for (int i = 0; i < max; i++) {
SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
cons.setWidth(maxWidthSpring);
cons.setHeight(maxHeightSpring);
}
//Then adjust the x/y constraints of all the cells so that they
//are aligned in a grid.
SpringLayout.Constraints lastCons = null;
SpringLayout.Constraints lastRowCons = null;
for (int i = 0; i < max; i++) {
SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
if (i % cols == 0) { //start of new row
lastRowCons = lastCons;
cons.setX(initialXSpring);
} else { //x position depends on previous component
cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), xPadSpring));
}
if (i / cols == 0) { //first row
cons.setY(initialYSpring);
} else { //y position depends on previous row
cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), yPadSpring));
}
lastCons = cons;
}
//Set the parent's size.
SpringLayout.Constraints pCons = layout.getConstraints(parent);
pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring.constant(yPad), lastCons.getConstraint(SpringLayout.SOUTH)));
pCons.setConstraint(SpringLayout.EAST, Spring.sum(Spring.constant(xPad), lastCons.getConstraint(SpringLayout.EAST)));
}
} |
Partager