GUI中的Java文本框(基于文本的游戏)

时间:2015-12-25 02:44:31

标签: java user-interface textbox

我正在为学校制作一个程序,它应该是一个GUI,但我想让它变得更有趣。所以我决定把它变成一个基于文本的游戏。唯一的问题是,我不知道如何在屏幕上添加文本框,并让它显示用户选项中发生的不同事情的文本。 代码就在这里,我想在战斗中或者去商店时出现一个文本框。如果你能提供帮助那就太好了!

    package src.main.derp.skdlfja;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.ScrollPane;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import javax.swing.ImageIcon;
    import javax.swing.JCheckBoxMenuItem;
    import javax.swing.JFrame; 
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JRadioButtonMenuItem; 
    import javax.swing.JScrollPane; 
    import javax.swing.JTextArea;
    import javax.swing.JTextField;

    public class Menu extends Char{
    public boolean atk = false;
    public boolean talk = false;
    public boolean item = false;
    public boolean surrender = false;
    public boolean spare = false;
    public boolean atkScreen = false;

   JTextArea output;
   JScrollPane scrollPane;

   protected JTextField textField;
   protected JTextArea textArea;
   private final static String newline = "\n"; 

   public JMenuBar createMenuBar() {
    JMenuBar menuBar;
    JMenu menu, submenu;
    JMenuItem menuItem;
    JRadioButtonMenuItem rbMenuItem;
    JCheckBoxMenuItem cbMenuItem;

    menuBar = new JMenuBar();
    menu = new JMenu("Action Menu");
    menu.setMnemonic(KeyEvent.VK_A);
    menu.getAccessibleContext().setAccessibleDescription("Fighing Actions");
    menuBar.add(menu);
    menuItem = new JMenuItem("Attack", KeyEvent.VK_T);
    menu.add(menuItem);
    submenu = new JMenu("Talk");
    submenu.setMnemonic(KeyEvent.VK_S);
    menuItem = new JMenuItem("Check");
    submenu.add(menuItem);
    menuItem = new JMenuItem("Say Hi");
    submenu.add(menuItem);
    menuItem = new JMenuItem("");
    submenu.add(menuItem);
    menuItem = new JMenuItem("");
    submenu.add(menuItem);
    menu.add(submenu);
    submenu = new JMenu("Item");
    submenu.setMnemonic(KeyEvent.VK_S);

    menuItem = new JMenuItem("Health Bottle");

    submenu.add(menuItem);

    menuItem = new JMenuItem("Smoke Bomb");
    submenu.add(menuItem);
    menu.add(submenu);

    submenu = new JMenu("Spare/Flee");
    submenu.setMnemonic(KeyEvent.VK_S);

    menuItem = new JMenuItem("Spare");

    submenu.add(menuItem);

    menuItem = new JMenuItem("Flee");
    submenu.add(menuItem);
    menu.add(submenu);
    menu = new JMenu("Shop");
    menu.setMnemonic(KeyEvent.VK_N);

    submenu = new JMenu("Buy");
    submenu.setMnemonic(KeyEvent.VK_S);

    menuItem = new JMenuItem("Health Potion");
    submenu.add(menuItem);

    menuItem = new JMenuItem("Smoke Bomb");
    submenu.add(menuItem);

    menuItem = new JMenuItem("New Book");
    submenu.add(menuItem);

    menuItem = new JMenuItem("Old Shoes");
    submenu.add(menuItem);

    menu.add(submenu);

    menuItem = new JMenuItem("Kill",
            KeyEvent.VK_T);
    menu.add(menuItem);

    submenu = new JMenu("Talk");
    submenu.setMnemonic(KeyEvent.VK_S);

    menuItem = new JMenuItem("Hello, who are you?");
    submenu.add(menuItem);

    menuItem = new JMenuItem("How long have you been set shop here?");
    submenu.add(menuItem);

    menuItem = new JMenuItem("What is this place?");
    submenu.add(menuItem);

    menuItem = new JMenuItem("Why am I here?");
    submenu.add(menuItem);
    menu.add(submenu);
    menuBar.add(menu);
    return menuBar;
    }

    public Container createContentPane() {

    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setOpaque(false);

    output = new JTextArea(5, 30);
    output.setEditable(false);
    scrollPane = new JScrollPane(output);

    contentPane.add(scrollPane, BorderLayout.CENTER);

    return contentPane;
    }
    protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = Menu.class.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
    }
    private static void createAndShowGUI() {

    JFrame frame = new JFrame("TBGRPG");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Menu demo = new Menu();
    frame.setJMenuBar(demo.createMenuBar());
    frame.setContentPane(demo.createContentPane());

    frame.setSize(450, 260);
    frame.setVisible(true);
    }
    public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
    }
    }

1 个答案:

答案 0 :(得分:0)

使用JOptionPane

JOptionPane.showMessageDialog(null,"whatever text you want to show");