shapeframe.java~13~

来自「前段时间帮同学写了个简单动画程序:“贪食蛇的原型”」· JAVA~13~ 代码 · 共 79 行

JAVA~13~
79
字号
package com.cxlife.shape;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;public class ShapeFrame extends JFrame implements ActionListener {  private JPanel contentPane;  private Rectangle rect = null;  private XYLayout xYLayout1 = new XYLayout();  private JButton jButton1 = new JButton();//定义一个Rectangle对象  private Timer timer = null;  //Construct the frame  public ShapeFrame() {    enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {      jbInit();    }    catch(Exception e) {      e.printStackTrace();    }  }  //Component initialization  private void jbInit() throws Exception  {    //setIconImage(Toolkit.getDefaultToolkit().createImage(ShapeFrame.class.getResource("[Your Icon]")));    contentPane = (JPanel) this.getContentPane();    jButton1.setText("jButton1");    jButton1.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        jButton1_actionPerformed(e);      }    });    contentPane.setLayout(xYLayout1);    this.setSize(new Dimension(400, 300));    this.setTitle("Frame Title");    rect = new Rectangle(0,0,200,60);    contentPane.add(jButton1, new XYConstraints(190, 193, -1, -1));    timer = new Timer(1000,this);    timer.start();  }  //Overridden so we can exit when window is closed  protected void processWindowEvent(WindowEvent e) {    super.processWindowEvent(e);    if (e.getID() == WindowEvent.WINDOW_CLOSING) {      System.exit(0);    }  }  public void paintComponents(Graphics g){    super.paintComponents(g);    this.paint(g);  }   public void paint(Graphics g) {     super.paint(g);     Graphics2D g2D = (Graphics2D)g;     g2D.setColor(Color.red);     g2D.fill(rect);   }   public void update(Graphics g){     this.paint(g);   }  void jButton1_actionPerformed(ActionEvent e) {    System.err.println("rect.x=" + rect.x);    System.err.println("rect.y=" + rect.y);     rect.setLocation(rect.x + 10,rect.y);     this.invalidate();     this.repaint();  }     public void actionPerformed(ActionEvent e) {         rect.setLocation(rect.x + 10,rect.y);         this.repaint(rect.x-10,rect.y,200,60);     }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?