使用循环添加JButton并在按下时更改它们

时间:2016-05-26 07:10:56

标签: java xml user-interface parsing jbutton

我正在尝试构建一个解析XML文件的程序,并使用XML中的属性生成GUI,它使用循环来生成JButton并将它们添加到GUI中,以便它可以拥有所需数量的按钮。按下按钮GUI应该刷新显示新按钮。这是我到目前为止所得到的:

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>

我不知道从哪里开始我不希望在第一个循环中有另一个循环来制作新按钮但是我想不出一种方法可以自己构建第二个循环并且仍然保留我的变量在范围或理想情况下重新使用相同的循环来显示新按钮,如果有人比我更了解GUI构建可以给我一些关于如何使这项工作的建议,将不胜感激。

1 个答案:

答案 0 :(得分:0)

我已将其更改为使用JList而不是修复问题的JButtons