Results 1 to 3 of 3
- 02-07-2010, 07:57 PM #1
How to add lines to a JFrame full of JTextFields
I'm trying to create an interface for the script i wrote to solve sudoku(i know i should use an allready made applet) but i need to do something to keep myself busy so i won't play WoW like a mad man so here is what i have made
the Choco part can be neglected, actualy is fully functional but i don;t know how to add the 4 lines but to be placed only in the center panelJava Code:import static choco.Choco.*; import choco.cp.model.CPModel; import choco.cp.solver.CPSolver; import choco.kernel.model.Model; import choco.kernel.model.constraints.Constraint; import choco.kernel.model.variables.integer.IntegerVariable; import choco.kernel.solver.Solver; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class interfata extends JFrame { public JTextField c[][] = new JTextField[9][]; JButton solve = new JButton("solve"); JButton clear = new JButton("clear"); JButton exit = new JButton("exit"); public int mat[][] = new int[9][9]; public interfata() { this.setTitle("Sudoku"); clear.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { clearAP(evt);} }); exit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { System.exit(0); } }); solve.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ solveAP(evt); } } ); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); solve.setSize(50,50); clear.setSize(50,50); exit.setSize(50,50); JPanel panou_centru = new JPanel(); JPanel panou_sud = new JPanel(); Container panou = this.getContentPane(); panou.setLayout(new BorderLayout(0,10)); panou_centru.setLayout(new GridLayout(9,9)); for(int i=0;i<9;i++) for(int j=0;j<9;j++) c[i]= new JTextField[9]; for(int i=0;i<9;i++) for(int j=0;j<9;j++){ c[i][j]= new JTextField("",3); panou_centru.add(c[i][j]); } add("Center", panou_centru); panou_sud.setLayout(new FlowLayout()); panou_sud.add(solve); panou_sud.add(clear); panou_sud.add(exit); add("South",panou_sud); }//constructor private void solveAP(ActionEvent evt) { for(int i=0;i<9;i++) for(int j=0;j<9;j++) { try { mat[i][j]=Integer.parseInt(c[i][j].getText()); } catch(Exception e) { mat[i][j] = 0; } } int n=9; Model m = new CPModel(); IntegerVariable[][] rows = makeIntVarArray("rows", n, n, 1, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = j; k < n; k++) { if (k != j) { m.addConstraint(neq(rows[i][j], rows[i][k])); } } } } for (int j = 0; j < n; j++) { for (int i = 0; i < n; i++) { for (int k = 0; k < n; k++) { if (k != i) { m.addConstraint(neq(rows[i][j], rows[k][j])); } } } } for (int ci = 0; ci < n; ci += 3) { for (int cj = 0; cj < n; cj += 3) { for (int i = ci; i < ci + 3; i++) { for (int j = cj; j < cj + 3; j++) { for (int k = ci; k < ci + 3; k++) { for (int l = cj; l < cj + 3; l++) { if (k != i || l != j) m.addConstraint(neq(rows[i][j], rows[k][l])); } } } } } } for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (mat[i][j] != 0) { Constraint cc = eq(rows[i][j], mat[i][j]); m.addConstraint(cc); } } } Solver s = new CPSolver(); s.read(m); s.solve(); for(int i=0;i<9;i++) for(int j=0;j<9;j++) c[i][j].setText(String.valueOf(s.getVar(rows[i][j]).getVal())); }//solveAP private void clearAP(ActionEvent evt) { for(int i=0;i<9;i++) for(int j=0;j<9;j++) c[i][j].setText(""); }//clearAP }//class
mod edit: code tags addedLast edited by Fubarable; 02-07-2010 at 07:59 PM. Reason: code tags added
silence i'm trying to meditate:p
-
Hello, and welcome to the forum. Thank you for contributing to a user's question in your previous first post.
One way to fix this is to make your central JPanel use a GridLayout(3, 3, 5, 5), a 3 x 3 grid with gaps of 5 points between items. Then paint the background of the center panel black. Where the black shows through will be your lines. You then create a 3 x 3 two dimensional array of inner JPanels that will hold the JTextFields and add these inner panels to your central panel. For instance:
Some other suggestions: Please read my signature below about using code tags since using these will help make your posts easier to read and thus understand. Also, you may wish to remove code in your posts that are unrelated to the problem and that may cause problems with our compiling and running your post. It also makes your posted code shorter and easier to read.Java Code:import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Interfata extends JFrame { private static final Dimension CELL_SIZE = new Dimension(40, 40); public JTextField c[][] = new JTextField[9][]; JButton solve = new JButton("solve"); JButton clear = new JButton("clear"); JButton exit = new JButton("exit"); public int mat[][] = new int[9][9]; public Interfata() { this.setTitle("Sudoku"); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { System.exit(0); } }); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); solve.setSize(50, 50); clear.setSize(50, 50); exit.setSize(50, 50); JPanel panou_centru = new JPanel(); JPanel panou_sud = new JPanel(); Container panou = this.getContentPane(); panou.setLayout(new BorderLayout(0, 10)); int gap = 5; panou_centru.setLayout(new GridLayout(3, 3, gap, gap)); panou_centru.setBorder(new EmptyBorder(gap, gap, gap, gap)); panou_centru.setBackground(Color.black); JPanel[][] innerPanels = new JPanel[3][3]; for (int i = 0; i < innerPanels.length; i++) { for (int j = 0; j < innerPanels[i].length; j++) { innerPanels[i][j] = new JPanel(new GridLayout(3, 3)); panou_centru.add(innerPanels[i][j]); } } for (int i = 0; i < 9; i++) { c[i] = new JTextField[9]; for (int j = 0; j < 9; j++) { c[i][j] = new JTextField("", 3); //panou_centru.add(c[i][j]); c[i][j].setPreferredSize(CELL_SIZE); innerPanels[i/3][j/3].add(c[i][j]); // TODO: For testing purposes only. Delete later c[i][j].setText("" + i + ", " + j); // TODO: delete this } } add("Center", panou_centru); panou_sud.setLayout(new FlowLayout()); panou_sud.add(solve); panou_sud.add(clear); panou_sud.add(exit); add("South", panou_sud); } private static void createAndShowUI() { JFrame frame = new Interfata(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
Much luck and look forward to seeing you here some more.Last edited by Fubarable; 02-07-2010 at 08:19 PM.
- 02-07-2010, 09:12 PM #3
thanks you for your help it helped me a lot
i apology for that mistack it won't happen agen ....
but i find it more aesthetically :
panou_centru.setBorder(new EmptyBorder(3, 3, 3, 3));
then
panou_centru.setBorder(new EmptyBorder(5, 5, 5, 5)); :D
thanks you agen for help matesilence i'm trying to meditate:p
Similar Threads
-
Make multiple JTextFields focus at the same time.
By toymachiner62 in forum AWT / SwingReplies: 6Last Post: 10-09-2009, 02:59 AM -
How to make a Jframe un-focusable when another Jframe is active?
By Robert_85 in forum Advanced JavaReplies: 4Last Post: 04-22-2009, 11:02 PM -
How to validate JTextFields components in JTreetable in single button click.
By selvin_raj in forum AWT / SwingReplies: 1Last Post: 07-03-2008, 01:05 PM -
Problem with JTextFields not null
By romina in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 05:17 AM -
JTextFields with username & password.
By Eric in forum AWT / SwingReplies: 2Last Post: 07-01-2007, 11:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks