【问题标题】:Why does the picture of the JButton reappear at the top?为什么JButton的图片会重新出现在顶部?
【发布时间】:2013-12-25 08:20:48
【问题描述】:

我正在尝试制作简单的项目,当鼠标单击 JPanel 时绘制形状。我有 4 个按钮来选择形状的类型。我还没有为按钮提供任何 ActionListiner(我知道该怎么做)。我的项目在Jpanel的顶部绘制了按钮的图片,不知道怎么解决。

   import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.Random;
    import javax.swing.JPanel;

    public class ShapeStamper extends JPanel {


    private int x = -1000, y = -1000;
    private boolean outside = false;

    public ShapeStamper() {

        addMouseListener(
                // anonymous inner class
                new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent event) {
                outside = false;
                x = event.getX();
                y = event.getY();
                repaint();
                System.out.println(x + " " + y);
            }
        } // end anonymous inner class
                ); // end call to addMouseMotionListener

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                outside = true;
                repaint();
                x = e.getX();
                y = e.getY();
                System.out.println(x + " " + y);
                System.out.println(outside);

            }
        });

    }

    @Override
    public void paintComponent(Graphics g) {
        g.drawOval(x, y, 40, 40);

    }

}



import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class ShapeStamperTest {



    public static void main(String[] args) {
        JFrame frame= new JFrame("Shape Stamper!");

   JPanel container;
     JButton circle = new JButton("Circle");
     JButton square = new JButton("Square");
      JButton rectangle = new JButton("Rectangle");
      JButton oval = new JButton("Oval");


        container = new JPanel(new GridLayout(1, 4));
        container.add(circle);
        container.add(square);
        container.add(rectangle);
        container.add(oval);


      final ShapeStamper shape = new ShapeStamper();
     circle.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Circle");

            }
        });
      frame.setSize(500, 500);
      frame.add(shape, BorderLayout.CENTER);
        frame.add(container, BorderLayout.SOUTH);;
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

【问题讨论】:

    标签: java swing jpanel awt


    【解决方案1】:

    您需要在paintComponent 方法中调用super.paintComponentpaintComponent 也应该有 protected 可见性

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawOval(x, y, 40, 40);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 2022-11-10
      • 2021-12-08
      • 2016-08-15
      相关资源
      最近更新 更多