Repaint()函数无法正常工作

时间:2019-05-03 02:58:51

标签: java swing jpanel

我想对名为“ lapphuong”的按钮使用addActionListener()函数,该事件是我单击该按钮的时间。我希望图像出现并运行。所以我像这样使用AffineTransform.getTranslateInstance(),但是重画功能不起作用。我的图像刚刚出现但没有运行。这是一流的:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author Admin
 */
public class VeHinh extends JPanel{
    int i = 0;
    HinhLapPhuong s = new HinhLapPhuong(10);
    @Override
    public void paintComponent(Graphics g){
        AffineTransform at = AffineTransform.getTranslateInstance(154 + i, 396);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(s.Anh, at, null);
        i++;
        System.out.println(i);
        repaint(); // repaint() doesn't work.//
        }

}

第二等:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author Admin
 */
public class AppPanel extends JPanel {
    JButton haichieu,tron,vuong,tamgiac;
    JButton bachieu,cau,lapphuong;
    AppPanel(){
    VeHinh v = new VeHinh();
    setSize(1362 , 610);
    setLayout(null);
    JButton haichieu = new JButton("Hai Chieu");
    JButton bachieu = new JButton("Ba Chieu");
    JButton cau = new JButton("Hinh Cau");
    JButton lapphuong = new JButton("Hinh Lap Phuong");
    add(haichieu);
    haichieu.setBounds(861, 107, 114, 38);
    bachieu.setBounds(181, 107, 114, 38);
    add(bachieu);
    haichieu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
    bachieu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                add(cau);
                cau.setBounds(80,241 , 114, 38);
                add(lapphuong);
                lapphuong.setBounds(329, 241, 114, 38);
            }
        });
    lapphuong.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            v.paintComponent(AppPanel.this.getGraphics());//My Problem is here.
//            repaint();
        }
    });
    }
}

0 个答案:

没有答案