1

XMLファイルをリロードした後に要素の一部を変更するGUIを作成しようとしています(アプリケーションは、XMLファイルの内容に従って、開始時にいくつかのGUI要素を作成します)。

アプリケーションを起動するとすべて正常に動作します (test.xml がロードされている場合は 20 個のボタン [10 個の単語と翻訳のペア]、testTwo.xml がロードされている場合は 4 個のボタン [2 個の単語と翻訳のペア] が作成されます) が、方法がわかりません。その後(ボタンをクリックした後)、GUIコンテンツをリロードまたはシャッフルします。

revalidate(); を入れてみました。ActionListener で、しかし私のアプリケーションでは動作しません。

これを行う方法について正しい方向に向けていただければ幸いです。

GUI: XmlGui.java

package xmltest;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import net.miginfocom.swing.MigLayout;
import org.xml.sax.SAXException;

public class XmlGui extends JFrame {

    protected JPanel panel;
    protected JFrame frame;
    protected File file = new File("src/xmltest/test.xml");
    protected JButton setOne, setTwo, shuffle;
    protected JTextArea text;
    protected XmlTest engine;

    public XmlGui() throws ParserConfigurationException, SAXException, IOException {

        XmlEvent xmlEvent = new XmlEvent(this);

        SAXParserFactory spfac = SAXParserFactory.newInstance();
        SAXParser sp = spfac.newSAXParser();
        XmlTest engine = new XmlTest();
        sp.parse(file, engine);
        engine.readList();
        engine.shuffleList(1);

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        panel = new JPanel(new MigLayout("Insets 0"));

        setOne = new JButton("Default (test.xml)");
        setOne.addActionListener(xmlEvent);
        panel.add(setOne, "split 3");

        setTwo = new JButton("Next set (testTwo.xml)");
        setTwo.addActionListener(xmlEvent);
        panel.add(setTwo);

        shuffle = new JButton("Shuffle");
        shuffle.addActionListener(xmlEvent);
        panel.add(shuffle, "wrap");

        for (int i = 0; i < engine.cardList.size(); i++) {

            JPanel xpanel = new JPanel(new MigLayout("Insets 0"));
            final String test = engine.cardList.get(i).getTextOne();
            JButton word = new JButton(engine.cardList.get(i).getWord()+" ("+i+")");
            word.setPreferredSize(new Dimension(200, 40));
            word.addActionListener(new ActionListener(){
                public void ActionListener(ActionEvent event) {

                }

                @Override
                public void actionPerformed(ActionEvent e) {
                    text.setText(test);
                }
            });

            JButton translation = new JButton(engine.cardList.get(i).getTranslation()+" ("+i+") ");
            translation.setName("translation"+i);
            translation.setPreferredSize(new Dimension(200, 40));

            xpanel.add(word);
            xpanel.add(translation);
            panel.add(xpanel, "wrap");

        }

        text = new JTextArea();
        text.setLineWrap(true);
        text.setPreferredSize(new Dimension(400, 45));
        panel.add(text);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] arguments) throws ParserConfigurationException, SAXException, IOException {

        XmlGui gui = new XmlGui();

    }

}

XML メソッド: XmlTest.java

package xmltest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XmlTest extends DefaultHandler {

       Card card;
       private String temp;
       ArrayList<Card> cardList = new ArrayList<Card>();

       public void characters(char[] buffer, int start, int length) {
              temp = new String(buffer, start, length);
       }


       public void startElement(String uri, String localName,
                     String qName, Attributes attributes) throws SAXException {
              temp = "";
              if (qName.equalsIgnoreCase("Card")) {
                     card = new Card();

              }
       }

       public void endElement(String uri, String localName, String qName)
                     throws SAXException {

              if (qName.equalsIgnoreCase("Card")) {
                     cardList.add(card);

              } else if (qName.equalsIgnoreCase("Word")) {
                     card.setWord(temp);
              } else if (qName.equalsIgnoreCase("Translation")) {
                     card.setTranslation(temp);
              } else if (qName.equalsIgnoreCase("TextOne")) {
                     card.setTextOne(temp);
              } else if (qName.equalsIgnoreCase("TextTwo")) {
                     card.setTextTwo(temp);
              }

       }

       public void readList() {
              System.out.println("Number of cards in the collection: " + cardList.size()  + ".\n");
              Iterator<Card> it = cardList.iterator();
              while (it.hasNext()) {
                     System.out.println(it.next().toString());
              }
       }

       public void shuffleList(int x) {
              System.out.println("Shuffled cards order ("+x+"): ");
              Collections.shuffle(cardList);
              Iterator<Card> it = cardList.iterator();
              while (it.hasNext()) {
                     System.out.print(it.next().shuffledList());
              }
              System.out.println("\n");
       }

}

イベント クラス: XmlEvent.java

package xmltest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class XmlEvent implements ActionListener {

        XmlGui xmlGui;

        XmlEvent (XmlGui in) {

        xmlGui = in;
    }


    @Override
    public void actionPerformed(ActionEvent e) {

        Object source = e.getSource();

        if (source.equals(xmlGui.setOne)) {

            xmlGui.file = new File("src/xmltest/test.xml");
            xmlGui.text.setText("test.xml");

        }

        if (source.equals(xmlGui.setTwo)) {

            xmlGui.file = new File("src/xmltest/testTwo.xml");
            xmlGui.text.setText("testTwo.xml");

        }

        if (source.equals(xmlGui.shuffle)) {

            xmlGui.text.setText("Shuffling list!");

        }

    }

}

XML データの表現: Card.java

package xmltest;

public class Card {

       private String word;
       private String translation;
       private String textOne;
       private String textTwo;

       public Card() {
       }

       public Card(String word, String translation, String textOne, String textTwo) {
              this.word = word;
              this.translation = translation;
              this.textOne = textOne;
              this.textTwo = textTwo;
       }

       public String getWord() {
              return word;
       }

       public void setWord(String word) {
              this.word = word;
       }

       public String getTranslation() {
              return translation;
       }

       public void setTranslation(String translation) {
              this.translation = translation;
       }

       public String getTextOne() {
              return textOne;
       }

       public void setTextOne(String textOne) {
              this.textOne = textOne;
       }

       public String getTextTwo() {
              return textTwo;
       }

       public void setTextTwo(String textTwo) {
              this.textTwo = textTwo;
       }

       public String toString() {
              StringBuffer sb = new StringBuffer();
              sb.append("Card details:");
              sb.append("\nWord: " + getWord());
              sb.append("\nTranslation: " + getTranslation());
              sb.append("\nTextOne: " + getTextOne());
              if(getTextTwo().equals("blank")) {
                  sb.append("\n\n");
              } else {
              sb.append("\nTextTwo: " + getTextTwo()+"\n\n");
              }

              return sb.toString();
       }

       public String shuffledList() {
           return getWord()+" ";
       }
}

XML1: test.xml

<?xml version="1.0" encoding="UTF-8"?>
<cards>
    <card>
        <word>cloud</word>
        <translation>chmura</translation>
        <textOne>D: a visible collection of particles of water or ice suspended in the air.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>rain</word>
        <translation>deszcz</translation>
        <textOne>D: water that is condensed from the aqueous vapor in the atmosphere and falls to earth in drops.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>wind</word>
        <translation>wiatr</translation>
        <textOne>D: air in natural motion, as that moving horizontally at any velocity along the earth's surface.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>storm</word>
        <translation>burza</translation>
        <textOne>D: a disturbance of the normal condition of the atmosphere, manifesting itself by winds of unusual force or direction, often accompanied by rain, snow, hail, thunder, and lightning, or flying sand or dust.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>blizzard</word>
        <translation>zamieć</translation>
        <textOne>D: a storm with dry, driving snow, strong winds, and intense cold.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>fog</word>
        <translation>mgła</translation>
        <textOne>D: a cloudlike mass or layer of minute water droplets or ice crystals near the surface of the earth, appreciably reducing visibility.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>sunny</word>
        <translation>słoneczny</translation>
        <textOne>D: abounding in sunshine.</textOne>
        <textTwo>test</textTwo>
    </card>
    <card>
        <word>weather</word>
        <translation>pogoda</translation>
        <textOne>D: the state of the atmosphere with respect to wind, temperature, cloudiness, moisture, pressure, etc.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>temperature</word>
        <translation>temperatura</translation>
        <textOne>D: a measure of the warmth or coldness of an object or substance with reference to some standard value.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>hail</word>
        <translation>grad</translation>
        <textOne>D: showery precipitation in the form of irregular pellets or balls of ice.</textOne>
        <textTwo>blank</textTwo>
    </card>
</cards>

XML2: testTwo.xml

<?xml version="1.0" encoding="UTF-8"?>
<cards>
    <card>
        <word>cloud</word>
        <translation>chmura</translation>
        <textOne>D: a visible collection of particles of water or ice suspended in the air.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>rain</word>
        <translation>deszcz</translation>
        <textOne>D: water that is condensed from the aqueous vapor in the atmosphere and falls to earth in drops.</textOne>
        <textTwo>blank</textTwo>
    </card>
</cards>
4

1 に答える 1

0

私は次の方法で問題を解決しました:

  1. JButton を XML 作成メカニズムから独自のメソッドに移動します(GUI 全体を作成するメソッドに配置される前に。
  2. メソッドによって XML データに従って生成された JButton の新しい JPanel をセットアップします。
  3. 新しいファイルのメソッドを呼び出す直前に、新しい XML ファイル JButton をロードするために、ActionListener の JButton を使用して JPanel の removeAll() を呼び出します。

それは正常に動作します。

于 2012-08-31T16:11:45.270 に答える