Actionlistener无法正常运行

时间:2014-05-07 15:09:03

标签: java variables static actionlistener jtextfield

我遇到了问题,我相信使用actionlistener。奇怪的是,它适用于某些计算机,而不适用于其他计算机。所有计算机都运行JE7.1。因此,在某些系统上,它可以完美地工作,用户输入文本,按Enter键,文本将附加到JTextPane并更新静态userInput变量。但是在其他计算机上,会附加文本,但变量似乎永远不会更新。

例如: “Hello”中的用户类型 “Hello被添加到JtextPane和静态变量userInput =”Hello“

我错过了一些非常愚蠢的东西吗?提前谢谢。

package com.core;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.text.*;


public class GUI {
    public static JFrame f;
    private static JPanel topPanel = new JPanel();
    private static JTextPane textArea = new JTextPane();
    public static JTextField inputText = new JTextField();
    private static String userInput = "";
    private static Player player;

    public static JPanel sidePanel = new JPanel();

    public JFrame buildFrame(){
        f = new JFrame("AMSA World");
        f.setSize(800, 600);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridBagLayout());

        textArea.setFocusable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        ActionListener listener = new ActionListener() {  
            public void actionPerformed(ActionEvent evt) {
                String text = inputText.getText();
                if(!text.equals("")){
                    appendToPane(text + "\n\n", Color.blue);
                    inputText.setText("");
                    if(text.startsWith("use ")){
                        ArrayList<Item> inventory = player.getInventory();
                        String key = text.substring(4);
                        boolean found = false;
                        for(Item i : inventory){
                            if(i.getName().equalsIgnoreCase(key)){
                                found = true;
                                i.use();
                                break;
                            }
                        }
                        if(!found){
                            appendToPane("Item does not exist in your iventory.\n\n", Color.black);
                        }
                    }
                    if(text.startsWith("inventory")){
                        ArrayList<Item> inventory = player.getInventory();
                        if(inventory.size() > 0){
                            for(Item i : inventory){
                                appendToPane(i.getName() +"\n", Color.darkGray);
                            }
                        }
                        else
                            appendToPane("Your iventory is empty.\n\n", Color.black);
                        appendToPane("#", Color.blue);
                    }
                    else{
                        userInput = text;
                    }
                }
            } 
        };
        inputText.addActionListener(listener);

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(5,5,5,5);
        c.weightx = 1.0;
        c.gridwidth = 3;
        c.weighty = 0.025;
        c.gridx = 0;
        c.gridy = 0;
        f.add(topPanel, c);

        c.fill = GridBagConstraints.BOTH;
        c.weighty = 1.0;
        c.gridwidth = 2;
        c.gridx = 0;
        c.gridy = 1;
        f.add(scrollPane, c);

        c.fill = GridBagConstraints.BOTH;
        c.weighty = 1.0;
        c.gridwidth = 1;
        c.gridx = 2;
        c.gridy = 1;
        f.add(sidePanel, c);
        sidePanel.setVisible(false);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weighty = 0.025;
        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = 2;
        f.add(inputText, c);

        f.setVisible(true);
        return f;
    }
    public static JTextField getTextField(){
        return inputText;
    }
    public static void toggleTextField(boolean value){
        inputText.setEnabled(value);
    }
    public static String getUserInput(){
        return userInput;
    }
    public static void setUserInput(String s){
        userInput = s;
    }
    public static JPanel getTopPanel(){
        return topPanel;
    }
    public static JPanel getSidePanel(){
        return sidePanel;
    }
    public static void setPlayer(Player p){
        player = p;
    }

    public static void appendToPane(String msg, Color c){
        if(inputText.isEnabled()){
        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

        textArea.setCharacterAttributes(aset, false);
        textArea.replaceSelection(msg);
        textArea.setCaretPosition(textArea.getDocument().getLength());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

要(尝试)回答你的问题,我猜你遇到了多线程问题。 Swing事件在它们自己的线程线程Event Dispatch Thread中运行。如果从另一个线程访问该变量,则会遇到线程之间变量未同步的偶然情况。

从更大的角度来看,编写Java代码的快捷方式通常最终会耗费大量的时间和精力。使用静态方法将整个UI放在静态变量中并不是一个好主意。花时间实例化一个实例。除此之外,我猜你没有使用SwingUtilities.invokeLater()启动UI。阅读我上面链接的Swing教程。 Swing效果很好,但并不简单。