下面这段代码为何界面的颜色无法修改????

2013-07-07 01:26:58
package event;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;

/**
* 棋子类
* 其中包括棋子的位置Point,以及棋子的颜色控制标记flag
* @author Administrator
*
*/

class Chess
{
Chess()
{
flag = 0;
p = new Point();
}
Point p;
int flag;

}
class MouseEventTest extends JFrame

{
/**
*
*/
private static final long serialVersionUID = 1L;

/**
* 保存鼠标位置
*/
int mouseX = 0;
int mouseY = 0;
Color color;

//表示棋子的个数
int len = 0;
//保存棋子信息
Chess[] chessArr;

MouseEventTest()
{

Container container = this.getContentPane();
container.setBackground(Color.YELLOW);
chessArr = new Chess[200];
this.setTitle("鼠标移动测试");
this.setSize(500,400);
int screenX = this.getToolkit().getScreenSize().width;
int screenY = this.getToolkit().getScreenSize().height;

this.setLocation(screenX/2 -250, screenY/2 -200);
// this.setResizable(false);
this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this.setBackground(Color.red);


container.addMouseListener(new MouseListener(){

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
MouseEventTest.this.mouseX = e.getX();
MouseEventTest.this.mouseY = e.getY();




//保存棋子信息,同时len +1
chessArr[len] = new Chess();

chessArr[len].p.x = e.getX();
chessArr[len].p.y = e.getY();

chessArr[len].flag = e.getButton();
len ++;

// chessArr[]
// repaint();

}

@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

});
}

public void paint(Graphics g)
{
// super.paint(g);
// this.getContentPane().setBackground(Color.yellow);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(color);

for(int i=0;i<len;i++)
{
if(chessArr[i].flag == 1)
g2.setColor(Color.BLUE);
else if(chessArr[i].flag == 2)
g2.setColor(Color.green);
else if(chessArr[i].flag == 3)
g2.setColor(Color.ORANGE);
if(mouseX >10) //因为程序启动后 会有一个默认的左上角的位置 0,0,我们进行操作的时候
//会优先在那个位置进行绘图
g2.drawOval(chessArr[i].p.x, chessArr[i].p.y,
100, 100);
}
// g2.drawOval(mouseX, mouseY, 100, 100);
}


public static void main(String[] args)
{
new MouseEventTest();
}

}



执行之后,界面依然是默认的白色。



而如下代码,则可以顺利的修改成其他的颜色。

package event;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

public class BackGroundTest extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;

BackGroundTest()
{
this.setTitle("这是什么,为什么不能修改颜色");
this.setVisible(true);
this.setSize(new Dimension(444,666));

Container container2 = this.getContentPane();
container2.setBackground(Color.red);

container2.addMouseListener(new MouseListener(){

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
// container.setBackground(Color.red);
System.out.print(e.getButton());
}

@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

});
}

public static void main(String[] args)
{
new BackGroundTest();
}
}




我完全混乱了,找了半天也不知道原因在哪儿,求知道的大大说下,万分感谢。
...全文
62 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
2013-07-07
  • 打赏
  • 举报
回复
我用的编译器是 eclpise ,请问下跟这个有关系么? 中间有段时间,我执行第二个类,但系统总会跑去执行第一个类。 重启了ELCPISE才没有继续这种状况。

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧