学习JAVA不到2个月,你可以不回答,因为没有积分了,对以下问题有点疑问
1. java是跨平台的,那他是怎么跨的?
2. 没有安装JDK的java运行环境,是否运行不了java?
3. IDE是怎么运行java源文件?jvm是怎么解释.class文件?
4. 以下代码:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.Timer;public class acte extends JFrame implements ActionListener{
Timer timer; //设置计时器的过程包括创建一个 Timer 对象
int x,y,old_X,old_Y, r,x0,y0,w,h,ang;
int sdo,mdo,hdo,old_M,old_H;
TimeZone tz =TimeZone.getTimeZone("JST"); //设置时区
final double RAD=Math.PI/180.0; //设置秒针的位移量 public static void main(String[] args){
new acte();
} public void actionPerformed(ActionEvent e) {
timer.restart(); //重新启动 Timer
} acte(){
super("Clock");
setBackground(new Color(0,0,192)); //设置背景色
setResizable(false);
setDefaultCloseOperation(3);
setSize(300,300);
setVisible(true);
int delay = 1000;
//窗体添加事件监听,监听秒表的触发
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
repaint();
}
};
new Timer(delay, taskPerformer).start();
}//Graphics 类是所有图形上下文的抽象基类,允许应用程序在组件 以及闭屏图像上进行绘制。
public void paint( Graphics g ) {
Insets insets = getInsets(); //返回此容器的大小
int L0 = (insets.left)/2;
int T0 = (insets.top)/2;
int hh,mm,ss;
String st;
h=getSize().height; //获得此组件的高
//绘制圆形
g.setColor(Color.white); //设置图形的颜色
g.drawOval(L0+30,T0+30,h-60,h-60); //绘制椭圆的边框
g.drawOval(L0+32,T0+32,h-64,h-64);
r=h/2-30;
x0=30+r-5+L0;
y0=30+r-5-T0;
ang=60;
for (int i=1; i<=12; i++) {
x=(int)((r+10)*Math.cos(RAD*ang)+x0);
y=(int)((r+10)*Math.sin(RAD*ang)+y0);
g.drawString(""+i,x,h-y);
ang-=30;
}
x0=30+r+L0; y0=30+r+T0;
//获取时间
Calendar now=Calendar.getInstance();
hh=now.get(Calendar.HOUR_OF_DAY);//小时
mm=now.get(Calendar.MINUTE);//分钟
ss=now.get(Calendar.SECOND);// 秒
g.setColor(Color.pink);
g.fillRect(L0,T0,60,28);//填充的矩形
g.setColor(Color.blue);
if (hh < 10) st="0"+hh; else st=""+hh;
if (mm < 10) st=st+":0"+mm; else st=st+":"+mm;
if (ss < 10) st=st+":0"+ss; else st=st+":"+ss;
g.drawString(st,L0,T0+25);
//计算时间和图形的关系
sdo=90-ss*6;
mdo=90-mm*6;
hdo=90-hh*30-mm/2;
//擦除秒针
if (old_X > 0) {
g.setColor(getBackground());
g.drawLine(x0,y0,old_X,(h-old_Y));
} else {
old_M=mdo;
old_H=hdo;
}
//绘制秒针
g.setColor(Color.yellow);
x=(int)((r-8)*Math.cos(RAD*sdo)+x0);
y=(int)((r-8)*Math.sin(RAD*sdo)+y0)-2*T0;
g.drawLine(x0,y0,x,(h-y));
old_X=x;
old_Y=y;
//擦除分针和时针
if (mdo != old_M) {
line(g,old_M,(int)(r*0.7),getBackground());
old_M=mdo;
}
if (hdo != old_H) {
line(g,old_H,(int)(r*0.5),getBackground());
old_H=hdo;
}
//绘制分针
line(g,mdo,(int)(r*0.7),Color.green);
//绘制时针
line(g,hdo,(int)(r*0.5),Color.red);
} // end paint
public void line(Graphics g, int t, int n, Color c) {
int [] xp = new int[4];
int [] yp = new int[4];
xp[0]=x0;
yp[0]=y0;
xp[1]= (int)((n-10)*Math.cos(RAD*(t-4))+x0);
yp[1]=h-(int)((n-10)*Math.sin(RAD*(t-4))+y0);
xp[2]= (int)( n *Math.cos(RAD* t )+x0);
yp[2]=h-(int)( n *Math.sin(RAD* t )+y0);
xp[3]= (int)((n-10)*Math.cos(RAD*(t+4))+x0);
yp[3]=h-(int)((n-10)*Math.sin(RAD*(t+4))+y0);
g.setColor(c);
g.fillPolygon(xp,yp,4);
}
}
缺点很明显,希望高手该下。
1. java是跨平台的,那他是怎么跨的?
2. 没有安装JDK的java运行环境,是否运行不了java?
3. IDE是怎么运行java源文件?jvm是怎么解释.class文件?
4. 以下代码:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.Timer;public class acte extends JFrame implements ActionListener{
Timer timer; //设置计时器的过程包括创建一个 Timer 对象
int x,y,old_X,old_Y, r,x0,y0,w,h,ang;
int sdo,mdo,hdo,old_M,old_H;
TimeZone tz =TimeZone.getTimeZone("JST"); //设置时区
final double RAD=Math.PI/180.0; //设置秒针的位移量 public static void main(String[] args){
new acte();
} public void actionPerformed(ActionEvent e) {
timer.restart(); //重新启动 Timer
} acte(){
super("Clock");
setBackground(new Color(0,0,192)); //设置背景色
setResizable(false);
setDefaultCloseOperation(3);
setSize(300,300);
setVisible(true);
int delay = 1000;
//窗体添加事件监听,监听秒表的触发
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
repaint();
}
};
new Timer(delay, taskPerformer).start();
}//Graphics 类是所有图形上下文的抽象基类,允许应用程序在组件 以及闭屏图像上进行绘制。
public void paint( Graphics g ) {
Insets insets = getInsets(); //返回此容器的大小
int L0 = (insets.left)/2;
int T0 = (insets.top)/2;
int hh,mm,ss;
String st;
h=getSize().height; //获得此组件的高
//绘制圆形
g.setColor(Color.white); //设置图形的颜色
g.drawOval(L0+30,T0+30,h-60,h-60); //绘制椭圆的边框
g.drawOval(L0+32,T0+32,h-64,h-64);
r=h/2-30;
x0=30+r-5+L0;
y0=30+r-5-T0;
ang=60;
for (int i=1; i<=12; i++) {
x=(int)((r+10)*Math.cos(RAD*ang)+x0);
y=(int)((r+10)*Math.sin(RAD*ang)+y0);
g.drawString(""+i,x,h-y);
ang-=30;
}
x0=30+r+L0; y0=30+r+T0;
//获取时间
Calendar now=Calendar.getInstance();
hh=now.get(Calendar.HOUR_OF_DAY);//小时
mm=now.get(Calendar.MINUTE);//分钟
ss=now.get(Calendar.SECOND);// 秒
g.setColor(Color.pink);
g.fillRect(L0,T0,60,28);//填充的矩形
g.setColor(Color.blue);
if (hh < 10) st="0"+hh; else st=""+hh;
if (mm < 10) st=st+":0"+mm; else st=st+":"+mm;
if (ss < 10) st=st+":0"+ss; else st=st+":"+ss;
g.drawString(st,L0,T0+25);
//计算时间和图形的关系
sdo=90-ss*6;
mdo=90-mm*6;
hdo=90-hh*30-mm/2;
//擦除秒针
if (old_X > 0) {
g.setColor(getBackground());
g.drawLine(x0,y0,old_X,(h-old_Y));
} else {
old_M=mdo;
old_H=hdo;
}
//绘制秒针
g.setColor(Color.yellow);
x=(int)((r-8)*Math.cos(RAD*sdo)+x0);
y=(int)((r-8)*Math.sin(RAD*sdo)+y0)-2*T0;
g.drawLine(x0,y0,x,(h-y));
old_X=x;
old_Y=y;
//擦除分针和时针
if (mdo != old_M) {
line(g,old_M,(int)(r*0.7),getBackground());
old_M=mdo;
}
if (hdo != old_H) {
line(g,old_H,(int)(r*0.5),getBackground());
old_H=hdo;
}
//绘制分针
line(g,mdo,(int)(r*0.7),Color.green);
//绘制时针
line(g,hdo,(int)(r*0.5),Color.red);
} // end paint
public void line(Graphics g, int t, int n, Color c) {
int [] xp = new int[4];
int [] yp = new int[4];
xp[0]=x0;
yp[0]=y0;
xp[1]= (int)((n-10)*Math.cos(RAD*(t-4))+x0);
yp[1]=h-(int)((n-10)*Math.sin(RAD*(t-4))+y0);
xp[2]= (int)( n *Math.cos(RAD* t )+x0);
yp[2]=h-(int)( n *Math.sin(RAD* t )+y0);
xp[3]= (int)((n-10)*Math.cos(RAD*(t+4))+x0);
yp[3]=h-(int)((n-10)*Math.sin(RAD*(t+4))+y0);
g.setColor(c);
g.fillPolygon(xp,yp,4);
}
}
缺点很明显,希望高手该下。
不同的平台,安装不同版本的jdk.
2. 没有安装JDK的java运行环境,是否运行不了java?
不可以。
3. IDE是怎么运行java源文件?jvm是怎么解释.class文件?
简单一句话:通过jdk的编译器来编译成.class。任何一本讲java基础的书都有详细介绍
对不起,代码我没看。
我是问 IDE是怎么运行java源文件?jvm是怎么解释.class文件? 他的内部是怎么搞的?听说可以在JDK里面 看的,问下怎么看? 谢谢。
程序源文件生成的.class不管在什么平台下生成的都是一样,不一样的是jvm,也就是不同的运行环境,window下面的jre与linux的jre执行相同的.class文件做不同的事情,返回相同的结果.他封装是执行过程,但在不同的平台下,你给他相同的东西,得到相同的返回结果,所以他跨平台是指他实现了不同平台下的jre!
2.没有安装JDK的java运行环境,是否运行不了java
没有jdk当然执行不了.class文件.不过如果只是运行的话只要安装jre就行.jdk是编译环境,里面包括了jre
3. IDE是怎么运行java源文件?jvm是怎么解释.class文件?
同上IDE如果在系统里找不到jre,或者你没有指定,就用自带的.jre不大不到100M.
jvm怎么解释.class文件,我也不知道怎么解释的.
呵呵
3,建议你看看《深入Java虚拟机》和编译原理。
4.AWT不懂。呵呵。