Java GUI 面板

Java GUI Panels

我正在尝试为我的 类 制作一款游戏,但我 运行 在实现我想做的一些事情时遇到了麻烦。本质上,我想要在同一个图形用户界面中包含多个不同的部分 window。

据我了解,我会使用 JPanels 创建这些不同的部分并将它们放在更大的 JPanel 中,我这样做对吗?

我这里有我的代码,但我真的需要帮助来解决所有这些问题。

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);

    }
}

我想知道要在 initializeWindow() 部分中放入什么来创建不同的部分,以及我如何判断它们是可见的,例如边框或颜色或只是看到它在那里的东西。

如有任何帮助,我们将不胜感激!

编辑

我正在阅读更多这方面的内容,并且在查看它时我有更多的问题。我可以将 JPanel 设置为特定尺寸,还是仅通过特定定位就可以满足我的所有需求?

编辑 V2

所以有人说我应该包括更多关于我想要什么样的界面布局的信息,谢谢你们对我的帮助!我仍然习惯于使用 Stack Overflow 并将其发布到 Stack Overflow,而不仅仅是在上面搜索我正在寻找的内容。

基本上我想要构建的东西与此布局类似

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

我希望每个部分都有一个单独的面板,如果这样做是个好方法的话。因此,将两侧的矩形作为面板,将中心正方形作为面板,第四个面板用于中间正方形内的棕色文本框类型。

这是解决问题的好方法吗?

(此外,任何关于我应该针对我的问题进行改进的建议都会很好。)

如果我假设正确,问题是如何在相同的环境中使用不同的 JPanles JFrame,或另一个 JPanel,以及您如何与他们互动。这是在 NetBeans 中用 Swing 制作的。我喜欢使用 AbsoluteLayout,但也可以随意使用其他任何东西。我有平面设计师的眼光所以更适合我。

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                   
}

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

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

您还可以执行以下操作:

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

当然不会有太多可看的,因为面板不包含任何组件。你的问题太笼统,无法给出具体答案。

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

您可以在面板中添加 Border。类似于:

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

阅读 How to Use Borders 上的 Swing 教程部分了解更多信息。