import java.awt.*;
import javax.swing.*;interface MyShape{
void draw(Graphics g);
    void setX1();
    void setX2();
    void setY1();
    void setY2();
    int getX1();
    int getX2();
    int getY1();
    int getY2();
}
public class MyLine extends JFrame implements MyShape {
    private int x1;
    private int x2;
    private int y1;
    private int y2;
    
    public MyLine(){
super("wwwww");

setSize(300,700);
setVisible(true);
}
 
public void setX1(){
x1=12;
}
public void setX2(){
x2=30;
}
public void setY1(){
y1=20;
}
public void setY2(){
y2=40;
}
public int getX1(){
return x1;
}
public int getX2(){
return x2;
}
public int getY1(){
return y1;
}
public int getY2(){
return y2;
}
public void draw(Graphics g){
g.drawLine(x1, y1, x2,y2);
}
public static void main(String[] args){
MyLine a=new MyLine();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
我想在一窗口中画出一条线,但是上面代码不能实现,怎么回事?应该怎样做啊?(要用接口)

解决方案 »

  1.   

    把你的程序改了一下import java.awt.*;
    import javax.swing.*;interface MyShape{  void paint(Graphics g);
      int getX1();
      int getX2();
      int getY1();
      int getY2();
      
    }
    public class Test extends JFrame implements MyShape {
      private int x1;
      private int x2;
      private int y1;
      private int y2;
        
      public Test(int x1,int x2,int y1,int y2){
      super("Test");
      
      this.x1 = x1;
      this.x2 = x2;
      this.y1 = y1;
      this.y2 = y2;  setSize(300,700);
      
      repaint();
      
      setVisible(true);
    }
     
    public int getX1(){
      return x1;
    }
    public int getX2(){
      return x2;
    }
    public int getY1(){
      return y1;
    }
    public int getY2(){
      return y2;
    }
    public void paint(Graphics g){
    g.drawLine(x1,y1, x2, y2);
    }
    public static void main(String[] args){
    Test a=new Test( 50, 80,500,400);a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }}