日期:2014-05-20 浏览次数:20761 次
import javax.swing.*; import java.awt.*; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub MainGui mainGui=new MainGui(); } } class MyPanel extends JPanel implements Runnable { int x=180; int y=10; int speed=5; long time=100; public void paint(Graphics g) { super.paint(g); g.setColor(Color.YELLOW); g.drawString("helloJava", x, y+=speed); } public MyPanel() { this.setBackground(Color.BLACK); Thread td=new Thread(this); td.start(); } public void run() { while (true) { repaint(); try { Thread.sleep(time); } catch (Exception e) { e.printStackTrace(); } } } } /* * 程序的主界面 */ class MainGui extends JFrame { //定义需要的组件 JMenuBar jmb; JMenu jm1; JMenu jm2; JMenuItem jmi1; JMenuItem jmi2; JMenuItem jmi3; MyPanel mp=new MyPanel(); public MainGui() { //初始化各个组件 jmb=new JMenuBar(); jm1=new JMenu("游戏"); jm2=new JMenu("帮助"); jmi1=new JMenuItem("开始游戏"); jmi2=new JMenuItem("退出游戏"); jmi3=new JMenuItem("游戏说明"); //添加相关的组件 jm1.add(jmi1); jm1.add(jmi2); jm2.add(jmi3); jmb.add(jm1); jmb.add(jm2); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(400,300); this.add(jmb,BorderLayout.NORTH); this.add(mp); this.setVisible(true); } }