Results 1 to 5 of 5

Thread: GUI[help]

  1. #1
    M2G
    M2G is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default GUI[help]

    I have a java project to make a tic-tac-toe game with variable dimensions and variable number of players.
    For example, if the grid is 9x9. Players will play normally trying to make 3 consecutive X or 0. Each player will have a score which will be incremented if he manages to make 3 consecutive X or 0.


    So far I made this:

    i want that buttons grid to be modified by those radiobuttons and I don't know how to do that.
    When i press the radio button 5x5 I want the buttton grid to be modified by that dimension.

    Here I gave that grid variable the value 3 so i could test it.

    Java Code:
        public Xsi0View(SingleFrameApplication app) {
            super(app);
            [COLOR="Red"]grid=3[/COLOR];
            [COLOR="Red"]int dimensiune = (grid*grid)[/COLOR];
            
            initComponents();
            
            for(int i=1; i<=dimensiune; i++){
                buttons[i] = new JButton();
                tabla.add(buttons[i]);
                buttons[i].addActionListener(this);
            }
    
        }
    This is initComponents with the GUI generated by Net Beans:

    Java Code:
    private void initComponents() {
    
            mainPanel = new javax.swing.JPanel();
            tabla = new javax.swing.JPanel();
            dimensiuni = new javax.swing.JPanel();
            dimensiune3 = new javax.swing.JRadioButton();
            dimensiune5 = new javax.swing.JRadioButton();
            dimensiune7 = new javax.swing.JRadioButton();
            dimensiune9 = new javax.swing.JRadioButton();
            grupbutoane = new javax.swing.ButtonGroup();
    
            mainPanel.setName("mainPanel"); // NOI18N
            mainPanel.setPreferredSize(new java.awt.Dimension(988, 873));
    
            tabla.setAutoscrolls(true);
            tabla.setName("tabla"); // NOI18N
            tabla.setLayout(new java.awt.GridLayout([COLOR="Red"]grid, grid[/COLOR]
            ));
    
            dimensiuni.setName("dimensiuni"); // NOI18N
            dimensiuni.setLayout(new java.awt.GridLayout(3, 3));
    
            grupbutoane.add(dimensiune3);
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(xsi0.Xsi0App.class).getContext().getResourceMap(Xsi0View.class);
            dimensiune3.setText(resourceMap.getString("dimensiune3.text")); // NOI18N
            dimensiune3.setName("dimensiune3"); // NOI18N
            dimensiuni.add(dimensiune3);
    
            grupbutoane.add(dimensiune5);
            dimensiune5.setText(resourceMap.getString("dimensiune5.text")); // NOI18N
            dimensiune5.setName("dimensiune5"); // NOI18N
            dimensiuni.add(dimensiune5);
    
            grupbutoane.add(dimensiune7);
            dimensiune7.setText(resourceMap.getString("dimensiune7.text")); // NOI18N
            dimensiune7.setName("dimensiune7"); // NOI18N
            dimensiuni.add(dimensiune7);
    
            grupbutoane.add(dimensiune9);
            dimensiune9.setText(resourceMap.getString("dimensiune9.text")); // NOI18N
            dimensiune9.setName("dimensiune9"); // NOI18N
            dimensiuni.add(dimensiune9);
    
            javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
            mainPanel.setLayout(mainPanelLayout);
            mainPanelLayout.setHorizontalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addGap(66, 66, 66)
                    .addComponent(dimensiuni, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                    .addComponent(tabla, javax.swing.GroupLayout.PREFERRED_SIZE, 708, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
            mainPanelLayout.setVerticalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addContainerGap(114, Short.MAX_VALUE)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                            .addComponent(dimensiuni, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(32, 32, 32))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                            .addComponent(tabla, javax.swing.GroupLayout.PREFERRED_SIZE, 471, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(19, 19, 19))))
            );
    
            setComponent(mainPanel);
        }// </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="comment">
    
        // Variables declaration - do not modify
        private javax.swing.JRadioButton dimensiune3;
        private javax.swing.JRadioButton dimensiune5;
        private javax.swing.JRadioButton dimensiune7;
        private javax.swing.JRadioButton dimensiune9;
        private javax.swing.JPanel dimensiuni;
        private javax.swing.ButtonGroup grupbutoane;
        private javax.swing.JPanel mainPanel;
        private javax.swing.JPanel tabla;
        // End of variables declaration
    This is the full source code:
    Java Code:
    /*
     * Xsi0View.java
     */
    
    package xsi0;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import org.jdesktop.application.SingleFrameApplication;
    import org.jdesktop.application.FrameView;
    
    /**
     * The application's main frame.
     */
    public class Xsi0View extends FrameView implements ActionListener{
    private JButton buttons[] = new JButton[100];
    private int count = 0;
        private int grid;
          private String letter = "";
        private boolean win = false;
            private int[][] winCombinations = new int[][] {
                {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
                {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //virticle wins
                {0, 4, 8}, {2, 4, 6}             //diagonal wins
            };
    
        public Xsi0View(SingleFrameApplication app) {
            super(app);
            grid=3;
            int dimensiune = (grid*grid);
            
            initComponents();
            
            for(int i=1; i<=dimensiune; i++){
                buttons[i] = new JButton();
                tabla.add(buttons[i]);
                buttons[i].addActionListener(this);
            }
    
        }
    
    
    
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            mainPanel = new javax.swing.JPanel();
            tabla = new javax.swing.JPanel();
            dimensiuni = new javax.swing.JPanel();
            dimensiune3 = new javax.swing.JRadioButton();
            dimensiune5 = new javax.swing.JRadioButton();
            dimensiune7 = new javax.swing.JRadioButton();
            dimensiune9 = new javax.swing.JRadioButton();
            grupbutoane = new javax.swing.ButtonGroup();
    
            mainPanel.setName("mainPanel"); // NOI18N
            mainPanel.setPreferredSize(new java.awt.Dimension(988, 873));
    
            tabla.setAutoscrolls(true);
            tabla.setName("tabla"); // NOI18N
            tabla.setLayout(new java.awt.GridLayout(grid, grid
            ));
    
            dimensiuni.setName("dimensiuni"); // NOI18N
            dimensiuni.setLayout(new java.awt.GridLayout(3, 3));
    
            grupbutoane.add(dimensiune3);
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(xsi0.Xsi0App.class).getContext().getResourceMap(Xsi0View.class);
            dimensiune3.setText(resourceMap.getString("dimensiune3.text")); // NOI18N
            dimensiune3.setName("dimensiune3"); // NOI18N
            dimensiuni.add(dimensiune3);
    
            grupbutoane.add(dimensiune5);
            dimensiune5.setText(resourceMap.getString("dimensiune5.text")); // NOI18N
            dimensiune5.setName("dimensiune5"); // NOI18N
            dimensiuni.add(dimensiune5);
    
            grupbutoane.add(dimensiune7);
            dimensiune7.setText(resourceMap.getString("dimensiune7.text")); // NOI18N
            dimensiune7.setName("dimensiune7"); // NOI18N
            dimensiuni.add(dimensiune7);
    
            grupbutoane.add(dimensiune9);
            dimensiune9.setText(resourceMap.getString("dimensiune9.text")); // NOI18N
            dimensiune9.setName("dimensiune9"); // NOI18N
            dimensiuni.add(dimensiune9);
    
            javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
            mainPanel.setLayout(mainPanelLayout);
            mainPanelLayout.setHorizontalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addGap(66, 66, 66)
                    .addComponent(dimensiuni, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                    .addComponent(tabla, javax.swing.GroupLayout.PREFERRED_SIZE, 708, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
            mainPanelLayout.setVerticalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addContainerGap(114, Short.MAX_VALUE)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                            .addComponent(dimensiuni, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(32, 32, 32))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                            .addComponent(tabla, javax.swing.GroupLayout.PREFERRED_SIZE, 471, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(19, 19, 19))))
            );
    
            setComponent(mainPanel);
        }// </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="comment">
    
        // Variables declaration - do not modify
        private javax.swing.JRadioButton dimensiune3;
        private javax.swing.JRadioButton dimensiune5;
        private javax.swing.JRadioButton dimensiune7;
        private javax.swing.JRadioButton dimensiune9;
        private javax.swing.JPanel dimensiuni;
        private javax.swing.ButtonGroup grupbutoane;
        private javax.swing.JPanel mainPanel;
        private javax.swing.JPanel tabla;
        // End of variables declaration
    
        public void actionPerformed(ActionEvent e) {
            
          
             count++;
    
            /*Calculate whose turn it is*/
            if(count % 2 == 0){
                letter = "O";
            } else {
                letter = "X";
            }
    
            /*Write the letter to the button and deactivate it*/
             JButton pressedButton = (JButton)e.getSource();
             pressedButton.setText(letter);
             pressedButton.setEnabled(false);
            /*Determine who won*/
            for(int i=0; i<=7; i++){
                if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) &&
                    buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) &&
                    (buttons[winCombinations[i][0]].getText() == null ? "" != null : !buttons[winCombinations[i][0]].getText().equals(""))){
                    win = true;
                }
            }
    
            /*Show a dialog when game is over*/
          /*  if(win == true){
                JOptionPane.showMessageDialog(null, letter + " wins the game!");
                System.exit(0);
            } else if(count == 9 && win == false){
                JOptionPane.showMessageDialog(null, "The game was tie!");
                System.exit(0);}*/
            }
        }
    How can I make this work?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

  3. #3
    M2G
    M2G is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks! I will do my best!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Last edited by DarrylBurke; 12-31-2010 at 08:15 PM.

  5. #5
    M2G
    M2G is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Sorry about cross posting. I needed a fast answer. Anyway i solved the problem and i managed to code what i wanted.

    This is how i made it!

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class GUI extends JFrame{
        static final String dimensiune[] = {"3", "5", "7", "9"};
        private int dimbuton = 3;
        JComboBox Dimensiunea;
            
        private JButton buttons[]= new JButton[90];
        JButton okButton = new JButton("OK");
        GridLayout tablaLayout = new GridLayout();
        
        public GUI(String name) {
            super(name);
            setResizable(true);
        }
        
        public void addComponentsToPane(final Container pane) {
            
            Dimensiunea = new JComboBox(dimensiune);
            final JPanel tabla = new JPanel();
            tabla.setLayout(tablaLayout);
            JPanel control = new JPanel();
            control.setLayout(new GridLayout(1,1));
            
    
            
            //Adaugare butoane
           
            
            //adaugare controale
            control.add(new Label("Selectati Dimensiunea:"));
            control.add(Dimensiunea);
            control.add(okButton);
            
            //Actiuni asupra bunotnului OK
            okButton.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    //setare dimensiuni
                    String dim = (String)Dimensiunea.getSelectedItem();
                    tabla.removeAll();
                   dimbuton=Integer.parseInt(dim);
    
                    for(int i=1; i<=dimbuton*dimbuton; i++){
            buttons[i] = new JButton();
            tabla.add(buttons[i]);
    
            }
                    //Setare coloane si randuri |revalidare si repaint|
                    tablaLayout.setColumns(dimbuton);
                    tablaLayout.setRows(dimbuton);
                    
                    tabla.revalidate();
                    tabla.repaint();
                    //setare layout
                   tablaLayout.layoutContainer(tabla);/////////////////
                }
            });
            pane.add(control, BorderLayout.SOUTH);
            pane.add(tabla, BorderLayout.CENTER);
           //pane.add(new JSeparator(), BorderLayout.CENTER);
            
        }
        
        /**
         * Create the GUI and show it.  For thread safety,
         * this method is invoked from the
         * event dispatch thread.
         */
        private static void createAndShowGUI() {
            //creaza fereastra.
            GUI frame = new GUI("X-0");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Seteaza continutul
            frame.addComponentsToPane(frame.getContentPane());
            //Afiseaza fereastra si seteaza dimensiunile.
            frame.setBounds(400, 20, 700, 700);
            //frame.pack();
            frame.setVisible(true);
        }
        
        public static void main(String[] args) {
    
    
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •