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都是这样
按逻辑来说修改值了就能输出所修改的值呀
怎么没人回答滴?
用Graphics 画图 修改颜色的问题
答案:1 悬赏:80 手机版
解决时间 2021-03-16 09:26
- 提问者网友:蔚蓝的太阳
- 2021-03-15 19:43
最佳答案
- 五星知识达人网友:何以畏孤独
- 2021-03-15 20:23
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;
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();
}
}
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);
}
}
就这原因:) "自答"
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;
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();
}
}
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);
}
}
就这原因:) "自答"
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯