用JAVA写一个程序~
写一个程序来改变背景的颜色,用JFrame,JPanel,在用一个MouseListener来控制颜色,如果鼠标在JFrame中,如果鼠标在左半边,背景显示红色,如果鼠标...
写一个程序来改变背景的颜色,用JFrame,JPanel,在用一个MouseListener来控制颜色,如果鼠标在JFrame中,如果鼠标在左半边,背景显示红色,如果鼠标在右半边,背景显示绿色。
请给出整个详细程序(可运行的)
下面是类似程序:
public class ChangeColor
{ public static void main (String[] args)
{
JFrame frame = new JFrame ("ChangeColor");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new ChangeColorPanel());
frame.pack();
frame.setVisible(true);
}
}
另一个程序类似:
public class ChangeColorPanel extends JPanel
{
。。。。。。
。。。。。。
。。。。。。
private class ChangeColorListener implements MouseListener
{
//------------------------------------------------------------
// Adds the current point to the list of points and redraws
// the panel whenever the mouse button is pressed.
//------------------------------------------------------------
public void mousePressed (MouseEvent event)
{
pointList.add(event.getPoint());
repaint();
}
// Provide empty definitions for unused event methods.
public void mouseClicked (MouseEvent event) {}
public void mouseReleased (MouseEvent event) {}
public void mouseEntered (MouseEvent event) {}
public void mouseExited (MouseEvent event) {}
}
}
请给出所写程序每句后的注解~ 谢谢 展开
请给出整个详细程序(可运行的)
下面是类似程序:
public class ChangeColor
{ public static void main (String[] args)
{
JFrame frame = new JFrame ("ChangeColor");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new ChangeColorPanel());
frame.pack();
frame.setVisible(true);
}
}
另一个程序类似:
public class ChangeColorPanel extends JPanel
{
。。。。。。
。。。。。。
。。。。。。
private class ChangeColorListener implements MouseListener
{
//------------------------------------------------------------
// Adds the current point to the list of points and redraws
// the panel whenever the mouse button is pressed.
//------------------------------------------------------------
public void mousePressed (MouseEvent event)
{
pointList.add(event.getPoint());
repaint();
}
// Provide empty definitions for unused event methods.
public void mouseClicked (MouseEvent event) {}
public void mouseReleased (MouseEvent event) {}
public void mouseEntered (MouseEvent event) {}
public void mouseExited (MouseEvent event) {}
}
}
请给出所写程序每句后的注解~ 谢谢 展开
5个回答
展开全部
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String args[]) {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public Test() {
super();
getContentPane().setLayout(null);
setResizable(false);
setTitle("Test");
setBounds(100, 100, 300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel1 = new JPanel();
panel1.setBounds(0, 0, 147, 267);
panel1.setBackground(Color.WHITE);
getContentPane().add(panel1);
final JPanel panel2 = new JPanel();
panel2.setBounds(147, 0, 147, 267);
panel2.setBackground(Color.WHITE);
panel2.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
panel1.setBackground(Color.GREEN);
panel2.setBackground(Color.GREEN);
}
public void mouseExited(MouseEvent arg0) {
panel1.setBackground(Color.WHITE);
panel2.setBackground(Color.WHITE);
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
panel1.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
panel1.setBackground(Color.RED);
panel2.setBackground(Color.RED);
}
public void mouseExited(MouseEvent arg0) {
panel1.setBackground(Color.WHITE);
panel2.setBackground(Color.WHITE);
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
getContentPane().add(panel2);
}
}
-------------------------------------------------------------------
//这是用两个panel实现的,回复zhangxu1216,如果只用一个panel,可以这样:
-------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame implements MouseMotionListener,MouseListener{
private static final long serialVersionUID = 1L;
JPanel pnl;
public static void main(String args[]) {
try {
new Test();
} catch (Exception e) {
e.printStackTrace();
}
}
public Test() {
super();
setResizable(false);
setTitle("Test");
setBounds(100, 100, 300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pnl = new JPanel();
pnl.addMouseMotionListener(this);
pnl.addMouseListener(this);
getContentPane().add(pnl, BorderLayout.CENTER);
setVisible(true);
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
int x = e.getX();
if (x < pnl.getWidth() / 2) {
pnl.setBackground(Color.RED);
}
if (x > pnl.getWidth() / 2) {
pnl.setBackground(Color.GREEN);
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
pnl.setBackground(Color.WHITE);
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String args[]) {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public Test() {
super();
getContentPane().setLayout(null);
setResizable(false);
setTitle("Test");
setBounds(100, 100, 300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel1 = new JPanel();
panel1.setBounds(0, 0, 147, 267);
panel1.setBackground(Color.WHITE);
getContentPane().add(panel1);
final JPanel panel2 = new JPanel();
panel2.setBounds(147, 0, 147, 267);
panel2.setBackground(Color.WHITE);
panel2.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
panel1.setBackground(Color.GREEN);
panel2.setBackground(Color.GREEN);
}
public void mouseExited(MouseEvent arg0) {
panel1.setBackground(Color.WHITE);
panel2.setBackground(Color.WHITE);
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
panel1.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
panel1.setBackground(Color.RED);
panel2.setBackground(Color.RED);
}
public void mouseExited(MouseEvent arg0) {
panel1.setBackground(Color.WHITE);
panel2.setBackground(Color.WHITE);
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
getContentPane().add(panel2);
}
}
-------------------------------------------------------------------
//这是用两个panel实现的,回复zhangxu1216,如果只用一个panel,可以这样:
-------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame implements MouseMotionListener,MouseListener{
private static final long serialVersionUID = 1L;
JPanel pnl;
public static void main(String args[]) {
try {
new Test();
} catch (Exception e) {
e.printStackTrace();
}
}
public Test() {
super();
setResizable(false);
setTitle("Test");
setBounds(100, 100, 300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pnl = new JPanel();
pnl.addMouseMotionListener(this);
pnl.addMouseListener(this);
getContentPane().add(pnl, BorderLayout.CENTER);
setVisible(true);
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
int x = e.getX();
if (x < pnl.getWidth() / 2) {
pnl.setBackground(Color.RED);
}
if (x > pnl.getWidth() / 2) {
pnl.setBackground(Color.GREEN);
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
pnl.setBackground(Color.WHITE);
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
展开全部
给分我就帮你用netbeans做~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1楼的可以达到要求,还不错,不过用了2个jpanel
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class ColorFrame {
public static void main(String[] args) {
final JFrame frame = new JFrame("ColorChange");
final JPanel pane = new JPanel();
frame.getContentPane().add(pane);
frame.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
pane.setBackground(Color.RED);
}
if (SwingUtilities.isRightMouseButton(e)) {
pane.setBackground(Color.GREEN);
}
}
});
frame.setSize(400,500);
frame.setVisible(true);
}
}
来个最简单的。。
public static void main(String[] args) {
final JFrame frame = new JFrame("ColorChange");
final JPanel pane = new JPanel();
frame.getContentPane().add(pane);
frame.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
pane.setBackground(Color.RED);
}
if (SwingUtilities.isRightMouseButton(e)) {
pane.setBackground(Color.GREEN);
}
}
});
frame.setSize(400,500);
frame.setVisible(true);
}
}
来个最简单的。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询