JFrame背景色问题
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassDragEventexten...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DragEvent extends JFrame
{
private int x=-1,y=-1;
public DragEvent()
{
super("事件测试");
Container c=getContentPane();
c.setBackground(Color.red);
c.setLayout(new FlowLayout());
c.add(new Label("拖拽鼠标"),BorderLayout.SOUTH);
addMouseMotionListener(new DragListener());
setSize(400,200);
show();
}
class DragListener extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
x=e.getX();
y=e.getY();
repaint();
}
}
public void paint(Graphics g)
{
g.fillOval(x,y,3,3);
}
public static void main(String args[])
{
DragEvent app=new DragEvent();
}
}
为什么背景色没有设置成红色,反而 拖拽鼠标 变成红色了。该怎么改 展开
import javax.swing.*;
import java.awt.event.*;
public class DragEvent extends JFrame
{
private int x=-1,y=-1;
public DragEvent()
{
super("事件测试");
Container c=getContentPane();
c.setBackground(Color.red);
c.setLayout(new FlowLayout());
c.add(new Label("拖拽鼠标"),BorderLayout.SOUTH);
addMouseMotionListener(new DragListener());
setSize(400,200);
show();
}
class DragListener extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
x=e.getX();
y=e.getY();
repaint();
}
}
public void paint(Graphics g)
{
g.fillOval(x,y,3,3);
}
public static void main(String args[])
{
DragEvent app=new DragEvent();
}
}
为什么背景色没有设置成红色,反而 拖拽鼠标 变成红色了。该怎么改 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询