Styling and slight refactor
This commit is contained in:
		
							parent
							
								
									6ae83248ef
								
							
						
					
					
						commit
						970aabd532
					
				
					 1 changed files with 23 additions and 14 deletions
				
			
		|  | @ -1,6 +1,8 @@ | |||
| package gui; | ||||
| 
 | ||||
| import javax.swing.*; | ||||
| import javax.swing.border.Border; | ||||
| 
 | ||||
| import java.awt.*; | ||||
| import java.awt.event.*; | ||||
| 
 | ||||
|  | @ -31,35 +33,42 @@ public class SudokuView extends JFrame { | |||
|         initializeGrid(); | ||||
|         initializeButtons(); | ||||
| 
 | ||||
|         pack(); | ||||
|         setLocationRelativeTo(null); | ||||
|         setMinimumSize(new Dimension(500, 500)); | ||||
|         pack(); // Resize the window to fit the components, if necessary | ||||
|         setLocationRelativeTo(null); // Center the window | ||||
|     } | ||||
| 
 | ||||
|     /** Initialize the grid, called by the constructor */ | ||||
|     private void initializeGrid() { | ||||
|         grid = new JTextField[9][9]; | ||||
|         JPanel gridPanel = new JPanel(new GridLayout(9, 9)); | ||||
|      | ||||
|         gridPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); | ||||
| 
 | ||||
|         int fontSize = 16; | ||||
|         String fontName = gridPanel.getFont().getName(); | ||||
|         Border border = BorderFactory.createLineBorder(Color.BLACK); | ||||
|         Font font = new Font(fontName, Font.BOLD, fontSize); | ||||
| 
 | ||||
|         for (int row = 0; row < 9; row++) { | ||||
|             for (int col = 0; col < 9; col++) { | ||||
|                 grid[row][col] = new JTextField(2); | ||||
|                 grid[row][col].setHorizontalAlignment(JTextField.CENTER); | ||||
|      | ||||
|                 JTextField cell = new JTextField(2); | ||||
|                 cell.setHorizontalAlignment(JTextField.CENTER); | ||||
|                 cell.setFont(font); | ||||
|                 cell.setBorder(border); | ||||
|                 cell.setForeground(Color.BLACK); | ||||
| 
 | ||||
|                 // Set background color to gray for every third JTextField | ||||
|                 if ((row / 3 + col / 3) % 2 == 1) { | ||||
|                     grid[row][col].setBackground(Color.LIGHT_GRAY); | ||||
|                     cell.setBackground(Color.LIGHT_GRAY); | ||||
|                 } | ||||
|                  | ||||
|                 Font boldFont = new Font(grid[row][col].getFont().getName(), Font.BOLD, grid[row][col].getFont().getSize()); | ||||
|                 grid[row][col].setFont(boldFont); | ||||
|                 grid[row][col].setForeground(Color.BLACK); | ||||
|                 gridPanel.add(grid[row][col]); | ||||
| 
 | ||||
|                 grid[row][col] = cell; | ||||
|                 gridPanel.add(cell); | ||||
|             } | ||||
|         } | ||||
|      | ||||
| 
 | ||||
|         add(gridPanel, BorderLayout.CENTER); | ||||
|     } | ||||
|      | ||||
| 
 | ||||
|     /** Initialize the buttons, called by the constructor */ | ||||
|     private void initializeButtons() { | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Imbus
						Imbus