直到鼠标直到鼠标不可见的按钮

发布于 2025-01-25 12:23:30 字数 2172 浏览 2 评论 0原文

我创建了一个帧和面板(用于Graphics),然后在面板中添加按钮。但是,当我运行程序时,直到我悬停在它们上时,才能看到按钮。图形方法也许有问题。

问题如何解决?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main{
    public static void main(String[] args) {
        
        JFrame f = new JFrame();
        f.setSize(600,500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        
        Panel panel = new Panel();
        
        f.add(panel);
        f.setVisible(true);

    }
}

class Panel extends JPanel implements ActionListener{
    int[] x = {200,400,300,200};
    int[] y = {100,100,200,100};
    JButton fillRed;
    JButton fillBlack;
    JButton cancel;
    Panel(){
         
        this.setLayout(null);
        fillRed = new JButton("Red");
        fillRed.setBounds(50, 400, 100, 50);
        fillRed.addActionListener(this);
        this.add(fillRed);
         
        fillBlack = new JButton("Black");
        fillBlack.setBounds(250, 400, 100, 50);
        fillBlack.addActionListener(this);
        this.add(fillBlack);
         
        cancel = new JButton("Cancel");
        cancel.setBounds(450,400,100,50);
        cancel.addActionListener(this);
        this.add(cancel);
         
    }
     
    public void paint(Graphics g) {
        super.paintComponent(g);
        g.drawPolygon(x,y,4);
        
    }
    public void fillRed(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillPolygon(x,y,4);
        
    }
    public void fillBlack(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.fillPolygon(x,y,4);
        
    }
    public void cancel(Graphics g) {    
        super.paintComponent(g);
        g.drawPolygon(x,y,4);
        repaint();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        if(e.getSource()==fillRed) {
             fillRed(getGraphics());
        }
        
        if(e.getSource()==fillBlack) {
             fillBlack(getGraphics());
        }
        
        if(e.getSource()==cancel) {
             cancel(getGraphics());
        }
    }
}

I created a frame and panel (for Graphics) and add buttons to the panel. However, when I run my program buttons are not visible until I hover over them. Maybe it is something wrong with graphics methods.

How the problem can be solved?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main{
    public static void main(String[] args) {
        
        JFrame f = new JFrame();
        f.setSize(600,500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        
        Panel panel = new Panel();
        
        f.add(panel);
        f.setVisible(true);

    }
}

class Panel extends JPanel implements ActionListener{
    int[] x = {200,400,300,200};
    int[] y = {100,100,200,100};
    JButton fillRed;
    JButton fillBlack;
    JButton cancel;
    Panel(){
         
        this.setLayout(null);
        fillRed = new JButton("Red");
        fillRed.setBounds(50, 400, 100, 50);
        fillRed.addActionListener(this);
        this.add(fillRed);
         
        fillBlack = new JButton("Black");
        fillBlack.setBounds(250, 400, 100, 50);
        fillBlack.addActionListener(this);
        this.add(fillBlack);
         
        cancel = new JButton("Cancel");
        cancel.setBounds(450,400,100,50);
        cancel.addActionListener(this);
        this.add(cancel);
         
    }
     
    public void paint(Graphics g) {
        super.paintComponent(g);
        g.drawPolygon(x,y,4);
        
    }
    public void fillRed(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillPolygon(x,y,4);
        
    }
    public void fillBlack(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.fillPolygon(x,y,4);
        
    }
    public void cancel(Graphics g) {    
        super.paintComponent(g);
        g.drawPolygon(x,y,4);
        repaint();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        if(e.getSource()==fillRed) {
             fillRed(getGraphics());
        }
        
        if(e.getSource()==fillBlack) {
             fillBlack(getGraphics());
        }
        
        if(e.getSource()==cancel) {
             cancel(getGraphics());
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神仙妹妹 2025-02-01 12:23:30

您的绘画代码主要是错误的。例如:

public void paint(Graphics g) {
    super.paintComponent(g);
    g.drawPolygon(x,y,4);
    
}

如果您需要覆盖油漆(),则第一个语句应为super.paint(g)。

但是,您不应覆盖油漆。对于自定义绘画,您覆盖paintcomponent(...),然后调用super.paintcomponent(g)。在

public void fillBlack(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.fillPolygon(x,y,4);
}

永远不要直接调用PaintComponent(...)。如果您需要更改组件的属性,则您调用Repaver(),而Swing将调用绘画方法。

    if(e.getSource()==fillRed) {
         fillRed(getGraphics());
    }

不要在秋千组件上调用GetGraphics()。绘画是通过设置类的属性来完成的,然后您调用Repaver()。如果您需要绘制多个对象,则需要跟踪要绘制的每个对象,然后在绘画方法中绘制每个对象。请查看自定义绘画方法完成。

Your painting code is mostly wrong. For example:

public void paint(Graphics g) {
    super.paintComponent(g);
    g.drawPolygon(x,y,4);
    
}

If you need to override paint() then the first statement should be super.paint(g).

However, you should NOT override paint. For custom painting you override paintComponent(...) and then invoke super.paintComponent(g). Read the Swing tutorial on Custom Painting for more information and working examples.

public void fillBlack(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.fillPolygon(x,y,4);
}

Never invoke paintComponent(...) directly. If you need to change a property of a component then you invoke repaint() and Swing will invoke the painting methods.

    if(e.getSource()==fillRed) {
         fillRed(getGraphics());
    }

Don't invoke getGraphics() on a Swing component. Painting is done by setting properties of your class and then you invoke repaint(). If you need to paint multiple objects then you need to keep track of each object you want to paint and then in the painting method you paint every object. Check out Custom Painting Approaches for example of how this can be done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文