Java Swing jpanel paint方法执行两次的问题
Java Swing jpanel paint方法执行两次的问题:
在其他环境下执行了两次,自己测试怎么都是执行了一次,记录一下这个问题;需要后继工作;
可能是进行各种参数设置的时候导致了paint方法的调用;
在调试过程中,发现repaint方法会因为各种情况不断的调用;注意JFrame和JPanel是不一样的哦;
package com.srie.test; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class JFrame002 extends JFrame { private static final long serialVersionUID = 1L; private JPanel contentPane; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { JFrame002 frame = new JFrame002(); frame.setSize(80, 60); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public JFrame002() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel() { private static final long serialVersionUID = 1L; int j = 10000; // 如果override paint方法,就不会执行paintComponent方法 @Override public void paint(Graphics g) { System.out.println("com.srie.test.JFrame002.JFrame002().new JPanel() {...}.paint(Graphics):" + j++); } @Override protected void paintComponent(Graphics g) { System.out.println("com.srie.test.JFrame002.JFrame002().new JPanel() {...}.paintComponent(Graphics):" + j++); } @Override public void update(Graphics g) { System.out.println("com.srie.test.JFrame002.JFrame002().new JPanel() {...}.update(Graphics):" + j++); } @Override public void repaint() { // 最开始打印的时候,j输出的竟然是0,在4种初始化之后,才把j初始化了; System.out.println("com.srie.test.JFrame002.JFrame002().new JPanel() {...}.repaint():" + j++); } }; contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); // 这个会增加一次repaint方法的调用; contentPane.setFont(null); // contentPane.setMinimumSize(new Dimension(10, 10)); setContentPane(contentPane); } int i = 0; // 这个放开了之后,里面的内容都不进行重绘了;只是在初始化的时候里面repaint了6次; // @Override // public void paint(Graphics g) { // System.out.println("com.srie.test.JFrame002.paint(Graphics):" + i++); // } // 这个不是paintComponent,多了一个s哦;是java.awt.Container的方法; @Override public void paintComponents(Graphics g) { System.out.println("com.srie.test.JFrame002.paintComponents(Graphics):" + i++); } @Override public void update(Graphics g) { System.out.println("com.srie.test.JFrame002.update(Graphics):" + i++); } @Override public void repaint() { System.out.println("com.srie.test.JFrame002.repaint():" + i++); } }
参考:
import java.awt.BorderLayout; import java.awt.Color; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Date; import javax.swing.*; public class SimpleGui3C implements ActionListener { JFrame frame; public static void main(String[] args) { SimpleGui3C gui = new SimpleGui3C(); gui.go(); } public void go() { frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Change colors"); button.addActionListener(this); MyDrawPanel drawPanel = new MyDrawPanel(); frame.getContentPane().add(BorderLayout.SOUTH, button); frame.getContentPane().add(BorderLayout.CENTER, drawPanel); frame.setResizable(false); frame.setSize(300, 300); frame.setVisible(true); } @Override public void actionPerformed(ActionEvent arg0) { frame.repaint(); } class MyDrawPanel extends JPanel { private static final long serialVersionUID = 1L; public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; int red = (int) (Math.random() * 255); int green = (int) (Math.random() * 255); int blue = (int) (Math.random() * 255); Color startColor = new Color(red, green, blue); red = (int) (Math.random() * 255); green = (int) (Math.random() * 255); blue = (int) (Math.random() * 255); Color endColor = new Color(red, green, blue); GradientPaint gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor); g2d.setPaint(gradient); g2d.fillOval(70, 70, 100, 100); System.out.println(new Date()); } } }