簡體   English   中英

直到鼠標懸停在 java 中,按鈕才會顯示

[英]Buttons are not displaying until mouse hover on it in java

直到或除非我將鼠標懸停在按鈕上,否則不會顯示 JButton。

package paint;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Rehan Shakir
 */

public class PaintFrame extends JFrame implements MouseMotionListener,ActionListener
{
  private int x=0; 
  private int y=0;
  private final JButton red,yellow,blue,green;
 private final JPanel p;
  //private final  JLabel l;
  private final  Container c; 
  private Color color;
  public PaintFrame()
  {

     setTitle("Paint");
     setSize(800,600);
     setDefaultCloseOperation(EXIT_ON_CLOSE);
     //setLayout(new FlowLayout());



     p = new JPanel();
     p.setLayout(new GridLayout(4,1));



     red = new JButton("Red");
     red.setBackground(Color.red);
     p.add(red);

     yellow = new JButton("Yellow");
     yellow.setBackground(Color.yellow);
     p.add(yellow);



     blue = new JButton("Blue");
     blue.setBackground(Color.blue);
     p.add(blue);

     green = new JButton("Green");
     green.setBackground(Color.green);
     p.add(green);

     red.addActionListener(this);
     yellow.addActionListener(this);
     blue.addActionListener(this);
     green.addActionListener(this);


     c = this.getContentPane();
     c.setLayout(new BorderLayout());
     JLabel l = new JLabel("Drag the mouse to draw",JLabel.RIGHT);
     c.add(l,BorderLayout.SOUTH);
     c.add(p,BorderLayout.WEST);

     c.addMouseMotionListener(this);
     setVisible(true);     

  }


    @Override
      public void mouseDragged(MouseEvent e )
      {
         x= e.getX();
         y= e.getY();
         repaint();

      }
  @Override
      public void mouseMoved(MouseEvent e)
      {

      }
      @Override
      public void actionPerformed(ActionEvent e)
      {
          String s = e.getActionCommand();
          if(s.equals("Red"))
              color = Color.red;
          else if(s.equals("Yellow"))
              color = Color.yellow;
          else if(s.equals("Blue"))
              color = Color.blue;
          else if(s.equals("Green"))
              color = Color.green;
          else
              color = Color.BLACK;

      }



  @Override
  public void paint(Graphics g)
  {
     g.setColor(color);
      g.fillOval(x, y, 4, 4);
  }

}

主課

public class Paint {
      package paint;

    public static void main(String[] args) {
        // TODO code application logic here
        PaintFrame Jf = new PaintFrame();

    }

}

當我執行這個程序時,只有第一個 JButton 顯示在屏幕上。 我必須將鼠標懸停在其他按鈕上,以便它們可以顯示在屏幕上。

為什么會這樣?

立即從您的標題中,我可以猜到您沒有在覆蓋中調用 super 的繪畫方法——我是對的:

@Override
public void paint(Graphics g)
{
   // ********** no super call here! *******
   g.setColor(color);
   g.fillOval(x, y, 4, 4);
}

如果不這樣做,您就不允許組件進行重要的內務繪畫,包括子組件的繪畫和“臟”像素的移除

這個:

@Override
public void paint(Graphics g)
{
   super.paint(g);
   g.setColor(color);
   g.fillOval(x, y, 4, 4);
}

可能會解決問題


說到這里,我必須補充:

  • 不要直接在 JFrame 中繪制
  • 在擴展 JPanel 的類的paintComponent方法重寫中進行繪制
  • 在 JPanel 的重寫方法中調用super.paintComponent(g) (出於同樣的原因)。

使用 BufferedImage 的示例:

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

@SuppressWarnings("serial")
public class MainPaintPanel extends JPanel {
    private PaintPanel paintPanel = new PaintPanel();

    public MainPaintPanel() {
        MyMouse myMouse = new MyMouse();
        paintPanel.addMouseListener(myMouse);
        paintPanel.addMouseMotionListener(myMouse);
        JPanel btnPanel = new JPanel(new GridLayout(0, 1, 3, 3));
        addColorButton(btnPanel, Color.RED, "Red");
        addColorButton(btnPanel, Color.YELLOW, "Yellow");
        addColorButton(btnPanel, Color.BLUE, "Blue");
        addColorButton(btnPanel, Color.GREEN, "Green");        

        setLayout(new BorderLayout());
        add(paintPanel, BorderLayout.CENTER);
        add(btnPanel, BorderLayout.LINE_START);
    }

    class MyMouse extends MouseAdapter {
        Point p1 = null;

        private void moveOval(MouseEvent e) {
            if (p1 == null) {
                return;
            }
            Point p2 = e.getPoint();
            paintPanel.addLine(p1, p2);
        }

        @Override
        public void mousePressed(MouseEvent e) {
            p1 = e.getPoint();
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            moveOval(e);
            p1 = e.getPoint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            moveOval(e);
            p1 = null;
        }
    }

    private void addColorButton(JPanel btnPanel, Color color, String name) {
        JButton button = new JButton(new ButtonAction(name, color));
        button.setBackground(color);
        btnPanel.add(button);
    }

    class ButtonAction extends AbstractAction {        
        private Color color;

        public ButtonAction(String name, Color color) {
            super(name);
            this.color = color;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            paintPanel.setOvalColor(color);
        }
    }


    private static void createAndShowGui() {
        MainPaintPanel mainPanel = new MainPaintPanel();

        JFrame frame = new JFrame("Painting GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}
@SuppressWarnings("serial")
class PaintPanel extends JPanel {
    private static final int PREF_W = 800;
    private static final int PREF_H = 600;
    private static final int OVAL_WIDTH = 4;
    private static final Stroke BASIC_STROKE = new BasicStroke(OVAL_WIDTH);
    private BufferedImage img;
    private Color ovalColor;

    public PaintPanel() {
        img = new BufferedImage(PREF_W, PREF_W, BufferedImage.TYPE_INT_ARGB);
    }

    public void addLine(Point p1, Point p2) {
        if (img != null && ovalColor != null) {
            Graphics2D g2 = img.createGraphics();
            g2.setStroke(BASIC_STROKE);
            g2.setColor(ovalColor);
            g2.drawLine(p1.x, p1.y, p2.x, p2.y);
            g2.dispose();
            repaint();
        }
    }

    public void setOvalColor(Color ovalColor) {
        this.ovalColor = ovalColor;
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (img != null) {
            g.drawImage(img, 0, 0, this);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM