编写一个java程序,显示一个面板的背景色,当按下鼠标键背景色为黑色,释放鼠标时背景色为白色
1个回答
展开全部
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class B {
/**
* @param args
*/
public static void main(String[] args) {
JFrame f = new JFrame();
final JPanel p = new JPanel();
f.add(p, BorderLayout.CENTER);
p.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
p.setBackground(Color.black);
}
public void mouseReleased(MouseEvent e) {
p.setBackground(Color.white);
}
});
f.setSize(200, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class B {
/**
* @param args
*/
public static void main(String[] args) {
JFrame f = new JFrame();
final JPanel p = new JPanel();
f.add(p, BorderLayout.CENTER);
p.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
p.setBackground(Color.black);
}
public void mouseReleased(MouseEvent e) {
p.setBackground(Color.white);
}
});
f.setSize(200, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询