2016-05-26 4 views
0

XMLファイルを解析し、XMLの属性を使用してGUIを生成するプログラムを構築しようとしています。ループを使用してJButtonを作成し、ボタンを押したときに必要な数のボタンを持つことができ、GUIは新しいボタンの表示をリフレッシュする必要があります。JButtonをループで追加し、押したときに変更する

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; 

public class TextAdventureGUI extends JFrame implements ActionListener { 

    private JPanel contentPane; 
    /** 
    * @wbp.nonvisual location=-20,79 
    */ 
    private final JPanel panel = new JPanel(); 

    /** 
    * 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> choicesList = sceneElement.getChildren(); 

        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 buttonNames[] = {"choice1", "choice2", "choice3", "choice4", "choice5"}; 
     JButton[] cButtons = new JButton[18]; 

     for(int temp = 1; temp <= choices.size(); temp++) 
     { 
      Element choice = choices.get(temp); 
      //String bName = buttonNames[temp]; 
      cButtons[temp] = new JButton(choice.getChildText("choiceDescription")); 
      cButtons[temp].addActionListener(this); 
      contentPane.add(cButtons[temp]); 

     } 

     JTextArea textArea = new JTextArea(scene.getChildText("sceneDescription")); 
     textArea.setBounds(5, 11, 451, 104); 
     contentPane.add(textArea); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 

    } 
} 

を、ここで私はパースだ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>This is the scene to parse 
     </SceneDescription> 

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

     <choice no="2"> 
      <choiceDescription>choice description 2 </choiceDescription> 
      <outcome>scene3</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> 

私はどこ私が持っている必要はありませんここから行くか分からないが、これは私がこれまで持っているものです最初のループの中に新しいボタンを作っている別のループですが、自分の上に2番目のループを作成して、変数をスコープに入れておく方法はないと思います。私よりもGUIビルドをよく理解すれば、この作業をどのように行うかについてのアドバイスをいただければ幸いです。

答えて

0

問題を修正したJButtonの代わりにJListを使用するように変更しました

関連する問題