为什么未调用我覆盖的paintComponent方法

时间:2019-06-16 04:50:09

标签: paintcomponent

我正在使用Netbeans。我设置了一个图形程序来作图。我覆盖了3个JPanel paintComponent方法,只是为了入门,但是这些方法没有被调用。

我有可以运行的程序。我的新替代代码看起来相同。我已经尝试了很多东西,但想法却用光了。我是第一次使用Borderlayout,但这应该不会引起问题。

import java.awt.Graphics;
import java.awt.Graphics2D;

/*
* To change this license header, choose License Headers in Project 
*Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

public class MainForm extends javax.swing.JFrame
{
/**
* Creates new form NewJFrame
*/
public MainForm()
{
   initComponents();
   initProgram();
}

private void initProgram()
{
   System.out.println("InitProgram");
}

/**
* 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()
{

   pnlPrintPanel = new javax.swing.JPanel();
   pnlGraphForm = new javax.swing.JPanel();
   pnlGraph = new javax.swing.JPanel();
   jLabel2 = new javax.swing.JLabel();
   jLabel1 = new javax.swing.JLabel();
   pnlInputPanel = new javax.swing.JPanel();
   jButton1 = 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);

   pnlGraphForm.setBorder
     (javax.swing.BorderFactory.createBevelBorder(
     javax.swing.border.BevelBorder.RAISED));

   pnlGraph.setBorder
     (javax.swing.BorderFactory.createBevelBorder(
     javax.swing.border.BevelBorder.RAISED));

   jLabel2.setText("jLabel2");

   javax.swing.GroupLayout pnlGraphLayout =
      new javax.swing.GroupLayout(pnlGraph);
   pnlGraph.setLayout(pnlGraphLayout);
   pnlGraphLayout.setHorizontalGroup(
   pnlGraphLayout.createParallelGroup(
      javax.swing.GroupLayout.Alignment.LEADING)
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, 
     pnlGraphLayout.createSequentialGroup()
     .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,  
      Short.MAX_VALUE)
         .addComponent(jLabel2)
         .addGap(277, 277, 277))
   );
   pnlGraphLayout.setVerticalGroup(
   pnlGraphLayout.createParallelGroup(
     javax.swing.GroupLayout.Alignment.LEADING)
     .addGroup(pnlGraphLayout.createSequentialGroup()
        .addGap(101, 101, 101)
        .addComponent(jLabel2)
        .addContainerGap(204, Short.MAX_VALUE))
  );

  jLabel1.setText("jLabel1");

  javax.swing.GroupLayout pnlGraphFormLayout = 
     new javax.swing.GroupLayout(pnlGraphForm);
   pnlGraphForm.setLayout(pnlGraphFormLayout);
   pnlGraphFormLayout.setHorizontalGroup(
   pnlGraphFormLayout.createParallelGroup(
     javax.swing.GroupLayout.Alignment.LEADING)
     .addGroup(pnlGraphFormLayout.createSequentialGroup()
        .addGap(30, 30, 30)
        .addComponent(pnlGraph, javax.swing.GroupLayout.DEFAULT_SIZE,
         javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addContainerGap())
     .addGroup(pnlGraphFormLayout.createSequentialGroup()
        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE,
        59, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(0, 0, Short.MAX_VALUE))
   );
   pnlGraphFormLayout.setVerticalGroup(
   pnlGraphFormLayout.createParallelGroup(
     javax.swing.GroupLayout.Alignment.LEADING)
     .addGroup(pnlGraphFormLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(pnlGraph, javax.swing.GroupLayout.DEFAULT_SIZE,   
     javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addPreferredGap(
          javax.swing.LayoutStyle.ComponentPlacement.RELATED)
           .addComponent(jLabel1)
           .addContainerGap())
     );

    pnlGraph = new javax.swing.JPanel()
    {
      //@Override
     //public void paint(Graphics g)
     @Override
     public void paintComponent(Graphics g)
     {
        System.out.println("In pnlGraph.paintComponent");
        //super.paint(g);

        /*
        if (grapher == null)
        {
           return;
        }
        */

        super.paintComponent(g);
        //            grapher.drawSlate((Graphics2D)g);
        pack();
     }
  };

   pnlInputPanel.setBorder(
   javax.swing.BorderFactory.createBevelBorder(
   javax.swing.border.BevelBorder.RAISED));

  jButton1.setText("jButton1");
  jButton1.addActionListener(new java.awt.event.ActionListener()
  {
     public void actionPerformed(java.awt.event.ActionEvent evt)
     {
        jButton1ActionPerformed(evt);
     }
  });

  javax.swing.GroupLayout pnlInputPanelLayout = 
     new javax.swing.GroupLayout(pnlInputPanel);
  pnlInputPanel.setLayout(pnlInputPanelLayout);
  pnlInputPanelLayout.setHorizontalGroup(
     pnlInputPanelLayout.createParallelGroup(
      javax.swing.GroupLayout.Alignment.LEADING)
     .addGroup(pnlInputPanelLayout.createSequentialGroup()
        .addGap(271, 271, 271)
        .addComponent(jButton1)
        .addContainerGap(297, Short.MAX_VALUE))
  );
  pnlInputPanelLayout.setVerticalGroup(
        pnlInputPanelLayout.createParallelGroup(
       javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(pnlInputPanelLayout.createSequentialGroup()
         .addGap(59, 59, 59)
        .addComponent(jButton1)
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
      Short.MAX_VALUE))
  );

  javax.swing.GroupLayout pnlPrintPanelLayout = 
    new javax.swing.GroupLayout(pnlPrintPanel);
  pnlPrintPanel.setLayout(pnlPrintPanelLayout);
  pnlPrintPanelLayout.setHorizontalGroup(
     pnlPrintPanelLayout.createParallelGroup(
       javax.swing.GroupLayout.Alignment.LEADING)
     .addGroup(pnlPrintPanelLayout.createSequentialGroup()
        .addContainerGap()

   .addGroup(pnlPrintPanelLayout.createParallelGroup(
     javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(pnlPrintPanelLayout.createSequentialGroup()
              .addComponent(pnlInputPanel,
        javax.swing.GroupLayout.DEFAULT_SIZE,
        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              .addContainerGap())
           .addComponent(pnlGraphForm,   
          javax.swing.GroupLayout.DEFAULT_SIZE, 
          javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  );
  pnlPrintPanelLayout.setVerticalGroup(
     pnlPrintPanelLayout.createParallelGroup(
        javax.swing.GroupLayout.Alignment.LEADING)
     .addGroup(pnlPrintPanelLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(pnlGraphForm, 
        javax.swing.GroupLayout.DEFAULT_SIZE, 
        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

        .addComponent(pnlInputPanel, 
        javax.swing.GroupLayout.DEFAULT_SIZE, 
        javax.swing.GroupLayout.DEFAULT_SIZE, 
        javax.swing.GroupLayout.PREFERRED_SIZE))
  );

  pnlGraphForm = new javax.swing.JPanel()
  {
     //@Override
     //public void paint(Graphics g)
     @Override
     public void paintComponent(Graphics g)
     {
        System.out.println("In pnlGraphForm.paintComponent");
        //super.paint(g);

        /*
        if (grapher == null)
        {
           return;
        }
        */

        super.paintComponent(g);
        //            grapher.drawSlate((Graphics2D)g);
        pack();
     }
  };

  getContentPane().add(pnlPrintPanel, java.awt.BorderLayout.CENTER);

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

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

  setJMenuBar(jMenuBar1);

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

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)                                         
{                                             
   System.out.println("BUTTON");
   pnlGraphForm.paintComponents((Graphics2D) 
   pnlGraphForm.getGraphics());
}                                        

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

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel pnlGraph;
private javax.swing.JPanel pnlGraphForm;
private javax.swing.JPanel pnlInputPanel;
private javax.swing.JPanel pnlPrintPanel;
// End of variables declaration                   
}

在这个阶段,我希望看到打印的面板/方法名称,但是没有发生

0 个答案:

没有答案