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>
基本上,我需要将元素sceneElement
的值更改为
的值,无论它们确定了什么选择,但我一点也不知道如何做到这一点,有人能给我指明正确的方向吗?
我想你就快到了。
希望下面的代码对您有所帮助。
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);
}
}
}
下面的非结构化表元素如何结构化,而不使用任何库。 所需的表: 维护html元素的属性顺序很重要。我试过用BeautifulSoup。它改变了顺序。请建议任何解决此问题的pythonic方法,它不需要使用beautifulsoup或lxml。
我有一个复杂的UI设置,如下所示: 谢谢你。
我已经被这件事困扰了几天,希望你能帮助我解决这个问题。我创建了一个类似流程图的应用程序来绘制不同类型的铅锤项目,并将不同的endpoint连接到这些项目上。将其创建为元素后,将根据铅锤项目的类型动态添加endpoint。是这样的吗。 这就是我创建铅锤项目的方式。 然后根据endpoint的类型添加endpoint。 这就是它看起来的样子 屏幕一 我设法从方法“GetConnections”中获取数
一、 我使用jlist来显示我的数据库数据。这个过程可以很好地从数据库中获取数据并显示在jlist元素中。我需要通过单击事件在jlist元素下显示我的数据。示例:包含用户名的Jlist,单击特定用户后,它应在Jlist元素下方显示该用户的属性。 如果有方法执行此过程,或者无法在特定jlist元素下显示,请建议我。 提前谢谢。
问题内容: 我正在尝试创建一个简单的程序来管理员工。尝试添加新员工时,我似乎无法让该员工显示在Jlist上。 主机… 添加员工的对话… 这就是我用来添加员工的方法 我认为这是正确的方法,但似乎无效。任何帮助将不胜感激。 问题答案: 有两个问题, 第一个: 不好:。使用equals(…)或equalsIgnoreCase(…)。理解检查是否绝对不是您感兴趣的 引用 相等性。您需要 函数 相等性,这是
假设我有一个像素化的图像……我想用处理来重画它。 起初应该有一个随机的彩色像素网格——过了一段时间,像素化的图像会通过这个像素化的网格出现。 我想我可以用这样的系统来处理这个问题:如果一个特定的随机颜色的像素与像素化图像的颜色相匹配,那么应该会有一个新的像素被绘制出来。 为了更好地理解,请看一下这段代码: 所以我能够在颜色匹配的情况下绘制像素——但是因为彩色噪声是随机绑定的——它们会再次消失……我