Zayne Kinkade
Zayne Kinkade

Reputation: 15

Java GUI Panels

I am trying to make a game for one of my classes, but I am running into trouble in implementing some of the things I want to do. Essentially, I want multiple different parts inside the same graphical user interface window.

To my understanding I would use JPanels to create these different parts and put them in a larger JPanel, am I right in this?

I have my code here, but really I need help just on what to do going about all of this.

public class FarkleWindow extends JFrame{

    private int windowWidth = 800;
    private int windowHeight = 600;

    private JPanel player1Dice, player2Dice, dicePanel, infoBox;
    private FarkleDisplay gameBoard;public FarkleWindow()
    {
        this.setTitle("Farkle!");
        this.setSize(windowWidth,windowHeight);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        inititalizeWindow();

        this.setVisible(true);

    }


    private void inititalizeWindow() {
        gameBoard = new FarkleDisplay();

        this.add(gameBoard, BorderLayout.CENTER);

        //addMenuOptions();

        player1Dice = new JPanel();
        gameBoard.add(player1Dice);

    }
}

I would like to know what to put in the initializeWindow() part to create the different parts, and how I would tell that they are visible, like a border or color or something to just see that it is there.

Any help would be appreciated!

EDIT

I am reading more on this, and as I look at it I have more questions. Can I make JPanels certain sizes, or is it just through specific positioning that I can fit everything I want?

EDIT V2

So it was said that I should include more information on what kind of layout I want for an interface, and thank you guys for the help I do have! I am still getting used to using and posting to Stack Overflow rather than just searching for what I am looking for on it.

Basically what I am going for and looking to build is something similar to this layout

http://bestbackgammon.com/farkle/index_files/image001.jpg

I want each part a separate Panel, if that is a good way to go about it. So having the two side rectangles as Panels and the center Square a Panel, and a fourth Panel for the brown text box type thing inside the middle Square.

Would this be a decent way to go about that?

(Also, any tips on what I should improve for my question in general would be good.)

Upvotes: 1

Views: 600

Answers (2)

camickr
camickr

Reputation: 324197

what to put in the initializeWindow() part to create the different parts,

player1Dice = new JPanel();
gameBoard.add(player1Dice);

You can also do something like:

JPanel panel1 = new JPanel();
panel1.setBackground( Color.GREEN );
gameBoard.add(panel1);
JPanel panel2 = new JPanel();
panel2.setBackground( Color.BLUE );
gameBoard.add(panel2);

Of course there won't be much to see because the panel doesn't contain any components. Your question is too vague to give a specific answer.

and how I would tell that they are visible, like a border or color or something to just see that it is there.

You can add a Border to the panel. Something like:

panel.setBorder( BorderFactory.createLineBorder(Color.RED) ); 

Read the section from the Swing tutorial on How to Use Borders for more information.

Upvotes: 1

mrbigheart
mrbigheart

Reputation: 148

If I assume correctly, the question is how to use different JPanles, in the same JFrame, or another JPanel, and how they you can interact with them. This is made with Swing in NetBeans. I like using AbsoluteLayout, but feel free to use anything else. I have a graphic designer's eye so it is better for me.

package panels;

/**
 *
 * @author Agnosto Theo
 */
public class panels extends javax.swing.JFrame {

    /**
     * Creates new form panels
     */
    public panels() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jPanel1MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jPanel1MouseExited(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 0, -1, -1));

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jPanel2MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jPanel2MouseExited(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 0, -1, -1));

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jPanel3MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jPanel3MouseExited(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, -1, -1));

        pack();
    }// </editor-fold>                        

    private void jPanel3MouseEntered(java.awt.event.MouseEvent evt) {                                     
        jPanel3.setBackground(java.awt.Color.DARK_GRAY);
    }                                    

    private void jPanel3MouseExited(java.awt.event.MouseEvent evt) {                                    
        jPanel3.setBackground(java.awt.Color.LIGHT_GRAY);
    }                                   

    private void jPanel1MouseEntered(java.awt.event.MouseEvent evt) {                                     
        jPanel1.setBackground(java.awt.Color.DARK_GRAY);
    }                                    

    private void jPanel1MouseExited(java.awt.event.MouseEvent evt) {                                    
        jPanel1.setBackground(java.awt.Color.LIGHT_GRAY);
    }                                   

    private void jPanel2MouseExited(java.awt.event.MouseEvent evt) {                                    
        jPanel2.setBackground(java.awt.Color.LIGHT_GRAY);
    }                                   

    private void jPanel2MouseEntered(java.awt.event.MouseEvent evt) {                                     
        jPanel2.setBackground(java.awt.Color.DARK_GRAY);
    }                                    

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(panels.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(panels.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(panels.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(panels.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new panels().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    // End of variables declaration                   
}

Upvotes: 0

Related Questions