在windowbuilder中使用我的游戏按钮

时间:2016-02-21 08:37:51

标签: java eclipse swing jframe windowbuilder

我早些时候在Stack溢出时查看了如何在eclipse中使用带有窗口构建器的按钮,如果你点击它一旦它会执行一行(或更多)代码,那么如果你再次点击它会做一个不同的行代码等(第三,第四,第五次点击等)。

我想知道如何在Java中实现这一点,你可以通过鼠标按下按钮来实现这一点。这是我的测试代码(此代码目前尚未确认,但我想知道您是否可以使用此示例)谢谢,任何帮助表示赞赏。

这款游戏基本上是从两个或多个指定选项中选择,引导您走向不同的路径,然后将它们输入到底部文本框中,该文本框称为Log,当用户进入时,文本框的最顶部称为控制台回答Log并单击Send它将该答案的结果打印到控制台文本框中。

[更新信息部分] 您好,我正在尝试制作一个游戏,其中用户在顶部框中获得一个称为“控制台”的问题,他们在底部框“日志”中输入答案,然后按发送继续下一部分。在那之后,我希望它做同样的事情,而不是在下面(我更新的代码),制作不同的按钮,使一个不可见,所以他们能够点击下一个,我希望它只是一个按钮他们点击并且每次处理不同的代码行,即如果框控制台中的问题说你想要一把剑或弓,并且用户选择弓并将其输入到日志框中并点击发送我想要它说好吗,你需要多少金币。用户输入金额并点击发送。等等等等。再次感谢您提供任何帮助。

package Game;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JLabel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
public class Swendiver {

    private JFrame frmSwendiver;
    private JTextField console;
    private JTextField Log;
    private JTextField Weapon;
    private JTextField Arrows;
    private JTextField next;
    private JTextField Class;
    private JTextField Coin;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Swendiver window = new Swendiver();
                    window.frmSwendiver.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Swendiver() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmSwendiver = new JFrame();
        frmSwendiver.setTitle("Swendiver 1.0");
        frmSwendiver.setAutoRequestFocus(false);
        frmSwendiver.setAlwaysOnTop(true);
        frmSwendiver.setBounds(100, 100, 450, 300);
        frmSwendiver.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmSwendiver.getContentPane().setLayout(null);

        console = new JTextField();
        console.setBounds(0, 0, 434, 44);
        console.setEditable(false);
        frmSwendiver.getContentPane().add(console);
        console.setColumns(10);

        Log = new JTextField();
        Log.setBounds(0, 241, 434, 20);
        Log.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                // KeyEvent.getKeyCode(VK_ENTE-R);
            }
        });
        frmSwendiver.getContentPane().add(Log);
        Log.setColumns(10);

        JLabel lblThankYouFor = new JLabel("Health : 100%");
        lblThankYouFor.setBackground(new Color(50, 205, 50));
        lblThankYouFor.setBounds(165, 165, 100, 27);
        frmSwendiver.getContentPane().add(lblThankYouFor);
        console.setText("Press The enter button to start Swendiver");
        JButton Send = new JButton("Enter");
        Send.setBounds(345, 41, 89, 200);
        Send.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        Send.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                boolean isClicked = true;
                if (isClicked == true) {
                    isClicked = false;

                    lblThankYouFor.setName("Health: 75%");
                    console.setText("[Hermin] Welcome. Ready for your adventure? (Click Enter)");
                    Send.setVisible(false);
                }
            }

        @Override
            public void mousePressed(MouseEvent arg0) {
                Log.setText("");
                ArrayList<String> weapon = new ArrayList<String>();
                console.setText("Ok, choose your weapon. <SWORD>||<BOW>");
                // Log.getText().equalsIgnoreCase("SWORD")
                // console.setText("Good choice.");
                Log.setText("");
                weapon.add("Sword");
                if (weapon.get(0).equalsIgnoreCase("Sword")) {
                    console.setText("[Hermin] A sword? I see. Good choice...");
                    next.setText("This box is used for Instructions/Actions");
                }
            }
        });

        frmSwendiver.getContentPane().add(Send);

        JLabel lblWeapon = new JLabel("Weapon:");
        lblWeapon.setBounds(266, 87, 56, 14);
        frmSwendiver.getContentPane().add(lblWeapon);
        JButton button3 = new JButton("Enter");
        button3.setBounds(345, 41, 89, 200);
        Weapon = new JTextField();
        Weapon.setBounds(249, 101, 86, 20);
        Weapon.setEditable(false);
        frmSwendiver.getContentPane().add(Weapon);
        Weapon.setColumns(10);

        JButton Enter = new JButton("Enter");
        Enter.setBounds(345, 41, 89, 200);
        frmSwendiver.getContentPane().add(Enter);


        JLabel lblArrows = new JLabel("Arrows:");
        lblArrows.setEnabled(false);
        lblArrows.setBounds(165, 87, 46, 14);
        frmSwendiver.getContentPane().add(lblArrows);

        Arrows = new JTextField();
        Arrows.setEnabled(false);
        Arrows.setBounds(155, 101, 68, 20);
        Arrows.setEditable(false);
        frmSwendiver.getContentPane().add(Arrows);
        Arrows.setColumns(10);
            Random attack = new Random();
        next = new JTextField();
        next.setBounds(0, 41, 239, 27);
        next.setEditable(false);
        frmSwendiver.getContentPane().add(next);
        next.setColumns(10);
        boolean attacksituation = false;
        JButton Attack = new JButton("Attack");
        Attack.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(attacksituation == true) {
                next.setText(Integer.toString(attack.nextInt(5)));
                Attack.setEnabled(false);
                Attack.setEnabled(true);
                }
                }
        });
        Attack.setBounds(0, 68, 89, 173);
        frmSwendiver.getContentPane().add(Attack);

        JLabel lblClass = new JLabel("Class:");
        lblClass.setBounds(276, 47, 46, 14);
        frmSwendiver.getContentPane().add(lblClass);

        Class = new JTextField();
        Class.setEditable(false);
        Class.setBounds(249, 61, 86, 20);
        frmSwendiver.getContentPane().add(Class);
        Class.setColumns(10);

        Coin = new JTextField();
        Coin.setText("78");
        Coin.setEditable(false);
        Coin.setBounds(125, 221, 86, 20);
        frmSwendiver.getContentPane().add(Coin);
        Coin.setColumns(10);
        next.setText("");
        JLabel lblCoin = new JLabel("Coin:");
        lblCoin.setBounds(99, 224, 46, 14);
        frmSwendiver.getContentPane().add(lblCoin);
        Enter.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {

                console.setText("[Hermin]Choose your class <Warrior> or <Archer>");
                if (Log.getText().equalsIgnoreCase("Warrior")) {
                    console.setText("[Hermin]A Swordsman.. I see. Good choice.");
                    Weapon.setText("Sword");
                    next.setEnabled(true);
                    next.setText("Click Enter to continue");
                    Log.setText("");
                    Class.setText("Warrior");
                    Enter.setVisible(false);
                }
                if (Log.getText().equalsIgnoreCase("Archer")) {
                    console.setText("[Hermin]An Archer.. Interesting. Here are your Items.");
                    Weapon.setText("Long-Bow");
                    Arrows.setText("19");
                    next.setEnabled(true);
                    next.setText("Click Enter to continue");
                    Log.setText("");
                    Class.setText("Archer");
                    lblArrows.setEnabled(true);
                    Arrows.setEnabled(true);
                    Enter.setVisible(false);

                }
                if(Log.getText().equalsIgnoreCase("/secretcommand")) {
                    Coin.setText(Integer.toString(300));
                    next.setText("222 Coin Aquired");
                    next.setEnabled(true);
                    Log.setText("");
                    next.setText("Click Enter to continue!");
                    Enter.setVisible(false);
                }

            }

        });

        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                console.setText("[Hermin] Great. Welcome then. Head into SheildsTown to collect some items."); 
                next.setText("/EquipArmor to get geared and go.");
                if(Log.getText().equalsIgnoreCase("/EquipArmor")){
                    next.setText("You leave the armory.");
                    console.setText("");
                    Log.setText("");
                }
            }
        });
        frmSwendiver.getContentPane().add(button3);

    }
}

0 个答案:

没有答案