Topic: 我的一个连线程序为何通过了编译却无发运行? |
Print this page |
1.我的一个连线程序为何通过了编译却无发运行? | Copy to clipboard |
Posted by: jackjoe Posted on: 2004-09-25 03:29 程序是这样的:先在frame上随便画6个点,分别编号1,2,3,4,5,6.做出图形界面:包括6个数字按钮和一个用于输入权值的textfield和一个connect按钮用于输出结果.比如我按1和3的按钮,并输入权值5再按connect此时图上1和3连线,并显示权值5.以下是我的代码: import java.awt.*; import java.awt.event.*; import java.applet.*; public class Line { public static void main(String[] args) { Picture hhh=new Picture();hhh.jjj(); } } class Picture extends Applet implements ActionListener { int m,i; int x2,y2,x1,y1; int type1,type2,type3; int x[]={200,100,300,400,200,85}; int y[]={200,150,200,100,75,70}; String s; Frame f; Panel p=new Panel(); Button b7; TextField b8; Button []button; int j=0; Picture() { Frame f=new Frame(); b8=new TextField(); b7=new Button("connect"); for(i=1;i<=6;i++) button[i]=new Button(""+i); p.setLayout(new GridLayout(4,2)); for(i=1;i<=6;i++) p.add(button[i]); p.add(b7); p.add(b8); f.setLayout(new BorderLayout()); f.add(p,BorderLayout.SOUTH); for(i=1;i<=6;i++) button[i].addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); f.setSize(500,400); f.setVisible(true); f.addWindowListener(new Monitor1); f.show(); } public void jjj(){f.getGraphics().drawString("1",x[0],y[0]);f.getGraphics().drawString("2",x[1],y[1]);f.getGraphics().drawString("3",x[2],y[2]);f.getGraphics().drawString("4",x[3],y[3]);f.getGraphics().drawString("5",x[4],y[4]);f.getGraphics().drawString("6",x[5],y[5]);} public void actionPerformed(ActionEvent e){ for(i=1;i<=6;i++){ if(e.getSource()==button[i]&j==0){ x2=x[i-1]; y2=y[i-1]; type1=1;j++; } if(e.getSource()==button[i]&j==1){ x1=x[i-1]; y1=y[i-1]; type2=2; } } if(b8.getText()!=""){ s=b8.getText(); type3=3; } if(e.getSource()==b7&type1==1&type2==2&type3==3){ f.getGraphics().drawString(s,(x1+x2)/2,(y1+y2)/2);f.getGraphics().drawLine(x1,y1,x2,y2); j=0; } } } class Monitor1 extends WindowAdapter { Monitor1(Frame f) { this.f=f; } private Frame f; public void windowClosing(WindowEvent e) { System.exit(0); } } 为何能通过编译却无法运行,我学JAVA没多久编这个东西感觉很多东西不懂,能不能劳烦给我一个清楚的代码看看,谢谢! |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |