编写一个Applet小程序

编写一个Applet小程序,第1张

import java.awt.Graphicsimport java.applet.Appletimport javax.swing.JFramepublic class AppletN extends Applet{public void paint(Graphics g){int sum=0 int row=1, col=1 for(int i=1i<=10i++){int sun=1for(int j=1j<=ij++){sun*=j }sum+=sun g.drawString(i+"!="+sun, 80*col, 40*row) if(++col==4){row++ col=1 }}row++ col=80 g.drawString("1!+2!+..+10!="+sum, col, 40*row) }public static void main(String a[]){JFrame j = new JFrame("AppletN") j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) j.setSize(400, 300) j.add(new AppletN()) j.setVisible(true) }}

import javax.swing.*import java.applet.*

class MyPanel extends JPanel{

JButton button1,button2

JLabel label

MyPanel(String s1,String s2,String s3){

button1=new JButton(s1)

button2=new JButton(s2)

label=new JLabel(s3)

add(button1)add(button2)add(label)

}

}

public class Example5_4 extends Applet{

MyPanel panel1,panel2

JButton button

public void init(){

panel1=new MyPanel("确定","取消","标签,我们在面板1中")

panel2=new MyPanel("确定","取消","标签,我们在面板2中")

button=new JButton("我是不在面板中的按钮")

add(panel1)add(panel2)add(button)

setSize(300,200)

}

}

import java.awt.*

/**

* @author Hardneedl

*/

interface Brush {

void doPaint(Graphics g)

}

import javax.swing.*

import javax.swing.border.*

import java.awt.*

import java.awt.event.*

/**

* @author Hardneedl

*/

public class StringAppletDemo extends JApplet {

private StringBrush stringBrush

private class _Canvas extends JComponent {

protected void paintComponent(Graphics g) {

super.paintComponent(g)

stringBrush.doPaint(g)

}

public Border getBorder() {return BorderFactory.createLineBorder(Color.BLACK,2)}

}

private _Canvas canvas

private final static int STEP = 10

private final static Point PIONT = new Point(100, 170)

protected void processKeyEvent(KeyEvent e) {

switch (e.getKeyCode()) {

case KeyEvent.VK_LEFT:

stringBrush.setLocation(PIONT.x -= STEP, PIONT.y)

canvas.paintImmediately(canvas.getBounds())

break

case KeyEvent.VK_RIGHT:

stringBrush.setLocation(PIONT.x += STEP, PIONT.y)

canvas.paintImmediately(canvas.getBounds())

break

case KeyEvent.VK_DOWN:

stringBrush.setLocation(PIONT.x, PIONT.y += STEP)

canvas.paintImmediately(canvas.getBounds())

break

case KeyEvent.VK_UP:

stringBrush.setLocation(PIONT.x, PIONT.y -= STEP)

canvas.paintImmediately(canvas.getBounds())

break

}

}

public void init() {

super.init()

stringBrush = new StringBrush(StringAppletDemo.class.getName())

canvas = new _Canvas()

getContentPane().add(canvas, BorderLayout.CENTER)

}

private class StringBrush implements Brush {

private String s

private StringBrush(String s) {this.s = s}

private Point p = new Point(100,100)

private void setLocation(int x,int y) {p.setLocation(x,y)}

public void doPaint(Graphics g) {

Graphics g2d = g.create()

g2d.drawString(s, p.x, p.y)

g2d.dispose()

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11597657.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存