java 画圆,改变其填充颜色。设置的color 属性为什么不能改变其颜色
package编程练习;importjava.awt.*;importjavax.swing.*;importjava.awt.color.*;importjava.aw...
package 编程练习;
import java.awt.*;
import javax.swing.*;
import java.awt.color.*;
import java.awt.event.*;
public class ColorCircle extends JFrame{
Color color;
JPanel jp1;
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
repaint();
}
public ColorCircle(){
panel pl=new panel();
jp1=new JPanel(new FlowLayout());
final JButton jbt=new JButton("click");
jbt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource()==jbt)
setColor(Color.YELLOW);
System.out.println("jbt is clicked");
}
});
jp1.add(jbt);
add(pl,BorderLayout.CENTER);
add(jp1,BorderLayout.SOUTH);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ColorCircle frame=new ColorCircle();
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class panel extends JPanel{
public void paint(Graphics g){
super.paint(g);
//g.setColor(Color.red);
g.fillOval(10, 10, 30, 30);
g.setColor(Color.green);
g.fillOval(40, 40, 30, 30);
}
} 展开
import java.awt.*;
import javax.swing.*;
import java.awt.color.*;
import java.awt.event.*;
public class ColorCircle extends JFrame{
Color color;
JPanel jp1;
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
repaint();
}
public ColorCircle(){
panel pl=new panel();
jp1=new JPanel(new FlowLayout());
final JButton jbt=new JButton("click");
jbt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource()==jbt)
setColor(Color.YELLOW);
System.out.println("jbt is clicked");
}
});
jp1.add(jbt);
add(pl,BorderLayout.CENTER);
add(jp1,BorderLayout.SOUTH);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ColorCircle frame=new ColorCircle();
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class panel extends JPanel{
public void paint(Graphics g){
super.paint(g);
//g.setColor(Color.red);
g.fillOval(10, 10, 30, 30);
g.setColor(Color.green);
g.fillOval(40, 40, 30, 30);
}
} 展开
2个回答
展开全部
你的paint已经把Color的值定成死的了g.setColor(Color.green)
修改下paint方法就行了 每次点击都会变颜色
public void paint(Graphics g){
super.paint(g);
int red=(int)(Math.random()*255);
int gree=(int)(Math.random()*255);
int blue=(int)(Math.random()*255);
g.setColor(new Color(red,gree,blue));
g.fillOval(40, 40, 30, 30);
修改下paint方法就行了 每次点击都会变颜色
public void paint(Graphics g){
super.paint(g);
int red=(int)(Math.random()*255);
int gree=(int)(Math.random()*255);
int blue=(int)(Math.random()*255);
g.setColor(new Color(red,gree,blue));
g.fillOval(40, 40, 30, 30);
追问
你给的代码的意思是我每次点击所变的颜色是随机的吧!我是想通过监听JButton按钮变成特定的颜色,也就是我setColor里面给出的颜色。
追答
g.setColor(Color.green);
和你写的
public void setColor(Color color) {
this.color = color;
repaint();
}
完全没有任何关系......你在
jbt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource()==jbt)
setColor(Color.YELLOW);
System.out.println("jbt is clicked");
}
里改变的是你自己写的setColor根本就改变g.setColor(Color.green);
你应该把panel改成ColorCircle的内部类,定义一个Color的属性,用这个属性去给panel中setColor赋值,然后通过外部类的setColor改变Color的值就行了
public class ColorCircle extends JFrame{
Color color=Color.blue;
JPanel jp1;
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
repaint();
}
public ColorCircle(){
panel pl=new panel();
jp1=new JPanel(new FlowLayout());
final JButton jbt=new JButton("click");
jbt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource()==jbt)
setColor(Color.YELLOW);
System.out.println("jbt is clicked");
}
});
jp1.add(jbt);
add(pl,BorderLayout.CENTER);
add(jp1,BorderLayout.SOUTH);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ColorCircle frame=new ColorCircle();
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class panel extends JPanel{
public void paint(Graphics g){
super.paint(g);
//g.setColor(Color.red);
g.fillOval(10, 10, 30, 30);
g.setColor(color);
g.fillOval(40, 40, 30, 30);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询