为什么不重绘()在我的JPanel上工作?

时间:2013-08-05 16:09:00

标签: java swing jpanel paintcomponent repaint

我已经创建了自己的JPanel子类,名为SubPatternPanel。我想在框架中使用它来绘制网格,但是当我从框架中为repaint()调用SubPatternPanel时,它不起作用。

运行时,框架会显示“新建”按钮。当按下New时,我的面板应该绘制一个网格,但它不是,我找不到原因。

以下是SubPatternPanel的代码:

package View;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;


public class SubPatternPanel extends JPanel
{

    public SubPatternPanel()
    {
    setLayout(null);
        setBackground(Color.white);
        setPreferredSize(new Dimension(240, 240));
    }

    @Override
    public void paintComponent(Graphics g)
    {   
    super.paintComponent(g);

    g.setColor(Color.black);

    for (int i = 0; i < 3; i++) 
    {
            for (int j = 0; j < 3; j++) 
        {
                g.drawLine(i * 80, j * 80, 240, j * 80);
                g.drawLine(i * 80, j * 80, i * 80, 240);
            }
        }
    }
}

大多数帧代码都是自动生成的,所以我不知道该怎样留下更清晰的画面。

package View;

import java.awt.BorderLayout;

public class PatternsMainFrame extends javax.swing.JFrame
{

    private SubPatternPanel subPatternPanel;

    public PatternsMainFrame()
    {
    initComponents();
    subPatternPanel = new SubPatternPanel();
    patternStartPanel.add(subPatternPanel, BorderLayout.CENTER);
    }

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

        patternStartPanel = new javax.swing.JPanel();
        newButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout patternStartPanelLayout = new javax.swing.GroupLayout(patternStartPanel);
        patternStartPanel.setLayout(patternStartPanelLayout);
        patternStartPanelLayout.setHorizontalGroup(
            patternStartPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 277, Short.MAX_VALUE)
        );
        patternStartPanelLayout.setVerticalGroup(
            patternStartPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 278, Short.MAX_VALUE)
        );

        newButton.setText("New");
        newButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                newButtonActionPerformed(evt);
            }
        });

        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(patternStartPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 248, Short.MAX_VALUE)
                .addComponent(newButton)
                .addGap(112, 112, 112))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(patternStartPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(newButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }                      

    private void newButtonActionPerformed(java.awt.event.ActionEvent evt)                                          
    {
    subPatternPanel.repaint();
    }                                         

    public static void main(String args[])
    {
    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(PatternsMainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex)
    {
        java.util.logging.Logger.getLogger(PatternsMainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex)
    {
        java.util.logging.Logger.getLogger(PatternsMainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex)
    {
        java.util.logging.Logger.getLogger(PatternsMainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }

    java.awt.EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
        new PatternsMainFrame().setVisible(true);
        }
    });
    }                    
    private javax.swing.JButton newButton;
    private javax.swing.JPanel patternStartPanel;                 
}

1 个答案:

答案 0 :(得分:1)

您应该使用setVisible(true)代替repaint()

repaint首先检查可见性是否设置为true。当您想要重绘已经绘制过一次的内容时,通常会调用repaint()