新手
新手求助!
题目要求 "用鼠标按下时,面板为红色,放开时为白色 "下面是我编的程序,可是我觉得没有错误啊,就是不行,请各位前辈帮小弟解决一下啊,不胜感激!
package 使用鼠标设置背景色;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GroundPanel extends JPanel implements MouseListener{
boolean f1=false;
boolean f2 =false;
public GroundPanel(){
addMouseListener(this);
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
f2=true;
}
public void mousePressed(MouseEvent e){
f1=true;
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(true==f1)
{
System.out.println( "f1 is true ");
g.setColor(Color.black);
f1=false;
}
if(true==f2)
{
System.out.println( "f2 is true ");
g.setColor(Color.red);
f2=false;
}
}
}
------解决方案--------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShowColor
{
JFrame f;
JPanel center;
public ShowColor()
{
f = new JFrame();
Container c = f.getContentPane();
center = new JPanel();
c.add(center, "Center ");
f.setSize(300,300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
center.setBackground(Color.red);
}
public void mouseReleased(MouseEvent me)
{
center.setBackground(Color.white);
}
});
}
public static void main(String args[])
{
new ShowColor();
}
}
------解决方案--------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShowColor
{
JFrame f;
JPanelColor center;
public ShowColor()
{
f = new JFrame();
Container c = f.getContentPane();
center = new JPanelColor();
c.add(center, "Center ");
f.setSize(300,300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
new ShowColor();
}
}
class JPanelColor extends JPanel
{
public JPanelColor()
{
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
题目要求 "用鼠标按下时,面板为红色,放开时为白色 "下面是我编的程序,可是我觉得没有错误啊,就是不行,请各位前辈帮小弟解决一下啊,不胜感激!
package 使用鼠标设置背景色;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GroundPanel extends JPanel implements MouseListener{
boolean f1=false;
boolean f2 =false;
public GroundPanel(){
addMouseListener(this);
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
f2=true;
}
public void mousePressed(MouseEvent e){
f1=true;
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(true==f1)
{
System.out.println( "f1 is true ");
g.setColor(Color.black);
f1=false;
}
if(true==f2)
{
System.out.println( "f2 is true ");
g.setColor(Color.red);
f2=false;
}
}
}
------解决方案--------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShowColor
{
JFrame f;
JPanel center;
public ShowColor()
{
f = new JFrame();
Container c = f.getContentPane();
center = new JPanel();
c.add(center, "Center ");
f.setSize(300,300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
center.setBackground(Color.red);
}
public void mouseReleased(MouseEvent me)
{
center.setBackground(Color.white);
}
});
}
public static void main(String args[])
{
new ShowColor();
}
}
------解决方案--------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShowColor
{
JFrame f;
JPanelColor center;
public ShowColor()
{
f = new JFrame();
Container c = f.getContentPane();
center = new JPanelColor();
c.add(center, "Center ");
f.setSize(300,300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
new ShowColor();
}
}
class JPanelColor extends JPanel
{
public JPanelColor()
{
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)