选择JList项时,使用从XML解析的新元素重新绘制GUI

时间:2016-05-31 10:27:23

标签: java xml swing

我正在尝试使用java和XML选择你自己的冒险故事到目前为止我有XML解析和java显示带有JTextPane的GUI,显示场景描述和显示选项的JList。用户通过在列表中选择它并按下标有“确认”的JButton来选择他们想要的选项但是我不知道从哪里开始我需要一种方法来重新绘制整个JFrame,当用户使用从XML获取的新值按下按钮。有没有一种简单的方法可以做到这一点我想避免在一切周围打一个巨大的循环或者用这里的新值重复我的整个程序是我的java代码:

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;
import javax.swing.JTextArea;
import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;

public class TextAdventureGUI extends JFrame implements ActionListener {

    private JPanel contentPane;
    /**
     * @wbp.nonvisual location=-20,79
     */
    private final JPanel panel = new JPanel();
    private static JList list;
    JScrollPane scrollPane;
    private final JTextPane txtpn = new JTextPane();
    private final JScrollPane scrollPane_1 = new JScrollPane();
    private final JButton Button = new JButton("Confirm");

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                    File inputFile = new File("input");
                    SAXBuilder saxB = new SAXBuilder();

                    Document doc = saxB.build(inputFile);

                    Element storyElement = doc.getRootElement();

                    List<Element> scenesList = storyElement.getChildren();

                    Element sceneElement = scenesList.get(1);
                    List<Element> sceneChildren = sceneElement.getChildren();

                    List<Element> choicesList = sceneChildren.subList(1, sceneChildren.size());


                    TextAdventureGUI frame = new TextAdventureGUI(sceneElement, choicesList);
                    frame.setVisible(true);



                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TextAdventureGUI(Element scene, List<Element> choices) {


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 482, 311);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new GridLayout(5, 0));
        //String[] array = null; 
        list = new JList(choices.toArray());
        scrollPane = new JScrollPane(list);
        contentPane.add(scrollPane_1);
        scrollPane_1.setViewportView(txtpn);
        txtpn.setEditable(false);
        txtpn.setText(scene.getChildText("SceneDescription"));

        Button.addActionListener(this);

        contentPane.add(scrollPane);
        scrollPane.setViewportView(list);

        contentPane.add(Button);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getActionCommand().equals("Confirm"));
        {
            Element Selection = (Element) list.getSelectedValue();
        }
    }
}

这是它解析的XML:

<?xml version = "1.0"?>
<Story>
    <Scene id = "scene1">
        <SceneDescription>Insert Scene Description
        </SceneDescription>

        <choice no="1">
            <choiceDescription>choice description 1 </choiceDescription>
            <outcome>end1</outcome>
        </choice>

        <choice no="2">
            <choiceDescription>choice description 2 </choiceDescription>
            <outcome>scene3</outcome>
        </choice>
    </Scene>

    <Scene id = "scene2">
        <SceneDescription>You are in a room, it's dark but you can see a faint sliver of light coming from what looks like a crack in the door what would you like to do?
        </SceneDescription>

        <choice no="1">
            <choiceDescription>Wait Here </choiceDescription>
            <outcome>scene1</outcome>
        </choice>

        <choice no="2">
            <choiceDescription>Try to leave the room </choiceDescription>
            <outcome>scene3</outcome>
        </choice>

        <choice no="3">
            <choiceDescription>Open the door to let in light and search the room</choiceDescription>
            <outcome>scene4</outcome>
        </choice>
    </Scene>

    <Scene id = "scene3">
        <SceneDescription>Insert Scene Description
        </SceneDescription>

        <choice no="1">
            <choiceDescription>choice description 1 </choiceDescription>
            <outcome>end1</outcome>
        </choice>

        <choice no="2">
            <choiceDescription>choice description 2 </choiceDescription>
            <outcome>scene1</outcome>
        </choice>
    </Scene>

    <Scene id = "end1">
        <SceneDescription>ending
        </SceneDescription>

    </Scene>

</Story>

基本上我需要将Element sceneElement的值更改为他们确认的任何选择的<outcome>,我一点也不知道怎么做才能有人指出我正确的方向?

1 个答案:

答案 0 :(得分:0)

我想你差点就到了。

  1. 您每次都没有重新初始化您的列表。
  2. 解析模型后,您必须根据xml
  3. 创建一个场景(模型)
  4. 每个场景都有选择列表(场景对象列表)
  5. 确认列表选择时传递与要加载的选项有关的场景。
  6. 希望以下代码可以帮助您。

    场景模型

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Objects;
    
    /**
     *
     * @author user
     */
    public class Scene {
       private  String id;
       private String descrition;
    
        List<Choice> choices = new ArrayList<Choice>();
    
        @Override
        public int hashCode() {
            int hash = 3;
            return hash;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Scene other = (Scene) obj;
            if (!Objects.equals(this.id, other.id)) {
                return false;
            }
            return true;
        }
    
        public Scene(String id, String descrition) {
            this.id = id;
            this.descrition = descrition;
        }
    
        public List<Choice> getChoices() {
            return choices;
        }
    
        public String getId() {
            return id;
        }
    
        public String getDescrition() {
            return descrition;
        }
    
    
    }
    

    选择模型

    public class Choice {
        String id;
        String description;
        String outSceneId;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public String getOutSceneId() {
            return outSceneId;
        }
    
        public void setOutSceneId(String outSceneId) {
            this.outSceneId = outSceneId;
        }
    
        @Override
        public String toString() {
            return "Choice{" + "description=" + description + '}';
        }
    
    }
    

    显示场景的框架

    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import java.awt.GridLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JTextPane;
    import java.io.*;
    import java.util.*;
    import javax.swing.DefaultListModel;
    import org.jdom2.*;
    import org.jdom2.input.SAXBuilder;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    
    public class TextAdventureGUI extends JFrame implements ActionListener {
    
        private JPanel contentPane;
        /**
         * @wbp.nonvisual location=-20,79
         */
        private final JPanel panel = new JPanel();
        private static JList list;
        JScrollPane scrollPane;
        private final JTextPane txtpn = new JTextPane();
        private final JScrollPane scrollPane_1 = new JScrollPane();
        private final JButton Button = new JButton("Confirm");
        private final DefaultListModel<Choice> model = new DefaultListModel<Choice>();
    
        private Map<String, Scene> scenesMap = new HashMap<String, Scene>();
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        TextAdventureGUI storyBoard = new TextAdventureGUI();
                        File inputFile = new File("input");
                        SAXBuilder saxB = new SAXBuilder();
    
                        Document doc = saxB.build(inputFile);
    
                        Element storyElement = doc.getRootElement();
                        Scene firstScene = null;
                        List<Element> scenesList = storyElement.getChildren();
                        for (Element sceneElement : scenesList) {
                            Scene scene = storyBoard.buildScene(sceneElement);
                            storyBoard.getScenesMap().put(scene.getId(), scene);
                            if (firstScene == null) {
                                firstScene = scene;
                            }
                        }
                        storyBoard.initScene(firstScene);
                        storyBoard.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        private Scene buildScene(Element sceneElement) {
            Scene scene = null;
            String id = sceneElement.getAttributeValue("id");
            if (id != null) {
                String sceneDescription = sceneElement.getChild("SceneDescription").getText();
                scene = new Scene(id, sceneDescription);
                for (Element element : sceneElement.getChildren()) {
                    if (element.getName().equals("choice")) {
                        scene.getChoices().add(this.buildChoice(element));
                    }
                }
            }
            return scene;
        }
    
        private Choice buildChoice(Element choiceElement) {
            Choice choice = null;
            String id = choiceElement.getAttributeValue("no");
            if (null != id) {
                choice = new Choice();
                choice.setId(id);
                choice.setDescription(choiceElement.getChildText("choiceDescription"));
                choice.setOutSceneId(choiceElement.getChildText("outcome"));
            }
            return choice;
        }
    
        public TextAdventureGUI() {
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 482, 311);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(new GridLayout(5, 0));
            //String[] array = null; 
            list = new JList();
            list.setModel(model);
            scrollPane = new JScrollPane(list);
            contentPane.add(scrollPane_1);
            scrollPane_1.setViewportView(txtpn);
            txtpn.setEditable(false);
            Button.addActionListener(this);
            contentPane.add(scrollPane);
            scrollPane.setViewportView(list);
            contentPane.add(Button);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("Confirm"));
            {
                Choice choice = (Choice) list.getSelectedValue();
                Scene outCome = scenesMap.get(choice.getOutSceneId());
                if (outCome != null) {
                    initScene(outCome);
                }
            }
        }
    
        public Map<String, Scene> getScenesMap() {
            return scenesMap;
        }
    
        public void initScene(Scene scene) {
            txtpn.setText(scene.getDescrition());
            model.clear();
            for (Choice choice : scene.getChoices()) {
                model.addElement(choice);
            }
        }
    
    }