用Graphics 画图 修改颜色的问题

this.jPanel1.getGraphics().drawLine(0,0,100,100);this.jPanel1.getGraphics().setColor(... this.jPanel1.getGraphics().drawLine(0,0,100,100);
this.jPanel1.getGraphics().setColor(new Color(255,255,255));
this.jPanel1.getGraphics().drawLine(0,0,100,100);
System.out.println(this.jPanel1.getGraphics().getColor());

为什么都是画黑色的线

为什么还是输出为0 0 0

在JPanel JLabel JButton都是这样

按逻辑来说修改值了就能输出所修改的值呀
怎么没人回答滴?
展开
 我来答
mdzlkj
2008-03-09
知道答主
回答量:1
采纳率:0%
帮助的人:0
展开全部
package untitled3;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* <p> Title: </p>
*
* <p> Description: </p>
*
* <p> Copyright: Copyright (c) 2008 </p>
*
* <p> Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JButton jButton1 = new JButton();

public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jPanel1.setPreferredSize(new Dimension(10, 50));
jButton1.setText("jButton1");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
contentPane.add(jPanel1, java.awt.BorderLayout.SOUTH);
jPanel1.add(jButton1);
contentPane.add(jPanel2, java.awt.BorderLayout.CENTER);
}

public void setColorAndDrawGraphics(Graphics g) {
g.setColor(Color.WHITE); //可以改变颜色,因为是引用传参
g.drawLine(0, 0, 100, 100); //颜色有改变
}

public void jButton1_actionPerformed(ActionEvent e) {
setColorAndDrawGraphics(jPanel2.getGraphics());
// jPanel2.getGraphics().setColor(Color.WHITE);//改变了也没改变(因为是副本)
jPanel2.getGraphics().drawLine(100, 100, 200, 200); //颜色无改变
}
}

class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}

就这原因:) "自答"

参考资料: http://topic.csdn.net/u/20080301/15/5f5d6ffd-f881-46cf-9b9e-35727542738f.html?1266013761

本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式