2012-08-30 110 views
1

我正在嘗試創建gui,它在重新加載XML文件(應用程序根據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> 
+0

tldr;您是否在重新填充頂層窗口的contentPane後嘗試調用'pack()'? –

+0

是的,我試過了(在設置新的XML路徑後將它添加到ActionListener中),並且它不起作用。我會繼續保持一段時間,如果沒有答案,我會刪除它並嘗試提供較短的示例。對不起,那篇長文章。 還有一件事 - 我試圖通過更改ActionListener中的文件路徑來重新加載文件。是否足夠實際重新加載文件在內存中?也許這是問題(我無法正確處理文件加載/重新加載)。 –

+0

我chcecked如果新文件存在,當我嘗試加載它,它似乎(它添加到ActionListener:file.exists()返回true,.getName()與.length()返回正確的名稱和文件大小) 。我明天會回到這個。 –

回答

0

我已經解決我的問題:

  1. 從XML創建技工到它自己的方法(移動的JButton它置於方法是創建整個GUI之前;
  2. 通過方法爲根據XML數據生成的JButtons設置新的JPanel;
  3. 在ActionListener中爲那些JButton調用removeAll()for 加載新的XML文件JButton就在調用方法爲新文件。

它工作正常。