2015-12-25 94 views
-1

我正在爲學校開發一個程序,它應該是一個GUI,但我想讓它變得更有趣。所以我決定讓它成爲一個基於文本的遊戲。唯一的問題是我不知道如何在屏幕上添加一個文本框,並讓它顯示用戶選項中發生的不同事情的文本。 代碼就在這裏,我想要一個文本框在戰鬥中出現,或者去商店。如果你能幫上忙,那就太好了!GUI(基於文本的遊戲)中的Java文本框

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

你的意思是一個文本框將被寫入,或顯示文本? – vlatkozelka

+0

JTextField#getText和JTextField#setText。看看[如何使用文本字段](https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html)瞭解更多詳情 – MadProgrammer

回答

0

使用JOptionPane

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

有沒有某個地方我不得不放它? –

+0

只要你需要顯示文本,就可以通過 –

+0

這種方式顯示文本,一個消息對話框應該彈出一個文本。 – vlatkozelka