初学者java的问题main()
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.util.*;publicc...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class DrawLine1 extends Frame {
public static void main(String[] args)
{
DrawLine1 mainFrame = new DrawLine1();
mainFrame.setSize(800,800);
mainFrame.setTitle("000");
mainFrame.setVisible(true);
}
int orgX;
int orgY;
int endX;
int endY;
Image oimg = null;
Graphics og = null;
Vector vLines = new Vector();
public void paint(Graphics g)
{
g.drawImage(oimg,0,0,this);
}
public DrawLine1() {
// Dimension d = getSize();
Dimension d = getSize();
oimg = this.createImage(d.width,d.height);
og = oimg.getGraphics();
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
DrawLine1.this.windowClosed();
}
}
);
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
orgX = e.getX();
orgY = e.getY();
}
public void mouseReleased(MouseEvent e)
{
endX = e.getX();
endY = e.getY();
Graphics g = getGraphics();
g.setColor(Color.RED);
g.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
g.drawString(orgX + "," + orgY,orgX,orgY);
g.drawString(e.getX() +","+e.getY(),e.getX(),e.getY());
g.drawLine(orgX,orgY,e.getX(),e.getY());
og.setColor(Color.RED);
og.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
og.drawString(orgX + "," + orgY,orgX,orgY);
og.drawString(e.getX() +","+e.getY(),e.getX(),e.getY());
og.drawLine(orgX,orgY,e.getX(),e.getY());
}
});
}
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}
1,这是一个简单的做一个窗体,在窗体里画直线,但这个编程有问题,为什么运行的时候说main()无效,有问题,究竟程序是怎样运行的? 这个问题出在哪?只要我把下面的代码全注释掉,只留下main()方法就没有问题了,下面的代码肯定是有问题,请高手帮助找出,谢谢。
2,如果我要是在建一个文件的话,把main()放在新的文件里,如果连接到别的文件里的代吗?
两个问题,大家帮帮助,感激不尽。 展开
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class DrawLine1 extends Frame {
public static void main(String[] args)
{
DrawLine1 mainFrame = new DrawLine1();
mainFrame.setSize(800,800);
mainFrame.setTitle("000");
mainFrame.setVisible(true);
}
int orgX;
int orgY;
int endX;
int endY;
Image oimg = null;
Graphics og = null;
Vector vLines = new Vector();
public void paint(Graphics g)
{
g.drawImage(oimg,0,0,this);
}
public DrawLine1() {
// Dimension d = getSize();
Dimension d = getSize();
oimg = this.createImage(d.width,d.height);
og = oimg.getGraphics();
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
DrawLine1.this.windowClosed();
}
}
);
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
orgX = e.getX();
orgY = e.getY();
}
public void mouseReleased(MouseEvent e)
{
endX = e.getX();
endY = e.getY();
Graphics g = getGraphics();
g.setColor(Color.RED);
g.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
g.drawString(orgX + "," + orgY,orgX,orgY);
g.drawString(e.getX() +","+e.getY(),e.getX(),e.getY());
g.drawLine(orgX,orgY,e.getX(),e.getY());
og.setColor(Color.RED);
og.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
og.drawString(orgX + "," + orgY,orgX,orgY);
og.drawString(e.getX() +","+e.getY(),e.getX(),e.getY());
og.drawLine(orgX,orgY,e.getX(),e.getY());
}
});
}
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}
1,这是一个简单的做一个窗体,在窗体里画直线,但这个编程有问题,为什么运行的时候说main()无效,有问题,究竟程序是怎样运行的? 这个问题出在哪?只要我把下面的代码全注释掉,只留下main()方法就没有问题了,下面的代码肯定是有问题,请高手帮助找出,谢谢。
2,如果我要是在建一个文件的话,把main()放在新的文件里,如果连接到别的文件里的代吗?
两个问题,大家帮帮助,感激不尽。 展开
4个回答
展开全部
可以了帮你解决了!!!代码如下:
//还有这个package 加上你的文件名!!!别忘记 不然你启动不来main方法
package com;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
public class DrawLine1 extends Frame {
int orgX;
int orgY;
int endX;
int endY;
Image oimg = null;
Graphics og = null;
Vector vLines = new Vector();
public static void main(String[] args){
DrawLine1 mainFrame = new DrawLine1();
}
public void paint(Graphics g){
g.drawImage(oimg,0,0,this);
}
public DrawLine1() {
// Dimension d = getSize();
//这个先初始化!!
setSize(800,800);
setTitle("000");
setVisible(true);
Dimension d = getSize();
// oimg=new Image();
oimg = createImage(d.width,d.height);
og = oimg.getGraphics();
// Add window listener.
this.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
new DrawLine1().windowClosed();
}
}
);
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
orgX = e.getX();
orgY = e.getY();
}
public void mouseReleased(MouseEvent e)
{
endX = e.getX();
endY = e.getY();
Graphics g = getGraphics();
g.setColor(Color.RED);
g.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
g.drawString(orgX + "," + orgY,orgX,orgY);
g.drawString(e.getX() +","+e.getY(),e.getX(),e.getY());
g.drawLine(orgX,orgY,e.getX(),e.getY());
og.setColor(Color.RED);
og.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
og.drawString(orgX + "," + orgY,orgX,orgY);
og.drawString(e.getX() +","+e.getY(),e.getX(),e.getY());
og.drawLine(orgX,orgY,e.getX(),e.getY());
}
});
}
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}
至于第二个问题的话 你问的应该是引用吧?引用外部类?
其实和你的mian方法一样。。。。。直接 DrawLine1 mainFrame = new DrawLine1();
然后调用就可以
//还有这个package 加上你的文件名!!!别忘记 不然你启动不来main方法
package com;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
public class DrawLine1 extends Frame {
int orgX;
int orgY;
int endX;
int endY;
Image oimg = null;
Graphics og = null;
Vector vLines = new Vector();
public static void main(String[] args){
DrawLine1 mainFrame = new DrawLine1();
}
public void paint(Graphics g){
g.drawImage(oimg,0,0,this);
}
public DrawLine1() {
// Dimension d = getSize();
//这个先初始化!!
setSize(800,800);
setTitle("000");
setVisible(true);
Dimension d = getSize();
// oimg=new Image();
oimg = createImage(d.width,d.height);
og = oimg.getGraphics();
// Add window listener.
this.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
new DrawLine1().windowClosed();
}
}
);
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
orgX = e.getX();
orgY = e.getY();
}
public void mouseReleased(MouseEvent e)
{
endX = e.getX();
endY = e.getY();
Graphics g = getGraphics();
g.setColor(Color.RED);
g.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
g.drawString(orgX + "," + orgY,orgX,orgY);
g.drawString(e.getX() +","+e.getY(),e.getX(),e.getY());
g.drawLine(orgX,orgY,e.getX(),e.getY());
og.setColor(Color.RED);
og.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
og.drawString(orgX + "," + orgY,orgX,orgY);
og.drawString(e.getX() +","+e.getY(),e.getX(),e.getY());
og.drawLine(orgX,orgY,e.getX(),e.getY());
}
});
}
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}
至于第二个问题的话 你问的应该是引用吧?引用外部类?
其实和你的mian方法一样。。。。。直接 DrawLine1 mainFrame = new DrawLine1();
然后调用就可以
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你不要在MAIN方法里写,你应该重新写一个类,再在MAIN中调用,学编程首先要有一个清晰的思路,然后把基础打好,以后做项目是很容已上手的。学编程可以把一个人的逻辑性增强!看得出来你好像还没入门样的,把基础放肆打好!不要想什么一下做出什么东西来.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1.你看看 public DrawLine1();
这是一个方法,你居然在main方法里面去new他,方法也能new吗?
还有,就算你DrawLine1被定义成一个类了,我没看到他继承什么类,也没看到他有size属性,你setSize做什么.
在main方法中你要用到框框应该是下面这个吧
Frame mainFrame =new Frame();
2.如果连接到别的文件里的代吗?
这是什么意思,字都打错还好意思问人,你能说清楚点不.
如果你要用到不同的文件的资源,要导包.
希望对你有帮助....
这是一个方法,你居然在main方法里面去new他,方法也能new吗?
还有,就算你DrawLine1被定义成一个类了,我没看到他继承什么类,也没看到他有size属性,你setSize做什么.
在main方法中你要用到框框应该是下面这个吧
Frame mainFrame =new Frame();
2.如果连接到别的文件里的代吗?
这是什么意思,字都打错还好意思问人,你能说清楚点不.
如果你要用到不同的文件的资源,要导包.
希望对你有帮助....
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
main()是用来运行类的,一般不直接在main()里写代码,只是建好类和所需的方法后用mian()来测试一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询