Frage

I'm running into a problem, I believe with the actionlistener. The weird thing is that it works on some computers and not on others. All computers are running JE7.1. So, on some systems, it works flawlessly, the user enters text, presses enter and the text is appended to the JTextPane and updates the static userInput variable. On other computers though, text is appended, but the variable never seems to update.

For example: user types in "Hello" "Hello is appened to the JtextPane and the static variable userInput = "Hello"

Am I missing something very silly here? Thank you in advance.

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());
        }
    }
}
War es hilfreich?

Lösung

To (try) to answer your question, I'm guessing you are running into multi-thread issues. Swing events run in their own thread thread, the Event Dispatch Thread. If you are accessing the variable from another thread, you will run into occasional situations where the variables are not synchronized between threads.

In the bigger picture, taking shortcuts in writing Java code will generally end up costing you a lot of time and effort. Placing your entire UI in static variables with static methods is not a good idea. Take the time to instantiate an instance. Beyond that, I'm guessing you didn't start the UI using SwingUtilities.invokeLater(). Read the Swing Tutorial, which I linked above. Swing works well, but it is not simple.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top