package clickrpg2;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.Timer;
public class MainFrame extends javax.swing.JFrame implements ActionListener {
static MainFrame mainFrame = new MainFrame();
static Hero hero = new Hero();
static JLabel fireballButton[] = new JLabel[1000000];
static int fireballTotal = 0;
Timer timer = new Timer(10, this);
public MainFrame() {
addKeyListener(new KeyThing());
addMouseListener(new MouseThing());
initComponents();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/clickrpg2/StickMan1.png"))); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(1454, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(597, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
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(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
mainFrame.setVisible(true);
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
int c = 0;
int x = 0;
int y = 0;
int xChar = jLabel1.getX();
int yChar = jLabel1.getY();
while (true) {
x = fireballButton[c].getX();
y = fireballButton[c].getY();
fireballButton[c].setBounds(x + 5, y, 64, 32);
c++;
if (c == fireballTotal) {
break;
}
}
jLabel1.setBounds(xChar,yChar,64,64);
}
class KeyThing extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int x = jLabel1.getX();
int y = jLabel1.getY();
int speed = hero.getSpeed();
jLabel1.setBounds(x, y, 64, 64);
int keycode = e.getKeyCode();
switch (keycode) {
case KeyEvent.VK_LEFT:
jLabel1.setBounds(x - speed, y, 64, 64);
break;
case KeyEvent.VK_RIGHT:
jLabel1.setBounds(x + speed, y, 64, 64);
break;
case KeyEvent.VK_DOWN:
jLabel1.setBounds(x, y + speed, 64, 64);
break;
case KeyEvent.VK_UP:
jLabel1.setBounds(x, y - speed, 64, 64);
break;
case KeyEvent.VK_SPACE:
x = jLabel1.getX();
y = jLabel1.getY();
fireballButton[fireballTotal] = new JLabel();
fireballButton[fireballTotal].setPreferredSize(new Dimension(x, y));
mainFrame.getContentPane().add(fireballButton[fireballTotal], BorderLayout.CENTER);
ImageIcon label = new ImageIcon("C:\\Users\\Nick\\Documents\\NetBeansProjects\\ClickRPG2\\src\\clickrpg2\\Fireball.png");
fireballButton[fireballTotal].setIcon(label);
mainFrame.setVisible(true);
fireballButton[fireballTotal].setBounds(x, y, 64, 64);
mainFrame.add(fireballButton[fireballTotal]);
fireballTotal++;
timer.start();
if (fireballTotal > 900000) {
fireballTotal = 0;
}
break;
}
}
}
class MouseThing extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
int x = mainFrame.getX();
int y = mainFrame.getY();
fireballButton[fireballTotal] = new JLabel();
ImageIcon label = new ImageIcon("C:\\Users\\Nick\\Documents\\NetBeansProjects\\ClickRPG2\\src\\clickrpg2\\Fireball.png");
fireballButton[fireballTotal].setIcon(label);
fireballButton[fireballTotal].setBounds(x, y, 64, 64);
fireballTotal++;
timer.start();
if (fireballTotal > 900000) {
fireballTotal = 0;
}
}
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
基本上,当我制作火球 jLabel 时,我只需要它不重置 jLabel1。\
有什么建议吗?在此先感谢,我是新来的,所以我希望代码块。因为我不知道错误发生在哪里,所以我需要保留大部分代码。抱歉。