Java GUI面板

时间:2015-03-28 18:07:03

标签: java swing user-interface jframe jpanel

我正在尝试为我的一个课程制作一个游戏,但是我在实现一些我想要做的事情时遇到了麻烦。基本上,我想在同一个图形用户界面窗口中有多个不同的部分。

根据我的理解,我会使用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制作成某种尺寸,还是只是通过特定的定位来满足我想要的一切?

EDIT V2

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

基本上我想要的东西和想要构建的东西类似于这种布局

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

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

这会是一个体面的方式吗?

(另外,关于我的问题一般来说我应该改进的任何提示都会很好。)

2 个答案:

答案 0 :(得分:1)

  

在initializeWindow()部分放置什么来创建不同的部分,

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

当然没有太多东西可以看,因为面板不包含任何组件。你的问题太模糊,无法给出具体答案。

  

以及我如何判断它们是可见的,如边框或颜色或其他东西,只是看到它在那里。

您可以在面板中添加Border。类似的东西:

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

阅读How to Use Borders上的Swing教程中的部分以获取更多信息。

答案 1 :(得分:0)

如果我假设正确,问题是如何使用不同的JPanles JFrame或其他JPanel,以及他们如何与他们互动。这是Swing中的NetBeans。我喜欢使用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                   
}