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()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)