简体   繁体   English

为什么我不能在面板上绘画

[英]Why can't I paint on the panel

I have made three panels south, east and north, I am trying to draw a circle on the north panel but I just can't figure out why it is not drawing. 我已经制作了三个面板,分别是南,东和北,我试图在北面板上画一个圆,但我不知道为什么没有画。 Here is my code: What I want is a small application that draws circles of different sizes and colors chosen by the user. 这是我的代码:我想要的是一个小型应用程序,它可以绘制用户选择的不同大小和颜色的圆圈。

import com.sun.prism.shader.DrawCircle_Color_Loader;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class drawing extends JFrame implements MouseListener{

   private int r = 255, g = 0, b = 0;
   private JSlider colorSlider, redSlider,greenSlider,blueSlider;
   private JLabel colorLabel,redLabel,greenLabel,blueLabel;
   private int x = 50;
   private int y = 50;


public drawing(){
    JFrame frame = new JFrame("Drawing App");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setSize(800,800);

    Container contentPane = frame.getContentPane();


    JMenuBar mb = new JMenuBar();
    frame.setJMenuBar(mb);

    JMenu color = new JMenu("Colour");
    JMenu size = new JMenu("Size");

    mb.add(color);
    mb.add(size);
    JMenuItem colorRed = new JMenuItem("Red");
    JMenuItem colorBlue = new JMenuItem("Blue");
    JMenuItem colorGreen = new JMenuItem("Green");
    color.add(colorBlue);
    color.add(colorGreen);
    color.add(colorRed);

    JMenuItem one = new JMenuItem("1");
    JMenuItem two = new JMenuItem("2");
    JMenuItem three = new JMenuItem("3");
    JMenuItem four = new JMenuItem("4");
    JMenuItem five = new JMenuItem("5");
    size.add(one);
    size.add(two);
    size.add(three);
    size.add(four);

    JPanel panel = new JPanel();
    setBackground(Color.WHITE);
    contentPane.add(panel,BorderLayout.NORTH);

    JPanel panel1 = new JPanel();
    contentPane.add(panel1,BorderLayout.SOUTH);

    JPanel panel2 = new JPanel();
    contentPane.add(panel2,BorderLayout.EAST);

    panel1.setLayout(new GridLayout(0,1));

   JColorChooser colors = new JColorChooser();
    panel1.add(colors);

   frame.setVisible(true);

    panel.add(panel);
    }

  public void paint(Graphics g)
   {
     g.setColor(Color.red);
     g.fillOval(x,y,100,100);
   }
  public void mousePressed(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}

  public void mouseClicked(MouseEvent e) {
     x = e.getX();
     y = e.getY();
     repaint();

  }
  public static void main(String[] args) {

    drawing Drawing = new drawing();

 }

} }

A panel with no content has a default size of 0x0, so the only way you might see such a panel is if the layout stretches it in both height and width. 没有内容的面板的默认大小为0x0,因此,您看到这种面板的唯一方法是布局在高度和宽度上都将其拉伸。 It is best to override the getPreferredSize() method of a custom painted panel to return a sensible size, then pack() the top level container. 最好重写自定义绘制面板的getPreferredSize()方法以返回合理的大小,然后将顶层容器pack()

You created another instance of JFrame inside the constructor drawing() 您在构造函数drawing()中创建了另一个JFrame实例。

  1. Remove that instance and replace with this for all the initialization inside the constructor. 删除该实例,并用该实例替换构造函数中的所有初始化。
  2. add super.paint(g); 添加super.paint(g); to the first line of your public void paint(Graphics g) method. 到您的无效绘画(Graphics g)方法的第一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM