Java视图系统隐藏/显示n jpanels

时间:2014-04-22 00:58:41

标签: java jpanel views hide show

我在java开始一个新项目,我正在开发一个视图控制系统"到目前为止,我一直做得很好,但是作为我第一次开发一个长期项目,如果我不是100%确定这将是好的,我不想使用它。 我希望大家看看代码并提出任何建议。

代码

package Main;

import Panels.BluePanel;
import Panels.RedPanel;
import javax.swing.JPanel;


public class MainDialog extends javax.swing.JFrame {

    private BluePanel jpnBodyBlue;
    private RedPanel jpnBodyRed;
    private BodyPanels currentBodyPanel;

    /**
     * Creates new form Principal
     */
    public MainDialog() {
        initComponents();
        initPanels();
    }


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

        jpnContenedor = new javax.swing.JPanel();
        jpnIzquierdo = new javax.swing.JPanel();
        jpnBody = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        btnMenuPanel1 = new javax.swing.JButton();
        btnMenuPanel2 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(java.awt.Color.white);

        jpnContenedor.setBackground(new java.awt.Color(255, 255, 255));

        jpnIzquierdo.setBackground(new java.awt.Color(240, 240, 240));

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

        jpnBody.setBackground(new java.awt.Color(65, 65, 65));
        jpnBody.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                jpnBodyComponentResized(evt);
            }
        });
        jpnBody.setLayout(null);

        javax.swing.GroupLayout jpnContenedorLayout = new javax.swing.GroupLayout(jpnContenedor);
        jpnContenedor.setLayout(jpnContenedorLayout);
        jpnContenedorLayout.setHorizontalGroup(
            jpnContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnContenedorLayout.createSequentialGroup()
                .addComponent(jpnIzquierdo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpnBody, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpnContenedorLayout.setVerticalGroup(
            jpnContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpnIzquierdo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jpnBody, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jToolBar1.setFloatable(false);

        btnMenuPanel1.setText("Pan 1");
        btnMenuPanel1.setFocusable(false);
        btnMenuPanel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnMenuPanel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnMenuPanel1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMenuPanel1ActionPerformed(evt);
            }
        });
        jToolBar1.add(btnMenuPanel1);

        btnMenuPanel2.setText("Pan 2");
        btnMenuPanel2.setFocusable(false);
        btnMenuPanel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnMenuPanel2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnMenuPanel2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMenuPanel2ActionPerformed(evt);
            }
        });
        jToolBar1.add(btnMenuPanel2);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
            .addComponent(jpnContenedor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpnContenedor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    private void btnMenuPanel1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        controlPanels(BodyPanels.Blue);
    }                                             

    private void btnMenuPanel2ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        controlPanels(BodyPanels.Red);
    }                                             

    /**
     * Resizes the currentBodyPanel everytime the bodyPanel container is
     * resized.
     *
     * @param evt
     */
    private void jpnBodyComponentResized(java.awt.event.ComponentEvent evt) {                                         
        //Checks if there is any currentBodyPanel (as it starts as null).
        if (currentBodyPanel == null) {
            return;
        }
        JPanel newPanel = null;
        switch (currentBodyPanel) {
            case Blue:
                newPanel = jpnBodyBlue;
                break;
            case Red:
                newPanel = jpnBodyRed;
                break;
        }
        newPanel.setBounds(0, 0, jpnBody.getWidth(), jpnBody.getHeight());  // TODO add your handling code here:
    }                                        


    // Variables declaration - do not modify                     
    private javax.swing.JButton btnMenuPanel1;
    private javax.swing.JButton btnMenuPanel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JPanel jpnBody;
    private javax.swing.JPanel jpnContenedor;
    private javax.swing.JPanel jpnIzquierdo;
    // End of variables declaration                   

    public void controlPanels(BodyPanels newPanel) {
        setPanelVisible(newPanel);
    }

    /**
     * Creates the possible body panels.
     */
    private void initPanels() {
        jpnBodyBlue = new BluePanel();
        jpnBody.add(jpnBodyBlue);
        jpnBodyBlue.setBounds(0, 0, jpnBody.getWidth(), jpnBody.getHeight());
        jpnBodyBlue.setVisible(false);

        jpnBodyRed = new RedPanel();
        jpnBody.add(jpnBodyRed);
        jpnBodyRed.setBounds(0, 0, jpnBody.getWidth(), jpnBody.getHeight());
        jpnBodyRed.setVisible(false);
    }

    /**
     * Hides the currentBodyPanel and shows the Panel passed to the method.
     *
     * @param panel
     */
    public void setPanelVisible(BodyPanels panel) {
        JPanel newPanel;
        if (currentBodyPanel != null) {
            switch (currentBodyPanel) {
                case Blue:
                    jpnBodyBlue.setVisible(false);
                    break;
                case Red:
                    jpnBodyRed.setVisible(false);
                    break;
                default:
                    break;
            }
        }
        switch (panel) {
            case Blue:
                newPanel = jpnBodyBlue;
                break;
            case Red:
                newPanel = jpnBodyRed;
                break;
            default:
                System.err.println("No action.");
                newPanel = null;
                break;
        }
        currentBodyPanel = panel;
        newPanel.setVisible(true);
    }

    /**
     * Enum for the possible body panels.
     */
    public enum BodyPanels {

        Red, Blue;
    }

}

BluePanel和RedPanel都是JPanel的扩展版本。这也可以作为一个例子,因为我已经完成了单一课程的整个工作。

我还想知道是否有任何lib已经完成了我在这里尝试做的事情。

0 个答案:

没有答案