我是一个完整的 Java 新手,需要一些帮助。基本上,我想为我已经制作的游戏制作一个具有背景的 JFrame。我刚刚使用了 NetbeansJFrame
设计选项卡,并希望我可以将代码组合到代码的专用部分中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
}
谢谢!