为什么我不能在面板上绘画
Why can't I paint on the panel
我做了南、东、北三个面板,我想在北面板上画一个圆圈,但我就是想不通为什么画不出来。这是我的代码:我想要的是一个小应用程序,可以绘制用户选择的不同大小和颜色的圆圈。
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();
}
}
没有内容的面板的默认大小为 0x0,因此您看到这种面板的唯一方式是布局在高度和宽度上拉伸它。最好将自定义绘制面板的 getPreferredSize()
方法覆盖为 return 合理的大小,然后 pack()
顶级容器。
您在构造函数 drawing() 中创建了另一个 JFrame 实例
- 删除该实例并替换为构造函数中所有初始化的实例。
- 添加super.paint(g);到 public void paint(Graphics g) 方法的第一行。
我做了南、东、北三个面板,我想在北面板上画一个圆圈,但我就是想不通为什么画不出来。这是我的代码:我想要的是一个小应用程序,可以绘制用户选择的不同大小和颜色的圆圈。
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();
}
}
没有内容的面板的默认大小为 0x0,因此您看到这种面板的唯一方式是布局在高度和宽度上拉伸它。最好将自定义绘制面板的 getPreferredSize()
方法覆盖为 return 合理的大小,然后 pack()
顶级容器。
您在构造函数 drawing() 中创建了另一个 JFrame 实例
- 删除该实例并替换为构造函数中所有初始化的实例。
- 添加super.paint(g);到 public void paint(Graphics g) 方法的第一行。