mardi 14 juin 2016

Change JFrame(without layout) to JPanel

I have to change my program's GUI implemented in Swing. Now I have many charts in seperate windows and I want to change it so they all display in only one. The constraction of the orginal version was that JFrame had a JPanel field. JFrame had no set layout.

private void initComponents() {// GEN-BEGIN:initComponents

        getContentPane().setLayout(null);

        setTitle("Wykres");
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                resizedWindow(evt);
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width - MIN_WIDTH) / 2, (screenSize.height - MIN_HEIGHT) / 2, MIN_WIDTH, MIN_HEIGHT);
    }

JPanel was declared as follows:

BorderLayout borderLayout = new BorderLayout();
            borderLayout.setHgap(0);
            borderLayout.setVgap(0);
            drawPanel = new JDrawPanel();
            drawPanel.setLayout(borderLayout);
            drawPanel.setComponentOrientation(ComponentOrientation.UNKNOWN);
setContentPane(drawPanel);

If I remove inheritance by JFrame and add inheritance by JPanel, the method paintComponent is not called. I think the reason is bad layout settings. However program calculates the distances from the edges, and I don't want to change it. How can I easily edit this program?

I also generated new GUI in NETBEANS

public class ViewPanel extends javax.swing.JFrame {

    public ViewPanel() {
       // initComponents();
    }
    public ViewPanel(JPanel histo, JPanel dystrybu, JPanel time){
        initComponents(histo, dystrybu, time);
    }
    /**
     * 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(JPanel histo, JPanel dystrybu, JPanel time) {

        MainTabs = new javax.swing.JTabbedPane();
        jHistoTab = histo;
        jDystybuTab = dystrybu;
        jTimeTab = time;



        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("zaleznosc czasowa");

        MainTabs.setToolTipText("");
        MainTabs.addTab("histogram", jHistoTab);
        MainTabs.addTab("dystrybuanta", jDystybuTab);
        MainTabs.addTab("zalenosc czasowa", jTimeTab);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(MainTabs, javax.swing.GroupLayout.DEFAULT_SIZE, 712, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(MainTabs, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                .addContainerGap())
        );

        MainTabs.getAccessibleContext().setAccessibleName("");


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

    /**
     * @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(ViewPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ViewPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ViewPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ViewPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ViewPanel().setVisible(true);
                AppSMO.main(null);
            }
        });
    }
    public void setHistoTab(JPanel panel){
        jHistoTab=panel;
    }

    // Variables declaration - do not modify                     
    private javax.swing.JTabbedPane MainTabs;
    private JPanel jDystybuTab;
    private JPanel jHistoTab;
    private JPanel jTimeTab;
    // End of variables declaration                   
}

Aucun commentaire:

Enregistrer un commentaire