主要问题是
addActionListener只是加了一个监听器,有事件时才执行,而不是顺序执行改动后代码如下:(///为改动的地方)import java.awt.*;
import java.awt.event.*;
import java.util.*;import javax.swing.*;public class DrawLine extends JFrame {
private Draw dr;
private Color choose;
private JButton b;
public DrawLine()
{
b = new JButton("55");
b.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
choose = JColorChooser.showDialog(DrawLine.this,"请选择一种颜色",choose);
dr.setColor(choose);///
System.out.println(choose);
}
}
);
dr = new Draw( choose );
dr.setBackground(Color.white);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(dr);
c.add(b);
validate();
setSize(300,300);
show();
} public static void main(String args[])
{
DrawLine dl = new DrawLine();
dl.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}class Line {
public int x0,y0,x1,y1;
}class Draw extends JPanel{
Vector lines = new Vector();
Line cur;
private Color color1;
public Draw( Color c )
{
color1 = c;
setPreferredSize(new Dimension(150,100));
addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(cur == null)
cur = new Line();
cur.x0 = e.getX();
cur.y0 = e.getY();
}
public void mouseReleased(MouseEvent e) {
cur.x1 = e.getX();
cur.y1 = e.getY();
lines.add(cur);
cur = null;
}
}); addMouseMotionListener( new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
cur.x1 = e.getX();
cur.y1 = e.getY();
repaint();
}
});
}
//设置线的颜色
public void setColor(Color lineColor){///
color1 = lineColor;///
}///
public void paint(Graphics g) {
super.paint(g);
g.setColor(color1);////
for(int i = 0;i<lines.size();i++){
Line tmp = (Line)lines.elementAt(i);
g.drawLine(tmp.x0, tmp.y0, tmp.x1, tmp.y1);
}
if(cur != null )
g.drawLine(cur.x0,cur.y0,cur.x1,cur.y1);
}}
—————————————————————————————————Just Wish you have a nice day !
addActionListener只是加了一个监听器,有事件时才执行,而不是顺序执行改动后代码如下:(///为改动的地方)import java.awt.*;
import java.awt.event.*;
import java.util.*;import javax.swing.*;public class DrawLine extends JFrame {
private Draw dr;
private Color choose;
private JButton b;
public DrawLine()
{
b = new JButton("55");
b.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
choose = JColorChooser.showDialog(DrawLine.this,"请选择一种颜色",choose);
dr.setColor(choose);///
System.out.println(choose);
}
}
);
dr = new Draw( choose );
dr.setBackground(Color.white);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(dr);
c.add(b);
validate();
setSize(300,300);
show();
} public static void main(String args[])
{
DrawLine dl = new DrawLine();
dl.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}class Line {
public int x0,y0,x1,y1;
}class Draw extends JPanel{
Vector lines = new Vector();
Line cur;
private Color color1;
public Draw( Color c )
{
color1 = c;
setPreferredSize(new Dimension(150,100));
addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(cur == null)
cur = new Line();
cur.x0 = e.getX();
cur.y0 = e.getY();
}
public void mouseReleased(MouseEvent e) {
cur.x1 = e.getX();
cur.y1 = e.getY();
lines.add(cur);
cur = null;
}
}); addMouseMotionListener( new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
cur.x1 = e.getX();
cur.y1 = e.getY();
repaint();
}
});
}
//设置线的颜色
public void setColor(Color lineColor){///
color1 = lineColor;///
}///
public void paint(Graphics g) {
super.paint(g);
g.setColor(color1);////
for(int i = 0;i<lines.size();i++){
Line tmp = (Line)lines.elementAt(i);
g.drawLine(tmp.x0, tmp.y0, tmp.x1, tmp.y1);
}
if(cur != null )
g.drawLine(cur.x0,cur.y0,cur.x1,cur.y1);
}}
—————————————————————————————————Just Wish you have a nice day !
问题是解决了
但是一次只能画一种颜色.
我想画几种颜色的直线.
该怎么办;