java jlist 重绘_选择JList项时,使用从XML解析的新元素重新绘制GUI(Repainting GUI with new Elements parsed from XML when a ...

I think you are almost there.

You do not have re-intialise your list everytime.

Once you parse the model you have to create a scene(Model) based on the xml

Each scene will have list of choices(list of scene objects)

On confirming the list selection Pass the scene pertaining to the choice to load.

Hope the below code helps you.

The Scene Model

import java.util.ArrayList;

import java.util.List;

import java.util.Objects;

/**

*

* @author user

*/

public class Scene {

private String id;

private String descrition;

List choices = new ArrayList();

@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 getChoices() {

return choices;

}

public String getId() {

return id;

}

public String getDescrition() {

return descrition;

}

}

The Choice Model

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 + '}';

}

}

And the Frame which displays the scenes

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 model = new DefaultListModel();

private Map scenesMap = new HashMap();

/**

* 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 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 getScenesMap() {

return scenesMap;

}

public void initScene(Scene scene) {

txtpn.setText(scene.getDescrition());

model.clear();

for (Choice choice : scene.getChoices()) {

model.addElement(choice);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值