繁体   English   中英

Java repaint()在jPanel类中不起作用

[英]Java repaint() is not working in jPanel class

我想每次调用名为change()的方法时重新绘制jPanel。 在这种方法中,我只是更改了布尔变量draw而将其称为this.repaint() 可以在面板上绘画,但是如果我单击按钮,线条仍然存在,但是线条应该消失了。 在调用repaint()我无法到达paintComponent()方法。 为什么方法repaint()无法正常工作?

这是我来自面板类的代码:

import java.awt.Graphics;

public class testPanel extends javax.swing.JPanel {

    public boolean draw = true;

    public testPanel() {
        initComponents();
    }

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

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 603, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 299, Short.MAX_VALUE)
        );
    }// </editor-fold>                        

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (draw == true) {
            g.drawLine(0, 0, 20, 35);
        }
    }

    public void change() {
        draw = !draw;
        this.repaint();

    }

}

编辑,这就是我访问方法change()

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    testPanel testPanel = new testPanel();
    testPanel.change();

}       

编辑,如何将jPanel添加到我的jFrame中:

private void initComponents() {

    jPanel1 = new testPanel();
    jButton1 = new javax.swing.JButton();
...

从给定的编辑:

在您的jButton1ActionPerformed方法中。 不必每次单击按钮都创建一个新的testPanel ,而是使用变量并在JFrame实际显示的testPanel实例上调用更改。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    jPanel1.change();
}

这是一个示例,它如何工作:

public class TestFrame extends JFrame{

    private testPanel panel = new testPanel(); // This is the pane with the line and that is actually visible to you.

    private JPanel underlayingPanel = new JPanel(); // This is the underlaying pane.

    public TestFrame() {

        underlayingPanel.setLayout(new BorderLayout());
        underlayingPanel.add(panel, BorderLayout.CENTER);
        //
        JButton button = new JButton("Press me");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // This is what you did initially
                //     testPanel panel = new testPanel();
                //     panel.change();
                // The method change is getting executed on the instance of testPanel that is stored inside the variable panel.
                // but the Panel that did get added onto your underlaying panel wont notice this change since it represents another instance
                // of testPanel. In order to make this panel notice the change invoke it on this specific instance
                panel.change();
            }
        });
        underlayingPanel.add(button, BorderLayout.NORTH);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800, 800);
        this.setContentPane(underlayingPanel);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new TestFrame();
    }
} 

作品:

    public static void main(String[] args){


    TestPanel panel = new TestPanel();

    JButton button = new JButton();
    ActionListener al = new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
         panel.change();
        }

    };

    button.addActionListener(al);

    JFrame frame = new JFrame();

    frame.add(panel);
    frame.add(button);

    frame.setVisible(true);
    frame.setLayout(new GridLayout(2, 1));
    frame.setSize(420, 360);



    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



}

或带有笑脸的有趣例子:

    /*
 * 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.
 */
package repaintquestions;

/**
 *
 * @author peter
 */
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class TestPanel extends javax.swing.JPanel {

    public boolean draw = true;

    public TestPanel() {
        initComponents();
    }

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

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 603, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 299, Short.MAX_VALUE)
        );
    }// </editor-fold>                        

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (draw == true) {
            // g.drawLine(0, 0, 20, 35);
        }
        paintSmile(g, draw);
    }

    public void change() {
        draw = !draw;

        this.repaint();

    }

    public void paintSmile(Graphics g, boolean smile) {

        g.setColor(Color.black);

        g.fillRect(0, 0, 400, 400);

        g.setColor(Color.yellow);

        g.fillOval(0, 0, 400, 400);

        g.setColor(Color.black);

        g.fillOval(100, 100, 50, 50);

        g.fillOval(250, 100, 50, 50);

        g.drawArc(150, 250, 100, 100, 180, 180);

        if (smile) {
            g.drawArc(150, 250, 100, 100, 180, 180);
        } else {
            g.drawArc(150, 250, 100, 100, 0, 180);
        }

      //  repaint();
    }

    public static void main(String[] args) {

        TestPanel panel = new TestPanel();

        JButton button = new JButton();
        ActionListener al = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                panel.change();
            }

        };

        button.addActionListener(al);

        JFrame frame = new JFrame();

        frame.add(panel);
        frame.add(button);

        frame.setVisible(true);
        frame.setLayout(new GridLayout(2, 1));
        frame.setSize(800, 800);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM