当前位置: 技术问答>java相关
各位大虾请帮忙!!AWT的问题,一段程序请读一下!(在线等待)
来源: 互联网 发布时间:2015-09-15
本文导语: 下面一段程序中,我不知道如何在Mousclicked中调用Graphics g 使其相应line的BUTTON划出直线?? 请大虾赐教!! import java.awt.*; import java.awt.event.*; public class line extends Frame{ static TheCanvas cv=new TheCanvas(); static TextField...
下面一段程序中,我不知道如何在Mousclicked中调用Graphics g
使其相应line的BUTTON划出直线??
请大虾赐教!!
import java.awt.*;
import java.awt.event.*;
public class line extends Frame{
static TheCanvas cv=new TheCanvas();
static TextField tf1=new TextField(10);
static TextField tf2=new TextField(10);
static TextField tf3=new TextField(10);
static TextField tf4=new TextField(10);
public static void main(String args[]){
line li=new line("canva");
li.setcalculator();
li.add(li.cv);
li.go();
}
line(String str){
super(str);
}
public void setcalculator()
{
Panel p1=new Panel();
Label first=new Label("opr1");
Label second=new Label("opr2");
Label third=new Label("opr3");
Label fourth=new Label("opr4");
p1.add(first);
tf1.setEditable(true);
tf1.resize(tf1.preferredSize());
p1.add(tf1);
p1.add(second);
tf2.setEditable(true);
tf2.resize(tf2.preferredSize());
p1.add(tf2);
p1.add(third);
tf3.setEditable(true);
tf3.resize(tf3.preferredSize());
p1.add(tf3);
p1.add(fourth);
tf4.setEditable(true);
tf4.resize(tf4.preferredSize());
p1.add(tf4);
Panel p2=new Panel();
Button drawline1=new Button("line");
drawline1.addMouseListener(new me1());
p2.add(drawline1);
add("South",p2);
add("North",p1);
}
public void go(){
TheAdapterTest tat=new TheAdapterTest();
addWindowListener(tat);
setSize(600,400);
setVisible(true);
}
}
class TheAdapterTest extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(1);
}
}
class TheCanvas extends Canvas
{
public int m=0;
public int n=0;
public int i=50;
public int j=50;
public void paint(Graphics g){
super.paint(g);
draw(g);
}
public void draw(Graphics g){
g.setColor(Color.pink);
g.drawLine(i,j,m,n);
}
}
class me1 implements MouseListener{
public void mouseClicked(MouseEvent e1){
Graphics g;
String str1=line.tf1.getText();
String str2=line.tf2.getText();
String str3=line.tf3.getText();
String str4=line.tf4.getText();
i=Integer.parseInt(str1);
j=Integer.parseInt(str2);
m=Integer.parseInt(str3);
n=Integer.parseInt(str4);
g.drawLine(i,j,m,n);
//line.cv.draw(g.drawLine(i,j,m,n));
}
public void mousePressed(MouseEvent e1){}
public void mouseReleased(MouseEvent e1){}
public void mouseEntered(MouseEvent e1){}
public void mouseExited(MouseEvent e1){}
}
使其相应line的BUTTON划出直线??
请大虾赐教!!
import java.awt.*;
import java.awt.event.*;
public class line extends Frame{
static TheCanvas cv=new TheCanvas();
static TextField tf1=new TextField(10);
static TextField tf2=new TextField(10);
static TextField tf3=new TextField(10);
static TextField tf4=new TextField(10);
public static void main(String args[]){
line li=new line("canva");
li.setcalculator();
li.add(li.cv);
li.go();
}
line(String str){
super(str);
}
public void setcalculator()
{
Panel p1=new Panel();
Label first=new Label("opr1");
Label second=new Label("opr2");
Label third=new Label("opr3");
Label fourth=new Label("opr4");
p1.add(first);
tf1.setEditable(true);
tf1.resize(tf1.preferredSize());
p1.add(tf1);
p1.add(second);
tf2.setEditable(true);
tf2.resize(tf2.preferredSize());
p1.add(tf2);
p1.add(third);
tf3.setEditable(true);
tf3.resize(tf3.preferredSize());
p1.add(tf3);
p1.add(fourth);
tf4.setEditable(true);
tf4.resize(tf4.preferredSize());
p1.add(tf4);
Panel p2=new Panel();
Button drawline1=new Button("line");
drawline1.addMouseListener(new me1());
p2.add(drawline1);
add("South",p2);
add("North",p1);
}
public void go(){
TheAdapterTest tat=new TheAdapterTest();
addWindowListener(tat);
setSize(600,400);
setVisible(true);
}
}
class TheAdapterTest extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(1);
}
}
class TheCanvas extends Canvas
{
public int m=0;
public int n=0;
public int i=50;
public int j=50;
public void paint(Graphics g){
super.paint(g);
draw(g);
}
public void draw(Graphics g){
g.setColor(Color.pink);
g.drawLine(i,j,m,n);
}
}
class me1 implements MouseListener{
public void mouseClicked(MouseEvent e1){
Graphics g;
String str1=line.tf1.getText();
String str2=line.tf2.getText();
String str3=line.tf3.getText();
String str4=line.tf4.getText();
i=Integer.parseInt(str1);
j=Integer.parseInt(str2);
m=Integer.parseInt(str3);
n=Integer.parseInt(str4);
g.drawLine(i,j,m,n);
//line.cv.draw(g.drawLine(i,j,m,n));
}
public void mousePressed(MouseEvent e1){}
public void mouseReleased(MouseEvent e1){}
public void mouseEntered(MouseEvent e1){}
public void mouseExited(MouseEvent e1){}
}
|
import java.awt.*;
import java.awt.event.*;
public class line extends Frame{
static TheCanvas cv;
static TextField tf1=new TextField(10);
static TextField tf2=new TextField(10);
static TextField tf3=new TextField(10);
static TextField tf4=new TextField(10);
static Panel p3=new Panel();
public static void main(String args[]){
line li=new line("canva");
li.setcalculator();
//li.add(BorderLayout.CENTER,li.cv=new TheCanvas(p3));
li.go();
}
line(String str){
super(str);
}
public void setcalculator()
{
Panel p1=new Panel();
Label first=new Label("opr1");
Label second=new Label("opr2");
Label third=new Label("opr3");
Label fourth=new Label("opr4");
p1.add(first);
tf1.setEditable(true);
tf1.resize(tf1.preferredSize());
p1.add(tf1);
p1.add(second);
tf2.setEditable(true);
tf2.resize(tf2.preferredSize());
p1.add(tf2);
p1.add(third);
tf3.setEditable(true);
tf3.resize(tf3.preferredSize());
p1.add(tf3);
p1.add(fourth);
tf4.setEditable(true);
tf4.resize(tf4.preferredSize());
p1.add(tf4);
Panel p2=new Panel();
Button drawline1=new Button("line");
drawline1.addMouseListener(new TheCanvas(p3));
p2.add(drawline1);
//Panel p3=new Panel();
//
add("Center",p3);
add("South",p2);
add("North",p1);
}
public void go(){
TheAdapterTest tat=new TheAdapterTest();
addWindowListener(tat);
setSize(600,400);
show();
}
}
class TheAdapterTest extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(1);
}
}
class TheCanvas extends Canvas implements MouseListener
{
int m;
int n;
int i;
int j;
Panel panel;
public TheCanvas(Panel panel) {
this.panel = panel;
}
public void mouseClicked(MouseEvent e1){
String str1=line.tf1.getText();
String str2=line.tf2.getText();
String str3=line.tf3.getText();
String str4=line.tf4.getText();
i=Integer.parseInt(str1);
j=Integer.parseInt(str2);
m=Integer.parseInt(str3);
n=Integer.parseInt(str4);
Graphics g=panel.getGraphics();
g.drawLine(i,j,m,n);
//Graphics g=((Component)e1.getSource()).getGraphics();
//line.cv.getGraphics() g.drawLine(i,j,m,n);
}
public void mousePressed(MouseEvent e1){}
public void mouseReleased(MouseEvent e1){}
public void mouseEntered(MouseEvent e1){}
public void mouseExited(MouseEvent e1){}
//public void paint(Graphics g){
// super.paint(g);
// draw(g);
// }
// public void draw(Graphics g){
// g.setColor(Color.pink);
// g.drawLine(i,j,m,n);
// }
}
import java.awt.event.*;
public class line extends Frame{
static TheCanvas cv;
static TextField tf1=new TextField(10);
static TextField tf2=new TextField(10);
static TextField tf3=new TextField(10);
static TextField tf4=new TextField(10);
static Panel p3=new Panel();
public static void main(String args[]){
line li=new line("canva");
li.setcalculator();
//li.add(BorderLayout.CENTER,li.cv=new TheCanvas(p3));
li.go();
}
line(String str){
super(str);
}
public void setcalculator()
{
Panel p1=new Panel();
Label first=new Label("opr1");
Label second=new Label("opr2");
Label third=new Label("opr3");
Label fourth=new Label("opr4");
p1.add(first);
tf1.setEditable(true);
tf1.resize(tf1.preferredSize());
p1.add(tf1);
p1.add(second);
tf2.setEditable(true);
tf2.resize(tf2.preferredSize());
p1.add(tf2);
p1.add(third);
tf3.setEditable(true);
tf3.resize(tf3.preferredSize());
p1.add(tf3);
p1.add(fourth);
tf4.setEditable(true);
tf4.resize(tf4.preferredSize());
p1.add(tf4);
Panel p2=new Panel();
Button drawline1=new Button("line");
drawline1.addMouseListener(new TheCanvas(p3));
p2.add(drawline1);
//Panel p3=new Panel();
//
add("Center",p3);
add("South",p2);
add("North",p1);
}
public void go(){
TheAdapterTest tat=new TheAdapterTest();
addWindowListener(tat);
setSize(600,400);
show();
}
}
class TheAdapterTest extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(1);
}
}
class TheCanvas extends Canvas implements MouseListener
{
int m;
int n;
int i;
int j;
Panel panel;
public TheCanvas(Panel panel) {
this.panel = panel;
}
public void mouseClicked(MouseEvent e1){
String str1=line.tf1.getText();
String str2=line.tf2.getText();
String str3=line.tf3.getText();
String str4=line.tf4.getText();
i=Integer.parseInt(str1);
j=Integer.parseInt(str2);
m=Integer.parseInt(str3);
n=Integer.parseInt(str4);
Graphics g=panel.getGraphics();
g.drawLine(i,j,m,n);
//Graphics g=((Component)e1.getSource()).getGraphics();
//line.cv.getGraphics() g.drawLine(i,j,m,n);
}
public void mousePressed(MouseEvent e1){}
public void mouseReleased(MouseEvent e1){}
public void mouseEntered(MouseEvent e1){}
public void mouseExited(MouseEvent e1){}
//public void paint(Graphics g){
// super.paint(g);
// draw(g);
// }
// public void draw(Graphics g){
// g.setColor(Color.pink);
// g.drawLine(i,j,m,n);
// }
}
|
public void mousePressed(MouseEvent e){
i=e.getX();
j=e.getY();
}
public void mouseDragged(MouseEvent e){
m=e.getX();
n=e.getY();
}
repaint();
i=e.getX();
j=e.getY();
}
public void mouseDragged(MouseEvent e){
m=e.getX();
n=e.getY();
}
repaint();