文本没有显示在textarea中,如果语句不起作用

时间:2013-12-16 19:13:06

标签: java swing netbeans textarea

我正在尝试使用netbeans创建一个简单的基于文本的RPG。目前我无法在textarea框中正确显示消息。它目前只显示所有消息的“你有#health left”,无论我按下攻击按钮多少次。我遇到的另一个问题是,即使在玩家或敌人超过0之后我的游戏仍然继续进行...代码如下。我正在使用netbeans来实现这一点,所以这里的代码可能看起来有些不可思议。

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.Random;
/**
 *
 * @author C.C
 */
public class slime extends javax.swing.JFrame {
    Random rng = new Random();
    int choice;
    int player = 100;
    int king = 100;
    int slime = 50;
    /**
     * Creates new form slime
     */
    public slime() {
        initComponents();
        choice = rng.nextInt(10)+1;
        if(choice >=7)
        {
            battle.append("You encountered a King Slime!");
        }
        else
        {
            battle.append("You encountered a slime!");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        attack = new javax.swing.JButton();
        heal = new javax.swing.JButton();
        charge = new javax.swing.JButton();
        run = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        battle = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        attack.setText("Attack");
        attack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                attackActionPerformed(evt);
            }
        });

        heal.setText("Heal");

        charge.setText("Charge");

        run.setText("Run");

        battle.setColumns(20);
        battle.setRows(5);
        jScrollPane1.setViewportView(battle);

        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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(heal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(attack, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(charge, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                            .addComponent(run, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(attack)
                    .addComponent(charge))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(heal)
                    .addComponent(run))
                .addContainerGap(16, Short.MAX_VALUE))
        );

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

    private void attackActionPerformed(java.awt.event.ActionEvent evt) {
       int damage = rng.nextInt(10)+1;
       int damage2 = rng.nextInt(15)+1;
       int damage3 = rng.nextInt(10)+1;
       if(player <= 0)
       {
           battle.setText("You have died");
           System.exit(0);
       }
       else
       {
            if(damage >=7)
            {
                battle.setText("You did "+ String.valueOf(damage)+ " damage. It was a critcal hit!");
                if (choice >=7)
                {
                    king = king-damage;
                    if(king <=0)
                    {
                        battle.append("You have defeated the King Slime!");
                        System.exit(0);
                    }
                    battle.append("The Slime King hit you for " + String.valueOf(damage2) + " damage!");
                    player = player-damage2;
                    battle.append("You have "+ String.valueOf(player) + " health left.");
                }
                else
                {
                    slime = slime-damage;
                    if(slime <=0)
                    {
                        battle.append("You have defeated the slime!");
                        System.exit(0);
                    }
                    battle.append("The slime hit you for " + String.valueOf(damage3) + " damage!");
                    player = player-damage3;
                    battle.append("You have "+ String.valueOf(player) + " health left.");                  
                }
            }
            else
            {
                battle.append("You did "+ String.valueOf(damage)+ " damage.");
                if (choice >=7)
                {
                    king = king-damage;
                    battle.append("The Slime King hit you for " + String.valueOf(damage2) + " damage!");
                    player = player-damage2;
                    battle.append("You have "+ String.valueOf(player) + " health left.");
                }
                else
                {
                    slime = slime-damage;
                    battle.append("The slime hit you for " + String.valueOf(damage3) + " damage!");
                    player = player-damage3;
                    battle.append("You have "+ String.valueOf(player) + " health left.");
                }
            }
       }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<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(slime.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(slime.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(slime.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(slime.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new slime().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton attack;
    private javax.swing.JTextArea battle;
    private javax.swing.JButton charge;
    private javax.swing.JButton heal;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton run;
    // End of variables declaration
}

0 个答案:

没有答案