简体   繁体   中英

How to use a linkedList and multiple classes with my Java GUI

How should I use a linked list with my GUI program? The program is supposed to have blocks (a block being a JPanel) with textareas and dropdown menus. The number of blocks depends on the number of times the user presses a button. It's supposed to be possible to put blocks within blocks. I want to store the information created, using a linked list and then saving to a file. I'm thinking, for the sub blocks, there would be linked lists within the linked list. Should I use Java.util.LinkedList? How do I add the information? A Node class? Should that be in a separate file? I started to try sitting up a linked list, but it's getting me confused.

I'm still unsure about how a person is supposed to make GUI's. I've only seen really simple GUI's. Can anyone tell me how I should arrange things? At the moment, I have three files, one for the main GUI window, one for the question blocks to be inserted within that, and one for some tools to use within those two files to make the code easier to understand. But, there seems to be something wrong with the question block file, because I've been making the background white for each JPanel, and there's a gray outline around the question blocks when I run the program.

I probably shouldn't paste a ton of code here... These are my files: http://asj127.webs.com/BuildAssessmentWindow.java http://asj127.webs.com/QuestionBlock.java http://asj127.webs.com/JPanelTools.java

I think you should separate the data model from the gui, and yes, add a list (LinkedList or ArrayList) to the ListNode1 class:

public class ListNode1 implements java.io.Serializable {
    private String num;
    private String questionText;
    private String answerText;
    private List<ListNode1> subNodes = new LinkedList<ListNode>();
...
    public List<ListNode1> getSubNodes() {
        return subNodes;
    }

    public void addSubNode(ListNode1 subNode) {
        subNodes.add(subNode);
    }

    public void removeSubNode(ListNode1 subNode) {
        subNodes.remove(subNode);
    }
}

Note that I have made the class serializable so that you can easily save it to or read it from a file.

This structure is in fact an n-ary tree (or forest).

At the gui level, using a list would be redundant because a Container such as JPanel already contains the list of all the components it contains, So you can simply use a JPanel to hold the QuestionBlock's of all the sub-nodes.

I would link a QuestionBlock to a ListNode1 instead of a list:

public class QuestionBlock extends JPanel {
    private JPanel container;
    private JComboBox numberDropDown, answerDropDown;
    private JTextArea question;
    private ListNode1 node;

    public QuestionBlock(ListNode1 node)
    {
        super(new FlowLayout());
        //setLocation(new Point(0, 0));
        setVisible(true);
        //setSize(800, 610);
        //setBackground(new Color(0, 0, 0));
        this.node=node;
        createGUI();
    }

    public void createGUI()
    {
        container = new JPanel();
        container.setLayout(new BorderLayout());

        numberDropDown = new JComboBox();
        numberDropDown.addItem("1.");
        numberDropDown.addItem("a.");
        container.add(panelTools.addThisComponent(numberDropDown), BorderLayout.LINE_START);

        questionsBlock = panelTools.addNewJPanel();
        questionsBlock.setLayout(new BorderLayout());

        question = new JTextArea(4, 30);
        question.setText(node.getQuestionText);
        question.setBackground(new Color(250, 250, 250)); //light gray color for noticeability
        questionsBlock.add(panelTools.addThisComponent(question), BorderLayout.PAGE_START);

        answerDropDown = new JComboBox();
        answerDropDown.addItem("--Select--");
        answerDropDown.addItem("SubQuestion");
        questionsBlock.add(panelTools.addThisComponent(answerDropDown), BorderLayout.PAGE_END);

        container.add(questionsBlock, BorderLayout.CENTER);
        add(container);
        for (ListNode1 subNode: node.getSubNodes()) {
           add(new QuestionBlock());
        }
    }
}

You would also need a method to save the answers:

public void saveAnswers() {
    node.setAnswerText((String)answerDropDown.getSelectedItem());
    for (Component comp: getComponents()) {
        if (comp instanceof QuestionBlock) {
            ((QuestionBlock)comp).saveAnswers();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM