制作了一个JFrame,制作了一个小程序,不知道如何将两者结合起来?

时间:2013-03-12 21:57:22

标签: java swing applet jframe

我是一个完整的Java新手,需要一些帮助。基本上,我想要制作一个具有我已经制作的游戏背景的JFrame。我刚刚使用了Netbeans JFrame设计标签,并希望我可以将代码合并到JFrame代码的专用部分中,这是我无法做到的。

或者,如果有人可以告诉我如何在我的游戏中创建的JFrame中添加背景,那就好了,代码如下:

    package javagame;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;


public class JavaGame extends JFrame {

    int x, y;
        Image piggy1;
    private Image dbImage;
    private Graphics dbg;


    public class ActionListener extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();

            if(keyCode == e.VK_LEFT) {
                if(x <= 0)
                    x = 0;
            else
                x += -5;
            }

            if(keyCode == e.VK_RIGHT) {
                if (x>= 450)
                    x = 450;
                else
                    x += +5;
                        }

            if(keyCode == e.VK_UP) {
                if(y<= 20)
                    y = 20;
            else
                y += -5;
            }
            if(keyCode == e.VK_DOWN) {
                if (y>= 450)
                    y = 450;
            else
                y += +5;
            }

        }
        public void keyReleased(KeyEvent e) {

        }
    }

    public JavaGame() {

                //Images
                ImageIcon i = new ImageIcon("C:/Users/MrBlueMKII/Documents/NetBeansProjects/JavaGame/src/javagame/PIGGY.gif");
                piggy1 = i.getImage();

                addKeyListener(new ActionListener());
        setTitle("Java Game");
        setSize(500, 500);
                setResizable(false);
                setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        x = 150;
        y = 150;

                ImageIcon j = new ImageIcon("C:/Users/MrBlueMKII/Downloads/field.jpg");



    }

    public void paint(Graphics g){
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);
    }


    public void paintComponent(Graphics g) {
        g.drawImage(piggy1, x, y, this);

        repaint();


    }


    public static void main(String[] args)  {

        new JavaGame();


    } 
}

JFrame的代码是:

package javagame;


public class JFrame extends javax.swing.JFrame {

public JFrame() {
    initComponents();
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jTextField1 = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel1.setLayout(null);

    jTextField1.setBackground(new java.awt.Color(223, 248, 248));
    jTextField1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    jTextField1.setForeground(new java.awt.Color(255, 255, 255));
    jTextField1.setText("Eat the flies to evolve wings!");
    jTextField1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jTextField1ActionPerformed(evt);
        }
    });
    jPanel1.add(jTextField1);
    jTextField1.setBounds(280, 0, 200, 80);

    jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\MrBlueMKII\\Downloads\\field.jpg")); // NOI18N
    jLabel1.setText("jLabel1");
    jPanel1.add(jLabel1);
    jLabel1.setBounds(0, 0, 500, 500);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {

}



public static void main(String args[]) {

    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>


    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new JFrame().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}

谢谢!

1 个答案:

答案 0 :(得分:1)

最好的方法是将您需要的所有常见元素移到单独的组件中(使用JPanel代替JFrameJApplet

这将允许您根据需要添加到框架或小程序。

这是非常常见的设计理念,不要通过在共同的祖先中共同实现逻辑将自己陷入特定的顶级容器