《Java程序设计》实验指导书-2 - [1] 下载本文

public class shiyan53 extends JFrame { JButton jbut0,jbut1,jbut2,jbut3,jbut4,jbut5; JButton jbut6,jbut7,jbut8,jbut9,jbutClear,jbutCalculate; JButton jbutAdd,jbutSubtract,jbutMultiply,jbutDivide; JTextField jTxt; float s=0,t=0,result; int operator; boolean flag=true; JLabel jlab=new JLabel(); // shiyan53(){ super(\ Container contentPane=getContentPane(); contentPane.setLayout(new GridLayout(5,4)); jbut0=new JButton(\ jbut1=new JButton(\ jbut2=new JButton(\ jbut3=new JButton(\ jbut4=new JButton(\ jbut5=new JButton(\ jbut6=new JButton(\ jbut7=new JButton(\ jbut8=new JButton(\ jbut9=new JButton(\ jbutAdd=new JButton(\ jbutSubtract=new JButton(\ jbutMultiply=new JButton(\ jbutDivide=new JButton(\ jbutClear=new JButton(\ jbutCalculate=new JButton(\ jTxt=new JTextField(); contentPane.add(jbut0); contentPane.add(jbut1); contentPane.add(jbut2); contentPane.add(jbut3); contentPane.add(jbut4); contentPane.add(jbut5); contentPane.add(jbut6); contentPane.add(jbut7); contentPane.add(jbut8); contentPane.add(jbut9); contentPane.add(jbutAdd); contentPane.add(jbutSubtract); contentPane.add(jbutMultiply);

contentPane.add(jbutDivide); contentPane.add(jbutClear); contentPane.add(jbutCalculate); contentPane.add(jTxt); jbut0.addActionListener(new ButtonHandler()); jbut1.addActionListener(new ButtonHandler()); jbut2.addActionListener(new ButtonHandler()); jbut3.addActionListener(new ButtonHandler()); jbut4.addActionListener(new ButtonHandler()); jbut5.addActionListener(new ButtonHandler()); jbut6.addActionListener(new ButtonHandler()); jbut7.addActionListener(new ButtonHandler()); jbut8.addActionListener(new ButtonHandler()); jbut9.addActionListener(new ButtonHandler()); jbutAdd.addActionListener(new ButtonHandler()); jbutSubtract.addActionListener(new ButtonHandler()); jbutMultiply.addActionListener(new ButtonHandler()); jbutDivide.addActionListener(new ButtonHandler()); jbutClear.addActionListener(new ButtonHandler1()); jbutCalculate.addActionListener(new ButtonHandler2()); }

//ActionEvent class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ String o,p; p=e.getActionCommand(); o=jTxt.getText(); jTxt.setText(o+p); if(p==\ else if(p==\ else if(p==\ else if(p==\ else if(flag) s=10*s+(int) (p.charAt(0)-48); else t=10*t+ (int) (p.charAt(0)-48); } }

//ActionEvent clear jTxt class ButtonHandler1 implements ActionListener{ public void actionPerformed(ActionEvent e){ jTxt.setText(\

flag=true; s=0; t=0; } }

//ActionEvent calculator class ButtonHandler2 implements ActionListener{ public void actionPerformed(ActionEvent e){ switch(operator){ case 1: result=s+t; break; case 2: result=s-t; break; case 3: result=s*t; break; case 4: result=s/t; break; } jTxt.setText(\ } } public static void main(String args[]){ shiyan53 cal=new shiyan53(); cal.setSize(400,150); cal.setDefaultCloseOperation(cal.EXIT_ON_CLOSE); cal.setVisible(true); } }

实验六 Applet

实验目的

1.掌握Java Applet程序结构和开发过程 2.了解Applet运行机制

3.学会编写Applet对应的HTML文件,掌握从HTML文件向Applet传递参数的方法。 4.了解Applet与浏览器的通信。 5.掌握在Applet图形界面设计。

实验原理

Applet小应用程序,是Java与Web相结合的一种应用形式。它可以使Web页具有动画、声音、图象和其他特效功能,可使Web页能够与用户动态交互,接受用户的输入并且给用户以相应的回应。而且在Applet中支持使用AWT和SWING组件构建图形化用户界面。 实验内容

1.编写Applet,从HTML文件中获取两个字符串参数,并将它们在浏览器中显示出来。 2.编写Applet,绘制直线、各种矩形、多边形、圆和椭圆等图形。

3.编写Applet,在浏览器中显示一幅动画。(在一定范围内碰撞运行的小球)

以下是本题的参考代码,请大家调试运行然后根据自己的需求修该相应的功能。例如:将小球的颜色改变、小球的运行轨迹改为水平或垂直移动等。 import java.applet.*; import java.awt.*;

public class shiyan63 extends Applet implements Runnable { int x = 150, y = 50, r = 15; int dx = 11, dy = 7; Thread animator;

int s,t;

volatile boolean pleaseStop;

public void paint(Graphics g) { g.setColor(Color.red);

g.fillOval(x-r, y-r, r*2, r*2); }

public void animate() {

Rectangle bounds = getBounds(); s= bounds.width; t= bounds.height;

if ((x - r + dx < 0) || (x + r + dx > bounds.width)) {dx = -dx;}

if ((y - r + dy < 0) || (y + r + dy > bounds.height)) {dy = -dy;} x += dx; y += dy; repaint(); }

public void run() {

while(!pleaseStop) { animate(); try { Thread.sleep(100);

}catch(InterruptedException e) {} } }

public void start() {

animator = new Thread(this); pleaseStop = false; animator.start(); }