编写一个程序,显示一个面板的背景色,按下鼠标时背景为绿色,释放鼠标时背景为橘黄 使用鼠标适配器
继承鼠标适配器的MyMouse类有问题,需要定义类,然而定义完说不是抽象类,改为抽象类后p.addMouseListener(newMyMouse());又提示抽象类不能...
继承鼠标适配器的MyMouse类有问题,需要定义类,然而定义完说不是抽象类,改为抽象类后p.addMouseListener(new MyMouse());又提示抽象类不能实例化
package jisuanqi;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Mouse {
public static void main(String[] args) {
JFrame f = new JFrame();
final JPanel p = new JPanel();
f.add(p, BorderLayout.CENTER);
p.addMouseListener(new MyMouse());
class MyMouse extends MouseAdapter{
public void mousePressed(MouseEvent e) {
p.setBackground(Color.green);
}
public void mouseReleased(MouseEvent e) {
p.setBackground(Color.orange);
}
}
f.setSize(200, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} 展开
package jisuanqi;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Mouse {
public static void main(String[] args) {
JFrame f = new JFrame();
final JPanel p = new JPanel();
f.add(p, BorderLayout.CENTER);
p.addMouseListener(new MyMouse());
class MyMouse extends MouseAdapter{
public void mousePressed(MouseEvent e) {
p.setBackground(Color.green);
}
public void mouseReleased(MouseEvent e) {
p.setBackground(Color.orange);
}
}
f.setSize(200, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} 展开
1个回答
2016-06-05
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Mouse {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200, 300);
final JPanel p = new JPanel();
f.add(p, BorderLayout.CENTER);
p.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
p.setBackground(Color.green);
}
public void mouseReleased(MouseEvent e) {
p.setBackground(Color.orange);
}
}
f.setVisible(true);
}
}
import java.awt.event.*;
import javax.swing.*;
public class Mouse {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200, 300);
final JPanel p = new JPanel();
f.add(p, BorderLayout.CENTER);
p.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
p.setBackground(Color.green);
}
public void mouseReleased(MouseEvent e) {
p.setBackground(Color.orange);
}
}
f.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询